Skip to content

Commit 9c471f0

Browse files
committed
Figured out and corrected the bug in UserDatabase.createUser now its possible to add multiple users to the file without issues
1 parent f837d19 commit 9c471f0

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

src/main/java/UserDatabase.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public boolean UserExists(String username, String email) {
3535
@Override
3636
public void createUser(String username, String password, String email, String type){
3737
User newUser = UserFactory.BirthUser(username, password, email, type);
38+
this.accountList.add(newUser);
3839
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
3940
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
40-
out.writeObject(newUser);
41-
this.accountList.add(newUser);
41+
out.writeObject(this.accountList);
4242
out.close();
4343
fileOut.close();
4444
}catch(Exception e){
@@ -66,19 +66,22 @@ public User getUser(String username) {
6666
public List<User> getList() {
6767
List<User> users = new ArrayList<>();
6868
try(FileInputStream fileIn = new FileInputStream(accounts);
69-
ObjectInputStream in = new ObjectInputStream(fileIn)){
70-
/*User user = (User) in.readObject();*/
69+
ObjectInputStream in = new ObjectInputStream(fileIn)) {
70+
71+
users = (ArrayList<User>) in.readObject();
72+
/*
7173
while(true){
7274
try{
7375
User user = (User) in.readObject();
7476
users.add(user);}
7577
catch(EOFException e){
7678
break;
77-
}
78-
}
79-
}catch(Exception e){
80-
e.printStackTrace();
79+
}*/
80+
return users;
81+
}catch(EOFException e){
82+
return users;
83+
} catch (IOException | ClassNotFoundException ex) {
84+
throw new RuntimeException(ex);
8185
}
82-
return users;
8386
}
8487
}

src/main/java/UserRegistrationUI.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ public void registerUser(String username, String password, String email) {
8686
}
8787
//For Testing purposes
8888
public static void main(String[] args){
89-
UserDatabase testDB = new UserDatabase();
89+
UserDatabase testDB = new UserDatabase(new File("Test4"));
9090
System.out.println(testDB.UserExists("RandomUser", "[email protected]"));
91+
System.out.println(testDB.getList().size());
9192
UserRegistrationUI testUI = new UserRegistrationUI(testDB);
93+
9294
testUI.GetUserCredentials();
9395
}
9496

src/test/java/UserDatabaseTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,45 @@
77
public class UserDatabaseTest {
88
@Test
99
public void addingFilesRightEmailAndUser(){
10-
File accounts = new File("Test");
10+
File accounts = new File("Test3");
1111
UserDatabase accountDatabase = new UserDatabase(accounts);
1212
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
1313
Assertions.assertTrue(accountDatabase.UserExists("MadhavGopakumar", "[email protected]"));
1414
}
1515
@Test
1616
public void addingMultipleFiles(){
17-
File accounts = new File("Test");
17+
File accounts = new File("Test3");
1818
UserDatabase accountDatabase = new UserDatabase(accounts);
1919
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
2020
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "[email protected]"));
2121
}
2222
@Test
2323
public void rightEmailWrongUser(){
24-
File accounts = new File("Test");
24+
File accounts = new File("Test3");
2525
UserDatabase accountDatabase = new UserDatabase(accounts);
2626
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
2727
Assertions.assertTrue(accountDatabase.UserExists("MadG", "[email protected]"));
2828
}
2929
@Test
3030
public void rightUserWrongEmail(){
31-
File accounts = new File("Test");
31+
File accounts = new File("Test3");
3232
UserDatabase accountDatabase = new UserDatabase(accounts);
3333
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
3434
}
3535
@Test
3636
public void listedUsers(){
37-
File accounts = new File("Test");
37+
File accounts = new File("Test3");
3838
UserDatabase accountDatabase = new UserDatabase(accounts);
3939
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
4040
List<User> lst = accountDatabase.getList();
4141
String email = lst.get(0).getEmail();
42+
System.out.println(email);
4243
Assertions.assertTrue(email.equals("[email protected]"));
4344
}
4445

4546
@Test
4647
public void userGot() {
47-
File accounts = new File("Test");
48+
File accounts = new File("Test3");
4849
UserDatabase accountDatabase = new UserDatabase(accounts);
4950
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
5051
User user = accountDatabase.getUser("MeenakshiGopakumar");

0 commit comments

Comments
 (0)