-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
204 lines (191 loc) · 6.34 KB
/
Copy pathUser.java
File metadata and controls
204 lines (191 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.util.ArrayList;
import java.io.*;
import java.util.Arrays;
/**
* Team Project -- User
*
* This file handles the user data for each user.
* For more in depth documentation see Docs/UserDataStorage.md
*
* @author William Boulton, Mukund Venkatesh, Alan Yi, Jai Menon, Kush Kodiya
*
* @version November 1, 2024
*
*/
public class User implements UserInt, Serializable {
private String username;
private String password;
private String firstName;
private String lastName;
private ArrayList<String> friends;
private ArrayList<String> blockedUsers;
public static final Object LOCK = new Object();
//profile picture
private byte[] profilePicture;
private boolean allowAll;
//line format: username|password|firstName|lastName|friends|blockedUsers|profilePicture|allowAll
public User(String line) {
String[] info = line.split("\\|");
username = info[0];
password = info[1];
firstName = info[2];
lastName = info[3];
this.friends = new ArrayList<String>();
this.blockedUsers = new ArrayList<String>();
//add friends
String[] friendGroup = info[4].split(",");
for (String friend : friendGroup) {
if (friend != null) {
this.friends.add(friend);
}
}
//add blocked users
String[] blockedPeople = info[5].split(",");
for (String blockedUser : blockedPeople) {
if (blockedUser != null) {
this.blockedUsers.add(blockedUser);
}
}
if (!info[6].contains(",")) {
this.profilePicture = null;
} else {
String[] byteValues = info[6].substring(1, info[6].length() - 1).split(",");
profilePicture = new byte[byteValues.length];
for (int i = 0; i < byteValues.length; i++) {
profilePicture[i] = Byte.parseByte(byteValues[i].trim());
}
}
allowAll = Boolean.parseBoolean(info[7]);
}
// you probably want a constructor which can take in a csv line from the database and make a user based on that
public User(String username, String password, String firstName, String lastName, byte[] pfp) {
//username rules - no commas, doesn't already exist, not empty
//if userDatabase is null, create a new userDatabase
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.friends = new ArrayList<String>();
this.blockedUsers = new ArrayList<String>();
//if there is no comma in the profile, there is no profile picture
if (pfp == null || pfp.length < 1) {
this.profilePicture = null;
} else {
this.profilePicture = pfp;
}
// allow all is set to true by default
allowAll = true;
}
public boolean addFriend(String user) {
//if user is not blocked, add to friends
synchronized (LOCK) {
if (!blockedUsers.contains(user)) {
friends.add(user);
return true;
}
return false;
}
}
public boolean removeFriend(String user) {
synchronized (LOCK) {
return friends.remove(user);
}
}
public boolean blockUser(String user) {
//if user doesnt exist at all, return false
synchronized (LOCK) {
blockedUsers.add(user);
//if user is a friend, remove from friends
friends.remove(user);
return true;
}
}
public boolean unblockUser(String user) {
//if user doesnt exist in blocked users or in the user database, return false
synchronized (LOCK) {
if (!blockedUsers.contains(user)) {
return false;
}
blockedUsers.remove(user);
return true;
}
}
public String getUsername() {
return username;
}
public boolean checkPassword(String inputPassword) {
return this.password.equals(inputPassword);
}
public boolean verifyLogin(String enteredPassword) {
return checkPassword(enteredPassword);
}
public boolean equals(User user) {
return this.username.equals(user.username);
}
public String toString() {
return String.format("%s|%s|%s|%s|%s|%s|%s|%b", username, password, firstName,
lastName, stringListToString(friends), stringListToString(blockedUsers),
Arrays.toString(profilePicture), allowAll);
}
public String stringListToString(ArrayList<String> list) {
//only take the usernames and put it in a list which is comma separated
StringBuilder sb = new StringBuilder();
for (String user : list) {
sb.append(user).append(",");
}
return sb.length() > 0 ? sb.substring(0, sb.length() - 1) : "";
}
public byte[] getProfilePicture() {
try {
return profilePicture;
} catch (Exception e) {
return null;
}
}
public String getPassword() {
return password;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public ArrayList<String> getFriends() {
return friends;
}
public void setFriends(ArrayList<String> friends) {
this.friends = friends;
}
public void setBlockedUsers(ArrayList<String> blockedUsers) {
this.blockedUsers = blockedUsers;
}
public ArrayList<String> getBlockedUsers() {
return blockedUsers;
}
public void changeUsername(String newUsername) {
this.username = newUsername;
}
public void changePassword(String newPassword) {
this.password = newPassword;
}
public boolean isAllowAll() {
return allowAll;
}
public void setAllowAll(boolean newBoolean) {
allowAll = newBoolean;
}
public void setProfilePicture(byte[] newProfilePicture) {
profilePicture = newProfilePicture;
}
public BufferedImage getProfilePictureImage() {
try {
return ImageIO.read(new ByteArrayInputStream(profilePicture));
} catch (Exception e) {
return null;
}
}
}