Skip to content

Commit 41d5d1c

Browse files
committed
attempting to change userdatabase to the version in another branch
1 parent 499d728 commit 41d5d1c

File tree

1 file changed

+16
-39
lines changed

1 file changed

+16
-39
lines changed

src/main/java/UserDatabase.java

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public UserDatabase(){
88
this.accounts = new File("TestUserDatabase3.csv");
99
this.accountList = this.getList();
1010
}
11-
public UserDatabase(File accounts) {
12-
if (!accounts.exists()) {
11+
public UserDatabase(File accounts){
12+
if(!accounts.exists()){
1313
try {
1414
accounts.createNewFile();
1515
} catch (IOException e) {
@@ -21,23 +21,6 @@ public UserDatabase(File accounts) {
2121
}
2222
@Override
2323
public boolean UserExists(String username, String email) {
24-
/*User user = null;
25-
try(FileInputStream fileIn = new FileInputStream(accounts);
26-
ObjectInputStream in = new ObjectInputStream(fileIn)){
27-
do{
28-
try{
29-
user = (User)in.readObject();
30-
}catch(NullPointerException e){
31-
user = null;
32-
break;
33-
}
34-
}while(!user.getEmail().equals(email) && !user.getUsername().equals(username));
35-
}catch(IOException e){
36-
e.printStackTrace();
37-
} catch (ClassNotFoundException e) {
38-
throw new RuntimeException(e);
39-
}
40-
return user != null;*/
4124
for(User user: this.accountList){
4225
if(user.getUsername().equals(username) || user.getEmail().equals(email)){
4326
return true;
@@ -46,26 +29,16 @@ public boolean UserExists(String username, String email) {
4629
return false;
4730
}
4831

49-
@Override
50-
public boolean UserExists(String username) {
51-
for(User user: this.accountList){
52-
if(user.getUsername().equals(username)){
53-
return true;
54-
}
55-
}
56-
return false;
57-
}
58-
59-
// Creates a new user with a username and password, and an email address
32+
// Creates a new user with a username and password, and an email address
6033
// The order is username, password, email address, verification status, status
6134
//
6235
@Override
6336
public void createUser(String username, String password, String email, String type){
6437
User newUser = UserFactory.BirthUser(username, password, email, type);
38+
this.accountList.add(newUser);
6539
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
6640
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
67-
out.writeObject(newUser);
68-
this.accountList.add(newUser);
41+
out.writeObject(this.accountList);
6942
out.close();
7043
fileOut.close();
7144
}catch(Exception e){
@@ -77,6 +50,7 @@ public void createUser(String username, String password, String email, String ty
7750
}
7851

7952
@Override
53+
// To be edited to get user from the array format rather than the serialized format.
8054
public User getUser(String username) {
8155
User ans = null;
8256
for (int i = 0; i < (this.getList().size()); i++) {
@@ -92,19 +66,22 @@ public User getUser(String username) {
9266
public List<User> getList() {
9367
List<User> users = new ArrayList<>();
9468
try(FileInputStream fileIn = new FileInputStream(accounts);
95-
ObjectInputStream in = new ObjectInputStream(fileIn)){
96-
/*User user = (User) in.readObject();*/
69+
ObjectInputStream in = new ObjectInputStream(fileIn)) {
70+
71+
users = (ArrayList<User>) in.readObject();
72+
/*
9773
while(true){
9874
try{
9975
User user = (User) in.readObject();
10076
users.add(user);}
10177
catch(EOFException e){
10278
break;
103-
}
104-
}
105-
}catch(Exception e){
106-
e.printStackTrace();
79+
}*/
80+
return users;
81+
}catch(EOFException e){
82+
return users;
83+
} catch (IOException | ClassNotFoundException ex) {
84+
throw new RuntimeException(ex);
10785
}
108-
return users;
10986
}
11087
}

0 commit comments

Comments
 (0)