Skip to content

Commit 046161e

Browse files
committed
Changed implementation of UserDatabase.UserExists and UserDatabase.getList. There still seems to be an issue with one of the tests
1 parent 3f64f0d commit 046161e

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed
File renamed without changes.

src/main/java/UserDatabase.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import java.util.List;
44
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList{
55
File accounts;
6+
List<User> accountList;
67
public UserDatabase(File accounts){
78
this.accounts = accounts;
9+
this.accountList = this.getList();
810
}
911
@Override
1012
public boolean UserExists(String username, String email) {
11-
User user = null;
13+
/*User user = null;
1214
try(FileInputStream fileIn = new FileInputStream(accounts);
1315
ObjectInputStream in = new ObjectInputStream(fileIn)){
1416
do{
@@ -24,7 +26,13 @@ public boolean UserExists(String username, String email) {
2426
} catch (ClassNotFoundException e) {
2527
throw new RuntimeException(e);
2628
}
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;
2836
}
2937

3038
// 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
3644
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
3745
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
3846
out.writeObject(newUser);
47+
this.accountList.add(newUser);
3948
out.close();
4049
fileOut.close();
4150
}catch(Exception e){
@@ -64,10 +73,14 @@ public List<User> getList() {
6473
List<User> users = new ArrayList<>();
6574
try(FileInputStream fileIn = new FileInputStream(accounts);
6675
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+
}
7184
}
7285
}catch(Exception e){
7386
e.printStackTrace();

src/test/java/UserDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import org.junit.jupiter.api.Test;
33

44
import java.io.File;
5-
import java.util.ArrayList;
65
import java.util.List;
76

87
public class UserDatabaseTest {
@@ -24,6 +23,7 @@ public void addingMultipleFiles(){
2423
public void rightEmailWrongUser(){
2524
File accounts = new File("TestUserDatabase2.csv");
2625
UserDatabase accountDatabase = new UserDatabase(accounts);
26+
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]", "Basic");
2727
Assertions.assertTrue(accountDatabase.UserExists("MadG", "[email protected]"));
2828
}
2929
@Test

0 commit comments

Comments
 (0)