@@ -8,8 +8,8 @@ public UserDatabase(){
8
8
this .accounts = new File ("TestUserDatabase3.csv" );
9
9
this .accountList = this .getList ();
10
10
}
11
- public UserDatabase (File accounts ) {
12
- if (!accounts .exists ()) {
11
+ public UserDatabase (File accounts ){
12
+ if (!accounts .exists ()){
13
13
try {
14
14
accounts .createNewFile ();
15
15
} catch (IOException e ) {
@@ -21,23 +21,6 @@ public UserDatabase(File accounts) {
21
21
}
22
22
@ Override
23
23
public boolean UserExists (String username , String email ) {
24
- /*User user = null;
25
- try(FileInputStream fileIn = new FileInputStream(accounts);
26
- ObjectInputStream in = new ObjectInputStream(fileIn)){
27
- do{
28
- try{
29
- user = (User)in.readObject();
30
- }catch(NullPointerException e){
31
- user = null;
32
- break;
33
- }
34
- }while(!user.getEmail().equals(email) && !user.getUsername().equals(username));
35
- }catch(IOException e){
36
- e.printStackTrace();
37
- } catch (ClassNotFoundException e) {
38
- throw new RuntimeException(e);
39
- }
40
- return user != null;*/
41
24
for (User user : this .accountList ){
42
25
if (user .getUsername ().equals (username ) || user .getEmail ().equals (email )){
43
26
return true ;
@@ -46,26 +29,16 @@ public boolean UserExists(String username, String email) {
46
29
return false ;
47
30
}
48
31
49
- @ Override
50
- public boolean UserExists (String username ) {
51
- for (User user : this .accountList ){
52
- if (user .getUsername ().equals (username )){
53
- return true ;
54
- }
55
- }
56
- return false ;
57
- }
58
-
59
- // Creates a new user with a username and password, and an email address
32
+ // Creates a new user with a username and password, and an email address
60
33
// The order is username, password, email address, verification status, status
61
34
//
62
35
@ Override
63
36
public void createUser (String username , String password , String email , String type ){
64
37
User newUser = UserFactory .BirthUser (username , password , email , type );
38
+ this .accountList .add (newUser );
65
39
try (FileOutputStream fileOut = new FileOutputStream (accounts )){
66
40
try (ObjectOutputStream out = new ObjectOutputStream (fileOut )){
67
- out .writeObject (newUser );
68
- this .accountList .add (newUser );
41
+ out .writeObject (this .accountList );
69
42
out .close ();
70
43
fileOut .close ();
71
44
}catch (Exception e ){
@@ -77,6 +50,7 @@ public void createUser(String username, String password, String email, String ty
77
50
}
78
51
79
52
@ Override
53
+ // To be edited to get user from the array format rather than the serialized format.
80
54
public User getUser (String username ) {
81
55
User ans = null ;
82
56
for (int i = 0 ; i < (this .getList ().size ()); i ++) {
@@ -92,19 +66,22 @@ public User getUser(String username) {
92
66
public List <User > getList () {
93
67
List <User > users = new ArrayList <>();
94
68
try (FileInputStream fileIn = new FileInputStream (accounts );
95
- ObjectInputStream in = new ObjectInputStream (fileIn )){
96
- /*User user = (User) in.readObject();*/
69
+ ObjectInputStream in = new ObjectInputStream (fileIn )) {
70
+
71
+ users = (ArrayList <User >) in .readObject ();
72
+ /*
97
73
while(true){
98
74
try{
99
75
User user = (User) in.readObject();
100
76
users.add(user);}
101
77
catch(EOFException e){
102
78
break;
103
- }
104
- }
105
- }catch (Exception e ){
106
- e .printStackTrace ();
79
+ }*/
80
+ return users ;
81
+ }catch (EOFException e ){
82
+ return users ;
83
+ } catch (IOException | ClassNotFoundException ex ) {
84
+ throw new RuntimeException (ex );
107
85
}
108
- return users ;
109
86
}
110
87
}
0 commit comments