Skip to content

Commit 05b4154

Browse files
committed
Made drastic changes to UserDatabase and implemented User.Data storage is done through inbuilt serialization interface by Java, and not manually. Passed all the tests
1 parent 8087bf8 commit 05b4154

File tree

4 files changed

+56
-26
lines changed

4 files changed

+56
-26
lines changed

src/main/java/User.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.Serializable;
2+
public class User implements Serializable{
3+
private String username;
4+
private String password;
5+
private String email;
6+
boolean verified = false;
7+
boolean online = false;
8+
public User(String username, String password, String email){
9+
this.username = username;
10+
this.password = password;
11+
this.email = email;
12+
}
13+
public String getEmail(){
14+
return this.email;
15+
}
16+
public String getUsername(){
17+
return this.username;
18+
}
19+
public String getPassword(){
20+
return this.password;
21+
}
22+
23+
}

src/main/java/UserDatabase.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import java.io.File;
2-
import java.io.FileNotFoundException;
3-
import java.io.PrintWriter;
1+
import java.io.*;
42
import java.util.Scanner;
53
public class UserDatabase implements UserExists, UserCreator{
64
File accounts;
@@ -9,31 +7,40 @@ public UserDatabase(File accounts){
97
}
108
@Override
119
public boolean UserExists(String username, String email) {
12-
try(Scanner x = new Scanner(this.accounts)){
13-
x.useDelimiter("\n");
14-
while(x.hasNext()) {
15-
String[] y = x.next().split(",");
16-
if(y[0].equals(username) || y[2].equals(email)){
17-
return true;
10+
User user = null;
11+
try(FileInputStream fileIn = new FileInputStream(accounts);
12+
ObjectInputStream in = new ObjectInputStream(fileIn)){
13+
do{
14+
try{
15+
user = (User)in.readObject();
16+
}catch(NullPointerException e){
17+
break;
1818
}
19-
}
20-
}catch(FileNotFoundException e){
21-
System.out.println("Cannot find File");
22-
};
23-
return false;
19+
}while(!user.getEmail().equals(email) && !user.getUsername().equals(username));
20+
}catch(IOException e){
21+
e.printStackTrace();
22+
} catch (ClassNotFoundException e) {
23+
throw new RuntimeException(e);
24+
}
25+
return user != null;
2426
}
2527

2628
// Creates a new user with a username and password, and an email address
2729
// The order is username, password, email address, verification status, status
2830
//
2931
@Override
30-
public void createUser(String username, String password, String email) {
31-
try(PrintWriter out = new PrintWriter(this.accounts)){
32-
out.println(username + "," + password + "," + email + "," + "no" + "," + "offline");
33-
out.flush();
34-
}catch (FileNotFoundException e){
35-
System.out.println("Error creating/writing to file");
36-
e.printStackTrace();
37-
};
32+
public void createUser(String username, String password, String email){
33+
User newUser = new User(username, password, email);
34+
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
35+
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
36+
out.writeObject(newUser);
37+
out.close();
38+
fileOut.close();
39+
}catch(Exception e){
40+
System.out.println("Error");
41+
}
42+
}catch(Exception e){
43+
System.out.println("Error");
44+
}
3845
}
3946
}

src/test/java/TestUserDatabase2.csv

Whitespace-only changes.

src/test/java/UserDatabaseTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
public class UserDatabaseTest {
77
@Test
88
public void addingFilesRightEmailAndUser(){
9-
File accounts = new File("TestUserDatabase.csv");
9+
File accounts = new File("TestUserDatabase2.csv");
1010
UserDatabase accountDatabase = new UserDatabase(accounts);
1111
accountDatabase.createUser("MadhavGopakumar", "123", "[email protected]");
1212
Assertions.assertTrue(accountDatabase.UserExists("MadhavGopakumar", "[email protected]"));
1313
}
1414
@Test
1515
public void addingMultipleFiles(){
16-
File accounts = new File("TestUserDatabase.csv");
16+
File accounts = new File("TestUserDatabase2.csv");
1717
UserDatabase accountDatabase = new UserDatabase(accounts);
1818
accountDatabase.createUser("MeenakshiGopakumar", "123", "[email protected]");
1919
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "[email protected]"));
2020
}
2121
@Test
2222
public void rightEmailWrongUser(){
23-
File accounts = new File("TestUserDatabase.csv");
23+
File accounts = new File("TestUserDatabase2.csv");
2424
UserDatabase accountDatabase = new UserDatabase(accounts);
2525
Assertions.assertTrue(accountDatabase.UserExists("MadG", "[email protected]"));
2626
}
2727
@Test
2828
public void rightUserWrongEmail(){
29-
File accounts = new File("TestUserDatabase.csv");
29+
File accounts = new File("TestUserDatabase2.csv");
3030
UserDatabase accountDatabase = new UserDatabase(accounts);
3131
Assertions.assertTrue(accountDatabase.UserExists("MeenakshiGopakumar", "ma"));
3232
}

0 commit comments

Comments
 (0)