Skip to content

Commit 7305239

Browse files
authored
Merge pull request #16 from CSC207-2022F-UofT/15-userdatabase-and-user
15 userdatabase and user
2 parents 233994e + a6560d9 commit 7305239

File tree

21 files changed

+261
-31
lines changed

21 files changed

+261
-31
lines changed

.gitignore

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
22
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
33

4-
# entities.user_entities.User-specific stuff
5-
#.idea/**/workspace.xml
6-
#.idea/**/tasks.xml
7-
#.idea/**/usage.statistics.xml
8-
#.idea/**/dictionaries
9-
#.idea/**/shelf
10-
11-
## AWS entities.user_entities.User-specific
12-
#.idea/**/aws.xml
4+
# entities.userEntities.User-specific stuff
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/**/usage.statistics.xml
8+
.idea/**/dictionaries
9+
.idea/**/shelf
10+
11+
# AWS entities.userEntities.User-specific
12+
.idea/**/aws.xml
1313

1414
# Generated files
1515
.idea/**/contentModel.xml

src/main/java/data_access/UserDatabase.java

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package data_access;
22

3-
import interface_adapters.IRetrieveList;
4-
import interface_adapters.user_registration_interface_adapters.UserExists;
5-
import entities.user_entities.*;
6-
import entities.user_entities.UserFactory;
7-
import use_cases.user_login_use_case.UserCreator;
8-
import interface_adapters.UserRetriever;
3+
import entities.userEntities.User;
4+
import entities.userEntities.UserFactory;
5+
import interface_adapters.Chat.ConvHistGateway;
6+
import interface_adapters.Chat.MsgSenderGateway;
7+
import interface_adapters.Chat.UserChatGateway;
8+
import interface_adapters.User.*;
99

1010
import java.io.*;
1111
import java.util.ArrayList;
1212
import java.util.List;
13-
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList {
13+
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList, UserModificationGateway,
14+
ConvHistGateway, MsgSenderGateway, UserChatGateway {
1415
File accounts;
1516
List<User> accountList;
1617
public UserDatabase(){
@@ -38,6 +39,16 @@ public boolean UserExists(String username, String email) {
3839
return false;
3940
}
4041

42+
@Override
43+
public boolean UserExists(String username) {
44+
for(User user: this.accountList){
45+
if(user.getUsername().equals(username)){
46+
return true;
47+
}
48+
}
49+
return false;
50+
}
51+
4152
// Creates a new user with a username and password, and an email address
4253
// The order is username, password, email address, verification status, status
4354
//
@@ -62,9 +73,9 @@ public void createUser(String username, String password, String email, String ty
6273
// To be edited to get user from the array format rather than the serialized format.
6374
public User getUser(String username) {
6475
User ans = null;
65-
for (int i = 0; i < (this.getList().size()); i++) {
66-
if (this.getList().get(i).getUsername().equals(username)) {
67-
ans = this.getList().get(i);
76+
for (int i = 0; i < (this.accountList.size()); i++) {
77+
if (this.accountList.get(i).getUsername().equals(username)) {
78+
ans = this.accountList.get(i);
6879
}
6980
}
7081
return ans;
@@ -78,11 +89,88 @@ public List<User> getList() {
7889
ObjectInputStream in = new ObjectInputStream(fileIn)) {
7990

8091
users = (ArrayList<User>) in.readObject();
92+
/*
93+
while(true){
94+
try{
95+
entities.userEntities.User user = (entities.userEntities.User) in.readObject();
96+
users.add(user);}
97+
catch(EOFException e){
98+
break;
99+
}*/
81100
return users;
82101
}catch(EOFException e){
83102
return users;
84103
} catch (IOException | ClassNotFoundException ex) {
85104
throw new RuntimeException(ex);
86105
}
87106
}
107+
108+
@Override
109+
public void modifyUser(String oldUsername, User modified){
110+
// swap in modified user to accountList
111+
this.accountList.remove(this.getUser(oldUsername));
112+
this.accountList.add(modified);
113+
114+
// overwrite the serialized file
115+
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
116+
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
117+
out.writeObject(this.accountList);
118+
out.close();
119+
fileOut.close();
120+
}catch(Exception e){
121+
System.out.println("Error");
122+
}
123+
}catch(Exception e){
124+
System.out.println("Error");
125+
}
126+
}
127+
128+
129+
// Below two methods are used by conversation history-related interactors
130+
// (Commented as objects are not found)
131+
// /**
132+
// * Pushes a new message to a chat's conversation history (in memory not persisting storage)
133+
// * @param dsRequestModel input data containing user ID, chat ID, message content
134+
// */
135+
// public void saveMessage(MsgSenderDsRequestModel dsRequestModel) {
136+
// String userID = dsRequestModel.getUserID();
137+
// String chatID = dsRequestModel.getChatID();
138+
// Message message = dsRequestModel.getMessage();
139+
//
140+
// // Find chat under specified entities.userEntities.User
141+
// Chat chat = this.getUser(userID).getChat(chatID);
142+
//
143+
// chat.addMessage(message);
144+
// }
145+
//
146+
// /**
147+
// * Gets a chat's conversation history (from memory not persisting storage)
148+
// * @param dsRequestModel input data containing user ID, chat ID
149+
// * @return a chat's conversation history
150+
// */
151+
// public ArrayList<Message> getConversationHistory(ConvHistDsRequestModel dsRequestModel) {
152+
// String userID = dsRequestModel.getUserID();
153+
// String chatID = dsRequestModel.getChatID();
154+
//
155+
// // Find chat under specified entities.userEntities.User
156+
// Chat chat = this.getUser(userID).getChat(chatID);
157+
//
158+
// return Chat.getConversationHistory();
159+
// }
160+
161+
162+
163+
164+
// This method will get a user's chats; this will be used by AppScreenLoader to display chats and could
165+
// also be used by ChatInteractor (Chat entity is undefined here and at the moment user doesn't have chats)
166+
// @Override
167+
// public ArrayList<Chat> getUserChats(String username) {
168+
// for (entities.userEntities.User user: accountList){
169+
// if (user.getUsername().equals(username)){
170+
// return user.getChats();
171+
// }
172+
// }
173+
// throw new RuntimeException("Invalid username: user does not exist");
174+
// }
175+
88176
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package entities.userEntities;
2+
3+
public class BasicUser extends User{
4+
public BasicUser(String Username, String Password, String Email){
5+
super(Username, Password, Email);
6+
}
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package entities.userEntities;
2+
3+
import interface_adapters.User.*;
4+
5+
import java.io.Serializable;
6+
import java.util.ArrayList;
7+
8+
public abstract class User implements Serializable, Changeable, UserAuthenticationI {
9+
protected String username;
10+
protected String password;
11+
protected String email;
12+
boolean verified = false;
13+
boolean online = false;
14+
// protected ArrayList<Chat> chats;
15+
public User(String username, String password, String email){
16+
this.username = username;
17+
this.password = password;
18+
this.email = email;
19+
// this.chats = new ArrayList<Chat>();
20+
}
21+
public String getEmail(){
22+
return this.email;
23+
}
24+
public String getUsername(){
25+
return this.username;
26+
}
27+
private String getPassword(){
28+
return this.password;
29+
}
30+
31+
// public ArrayList<Chat> getChats() {
32+
// return this.chats;
33+
// }
34+
35+
@Override
36+
public Boolean PasswordMatch(String attempt){
37+
return (this.getPassword().equals(attempt));
38+
}
39+
40+
@Override
41+
// from interface_adapters.User.Changeable
42+
public void changeFeature(String feature, String newFeature){
43+
if (feature == "Username"){
44+
this.username = newFeature;
45+
} else if (feature == "Password"){
46+
this.password = newFeature;
47+
} else if (feature == "Email"){
48+
this.email = newFeature;
49+
}
50+
}
51+
52+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package entities.userEntities;
2+
3+
import entities.userEntities.BasicUser;
4+
import entities.userEntities.User;
5+
6+
public class UserFactory {
7+
//Following the factory design pattern, just in case in the future we decide to add various different types of Users
8+
public static User BirthUser(String Username, String Password, String Email, String type){
9+
return new BasicUser(Username, Password, Email);
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interface_adapters.Chat;
2+
3+
public interface ConvHistGateway {
4+
// public ArrayList<Message> getConversationHistory(ConvHistDsRequestModel dsRequestModel);
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interface_adapters.Chat;
2+
3+
public interface MsgSenderGateway {
4+
// public void saveMessage(MsgSenderDsRequestModel dsRequestModel);
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package interface_adapters.Chat;
2+
3+
import java.util.ArrayList;
4+
5+
public interface UserChatGateway {
6+
7+
// ArrayList<Chat> getUserChats(String username);
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interface_adapters.User;
2+
3+
public interface Changeable {
4+
void changeFeature(String feature, String newFeature);
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package interface_adapters.User;
2+
3+
import entities.userEntities.User;
4+
5+
import java.util.List;
6+
7+
public interface IRetrieveList {
8+
List<User> getList();
9+
}

0 commit comments

Comments
 (0)