Skip to content

Commit 657a108

Browse files
committed
Merge remote-tracking branch 'origin/15-userdatabase-and-user' into 15-userdatabase-and-user
2 parents 4746d6e + 738ad69 commit 657a108

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/main/java/UserDatabase.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import java.io.*;
22
import java.util.Scanner;
3-
public class UserDatabase implements UserExists, UserCreator{
3+
public class UserDatabase implements UserExists, UserRetriever, UserCreator{
44
File accounts;
55
public UserDatabase(File accounts){
66
this.accounts = accounts;
@@ -43,4 +43,25 @@ public void createUser(String username, String password, String email, String ty
4343
System.out.println("Error");
4444
}
4545
}
46+
47+
@Override
48+
// To be edited to get user from the array format rather than the serialized format.
49+
public User getUser(String username) {
50+
User user = null;
51+
try(FileInputStream fileIn = new FileInputStream(accounts);
52+
ObjectInputStream in = new ObjectInputStream(fileIn)){
53+
do{
54+
try{
55+
user = (User)in.readObject();
56+
}catch(NullPointerException e){
57+
break;
58+
}
59+
}while(!user.getUsername().equals(username));
60+
}catch(IOException e){
61+
e.printStackTrace();
62+
} catch (ClassNotFoundException e) {
63+
throw new RuntimeException(e);
64+
}
65+
return user;
66+
}
4667
}

src/main/java/UserRetriever.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public interface UserRetriever {
2+
User getUser(String username);
3+
}

src/test/java/UserDatabaseTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ public void rightUserWrongEmail(){
3030
UserDatabase accountDatabase = new UserDatabase(accounts);
3131
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
3232
}
33-
}
33+
}

0 commit comments

Comments
 (0)