3
3
import java .util .List ;
4
4
public class UserDatabase implements UserExists , UserRetriever , UserCreator , IRetrieveList {
5
5
File accounts ;
6
+ List <User > accountList ;
6
7
public UserDatabase (File accounts ){
7
8
this .accounts = accounts ;
9
+ this .accountList = this .getList ();
8
10
}
9
11
@ Override
10
12
public boolean UserExists (String username , String email ) {
11
- User user = null ;
13
+ /* User user = null;
12
14
try(FileInputStream fileIn = new FileInputStream(accounts);
13
15
ObjectInputStream in = new ObjectInputStream(fileIn)){
14
16
do{
@@ -24,7 +26,13 @@ public boolean UserExists(String username, String email) {
24
26
} catch (ClassNotFoundException e) {
25
27
throw new RuntimeException(e);
26
28
}
27
- return user != null ;
29
+ return user != null;*/
30
+ for (User user : this .accountList ){
31
+ if (user .getUsername ().equals (username ) || user .getEmail ().equals (email )){
32
+ return true ;
33
+ }
34
+ }
35
+ return false ;
28
36
}
29
37
30
38
// Creates a new user with a username and password, and an email address
@@ -36,6 +44,7 @@ public void createUser(String username, String password, String email, String ty
36
44
try (FileOutputStream fileOut = new FileOutputStream (accounts )){
37
45
try (ObjectOutputStream out = new ObjectOutputStream (fileOut )){
38
46
out .writeObject (newUser );
47
+ this .accountList .add (newUser );
39
48
out .close ();
40
49
fileOut .close ();
41
50
}catch (Exception e ){
@@ -64,10 +73,14 @@ public List<User> getList() {
64
73
List <User > users = new ArrayList <>();
65
74
try (FileInputStream fileIn = new FileInputStream (accounts );
66
75
ObjectInputStream in = new ObjectInputStream (fileIn )){
67
- User user = (User ) in .readObject ();
68
- while (user != null ){
69
- users .add (user );
70
- user = (User )in .readObject ();
76
+ /*User user = (User) in.readObject();*/
77
+ while (true ){
78
+ try {
79
+ User user = (User ) in .readObject ();
80
+ users .add (user );}
81
+ catch (EOFException e ){
82
+ break ;
83
+ }
71
84
}
72
85
}catch (Exception e ){
73
86
e .printStackTrace ();
0 commit comments