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