-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserList.java
136 lines (133 loc) · 3.57 KB
/
UserList.java
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
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
/**
* this is the user list
* @author: J TEA: Tessa Neal, Eve Blom, Anna Phan, and Jacqueline Askey
*/
public class UserList {
private ArrayList<User> users;
private static UserList userList;
/**
* Gets the users from the JSON
*/
private UserList() {
users = DataLoader.getUsers();
}
/**
* Creates a new user list
* @return the new user list
*/
public static UserList getInstance() {
if (userList == null) {
userList = new UserList();
}
return userList;
}
/**
* Checks if this user exist by looking for username
* @param userName this is the user's username
* @return wheather the username is already used
*/
public boolean haveUser(String userName) {
for (User user : users) {
if (user.getUsername().equals(userName)) {
return true;
}
}
return false;
}
/**
* Checks if this user exist by looking for username and password
* @param userName User's Username
* @param password User's Password
* @return wheather the username and password is already used
*/
public boolean haveUser(String userName, String password) {
for (User user : users) {
if (user.getUsername().equals(userName) && user.getPassword().equals(password)) {
return true;
}
}
return false;
}
/**
* Adding a new user
* @param firstName user's first name
* @param lastName user's last name
* @param email user' email
* @param birthday user's birthday
* @param username user's username
* @param password user's password
* @param type type of account the user is wanting
* @return wheather the user is added or not
*/
public boolean addUser(String firstName, String lastName, String email, Date birthday, String username,
String password, AccountType type) {
if (haveUser(username))
return false;
users.add(new User(firstName, lastName, email, birthday, username, password, type));
saveUsers();
return true;
}
/**
* gets the user with id number
* @param id user's id number
* @return the user
*/
public User getUser(UUID id) {
for (User user : users) {
if (user.getId().equals(id)) {
return user;
}
}
return null;
}
/**
* gets the user with username
* @param userName user's username
* @return the user
*/
public User getUser(String userName) {
for (User user : users) {
if (user.getUsername().equals(userName)) {
return user;
}
}
return null;
}
/**
* list of users
* @return users
*/
public ArrayList<User> getUsers() {
return users;
}
/**
* saves the users and writes them to the user json
*/
public void saveUsers() {
DataWriter.saveUsers();
}
/**
* get the size of the user list
* @return the size of the user list
*/
public int size() {
return users.size();
}
/**
* find the user with username
* @param id user's id
* @return the user
*/
public String findUsername(UUID id) {
User temp = null;
for (User user : users) {
if (user.getId().equals(id)) {
temp = user;
}
}
return temp.username;
}
}