Skip to content

Commit 4ff2095

Browse files
committed
Modifications to UserDatabase from user registration branch
1 parent 046161e commit 4ff2095

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/main/java/UserDatabase.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,23 @@
44
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList{
55
File accounts;
66
List<User> accountList;
7+
public UserDatabase(){
8+
this.accounts = new File("TestUserDatabase3.csv");
9+
this.accountList = this.getList();
10+
}
711
public UserDatabase(File accounts){
12+
if(!accounts.exists()){
13+
try {
14+
accounts.createNewFile();
15+
} catch (IOException e) {
16+
throw new RuntimeException(e);
17+
}
18+
}
819
this.accounts = accounts;
920
this.accountList = this.getList();
1021
}
1122
@Override
1223
public boolean UserExists(String username, String email) {
13-
/*User user = null;
14-
try(FileInputStream fileIn = new FileInputStream(accounts);
15-
ObjectInputStream in = new ObjectInputStream(fileIn)){
16-
do{
17-
try{
18-
user = (User)in.readObject();
19-
}catch(NullPointerException e){
20-
user = null;
21-
break;
22-
}
23-
}while(!user.getEmail().equals(email) && !user.getUsername().equals(username));
24-
}catch(IOException e){
25-
e.printStackTrace();
26-
} catch (ClassNotFoundException e) {
27-
throw new RuntimeException(e);
28-
}
29-
return user != null;*/
3024
for(User user: this.accountList){
3125
if(user.getUsername().equals(username) || user.getEmail().equals(email)){
3226
return true;
@@ -41,10 +35,10 @@ public boolean UserExists(String username, String email) {
4135
@Override
4236
public void createUser(String username, String password, String email, String type){
4337
User newUser = UserFactory.BirthUser(username, password, email, type);
38+
this.accountList.add(newUser);
4439
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
4540
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
46-
out.writeObject(newUser);
47-
this.accountList.add(newUser);
41+
out.writeObject(this.accountList);
4842
out.close();
4943
fileOut.close();
5044
}catch(Exception e){
@@ -72,19 +66,22 @@ public User getUser(String username) {
7266
public List<User> getList() {
7367
List<User> users = new ArrayList<>();
7468
try(FileInputStream fileIn = new FileInputStream(accounts);
75-
ObjectInputStream in = new ObjectInputStream(fileIn)){
76-
/*User user = (User) in.readObject();*/
69+
ObjectInputStream in = new ObjectInputStream(fileIn)) {
70+
71+
users = (ArrayList<User>) in.readObject();
72+
/*
7773
while(true){
7874
try{
7975
User user = (User) in.readObject();
8076
users.add(user);}
8177
catch(EOFException e){
8278
break;
83-
}
84-
}
85-
}catch(Exception e){
86-
e.printStackTrace();
79+
}*/
80+
return users;
81+
}catch(EOFException e){
82+
return users;
83+
} catch (IOException | ClassNotFoundException ex) {
84+
throw new RuntimeException(ex);
8785
}
88-
return users;
8986
}
9087
}

0 commit comments

Comments
 (0)