Skip to content

Commit b0fef64

Browse files
committed
finalized getUser and added tests
1 parent 1a3c77d commit b0fef64

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/main/java/UserDatabase.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public boolean UserExists(String username, String email) {
2525
}
2626
return user != null;
2727
}
28-
// TODO: create userExists that takes 1 parameter: username.
2928

3029
// Creates a new user with a username and password, and an email address
3130
// The order is username, password, email address, verification status, status
@@ -49,23 +48,15 @@ public void createUser(String username, String password, String email, String ty
4948
@Override
5049
// To be edited to get user from the array format rather than the serialized format.
5150
public User getUser(String username) {
52-
User user = null;
53-
try(FileInputStream fileIn = new FileInputStream(accounts);
54-
ObjectInputStream in = new ObjectInputStream(fileIn)){
55-
do{
56-
try{
57-
user = (User)in.readObject();
58-
}catch(NullPointerException e){
59-
break;
60-
}
61-
}while(!user.getUsername().equals(username));
62-
}catch(IOException e){
63-
e.printStackTrace();
64-
} catch (ClassNotFoundException e) {
65-
throw new RuntimeException(e);
51+
User ans = null;
52+
for (int i = 0; i < (this.getList().size()); i++) {
53+
if (this.getList().get(i).getUsername().equals(username)) {
54+
ans = this.getList().get(i);
55+
}
6656
}
67-
return user;
57+
return ans;
6858
}
59+
6960
//Returns an ArrayList with the users that is extracted from the file, so that other objects can use this list.
7061
@Override
7162
public List<User> getList() {

src/test/java/UserDatabaseTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import org.junit.jupiter.api.Test;
33

44
import java.io.File;
5+
import java.util.ArrayList;
6+
import java.util.List;
57

68
public class UserDatabaseTest {
79
@Test
@@ -30,4 +32,24 @@ public void rightUserWrongEmail(){
3032
UserDatabase accountDatabase = new UserDatabase(accounts);
3133
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
3234
}
35+
@Test
36+
public void listedUsers(){
37+
File accounts = new File("TestUserDatabase2.csv");
38+
UserDatabase accountDatabase = new UserDatabase(accounts);
39+
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
40+
List<User> lst = accountDatabase.getList();
41+
String email = lst.get(0).getEmail();
42+
Assertions.assertTrue(email.equals("[email protected]"));
43+
}
44+
45+
@Test
46+
public void userGot() {
47+
File accounts = new File("TestUserDatabase2.csv");
48+
UserDatabase accountDatabase = new UserDatabase(accounts);
49+
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]", "Basic");
50+
User user = accountDatabase.getUser("MeenakshiGopakumar");
51+
String email = user.getEmail();
52+
Assertions.assertTrue(email.equals("[email protected]"));
53+
}
54+
3355
}

0 commit comments

Comments
 (0)