Skip to content

Commit 499d728

Browse files
committed
Merge remote-tracking branch 'origin/15-userdatabase-and-user' into 15-userdatabase-and-user
# Conflicts: # src/main/java/UserDatabase.java
2 parents 4ff2095 + 14960fb commit 499d728

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

src/main/java/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ private String getPassword(){
2020
return this.password;
2121
}
2222

23+
public Boolean PasswordMatch(String attempt){
24+
return (this.getPassword().equals(attempt));
25+
}
26+
2327
@Override
2428
// from Changeable
2529
public void changeFeature(String feature, String newFeature){

src/main/java/UserDatabase.java

Lines changed: 39 additions & 16 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,6 +21,23 @@ 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;*/
2441
for(User user: this.accountList){
2542
if(user.getUsername().equals(username) || user.getEmail().equals(email)){
2643
return true;
@@ -29,16 +46,26 @@ public boolean UserExists(String username, String email) {
2946
return false;
3047
}
3148

32-
// Creates a new user with a username and password, and an email address
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
3360
// The order is username, password, email address, verification status, status
3461
//
3562
@Override
3663
public void createUser(String username, String password, String email, String type){
3764
User newUser = UserFactory.BirthUser(username, password, email, type);
38-
this.accountList.add(newUser);
3965
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
4066
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
41-
out.writeObject(this.accountList);
67+
out.writeObject(newUser);
68+
this.accountList.add(newUser);
4269
out.close();
4370
fileOut.close();
4471
}catch(Exception e){
@@ -50,7 +77,6 @@ public void createUser(String username, String password, String email, String ty
5077
}
5178

5279
@Override
53-
// To be edited to get user from the array format rather than the serialized format.
5480
public User getUser(String username) {
5581
User ans = null;
5682
for (int i = 0; i < (this.getList().size()); i++) {
@@ -66,22 +92,19 @@ public User getUser(String username) {
6692
public List<User> getList() {
6793
List<User> users = new ArrayList<>();
6894
try(FileInputStream fileIn = new FileInputStream(accounts);
69-
ObjectInputStream in = new ObjectInputStream(fileIn)) {
70-
71-
users = (ArrayList<User>) in.readObject();
72-
/*
95+
ObjectInputStream in = new ObjectInputStream(fileIn)){
96+
/*User user = (User) in.readObject();*/
7397
while(true){
7498
try{
7599
User user = (User) in.readObject();
76100
users.add(user);}
77101
catch(EOFException e){
78102
break;
79-
}*/
80-
return users;
81-
}catch(EOFException e){
82-
return users;
83-
} catch (IOException | ClassNotFoundException ex) {
84-
throw new RuntimeException(ex);
103+
}
104+
}
105+
}catch(Exception e){
106+
e.printStackTrace();
85107
}
108+
return users;
86109
}
87110
}

src/main/java/UserExists.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
public interface UserExists {
2-
boolean UserExists(String username, String password);
2+
boolean UserExists(String username, String email);
3+
boolean UserExists(String username);
34
}

0 commit comments

Comments
 (0)