forked from Harshil1823/Learning-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
252 lines (233 loc) · 7.04 KB
/
User.java
File metadata and controls
252 lines (233 loc) · 7.04 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import java.util.ArrayList;
/**
* @author JavaDoc
* Represents a user.
*/
public class User {
private String firstname;
private String lastname;
private String email;
private String phonenumber;
private boolean isAuthor;
private String userID;
private String username;
private String password;
private ArrayList<Course> grades;
public UserList users;
public ArrayList<Course> courses;
public ArrayList<Course> createdCourses;
/**
* Constructor to initialize user.
* @param firstname String first name.
* @param lastname String last name.
* @param email String user email.
* @param phonenumber String user phone number.
* @param username String of username.
* @param password String of password.
* @param userID String of user ID.
* @param isAuthor String "yes" or "no" to determine if author.
*/
public User(String firstname, String lastname, String email, String phonenumber, String username,
String password, String userID, String isAuthor) {
if (validUser(firstname, lastname, email, phonenumber, username, password, userID, isAuthor)) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.phonenumber = phonenumber;
this.username = username;
this.password = password;
this.isAuthor = validAuthor(isAuthor);
this.userID = userID;
this.courses = new ArrayList<Course>();
this.createdCourses = new ArrayList<Course>();
users = UserList.getInstance();
users.addUser(this);
System.out.println("User created");
}
}
/**
* add a course to a user's list of courses by calling the
* addCourse method on a User object and passing in a Course object.
* @param course
*/
public void addCourse(Course course) {
courses.add(course);
}
//authhor will have created course list.
public void addCreatedCourse(Course course){
createdCourses.add(course);
}
/**
* Determines if a user is valid.
* @param firstname String first name.
* @param lastname String last name.
* @param email String user email.
* @param phonenumber String user phone number.
* @param username String of username.
* @param password String of password.
* @param userID String of user ID.
* @param isAuthor String "yes" or "no" to determine if author.
* @return true if valid, false otherwise.
*/
public boolean validUser(String firstname, String lastname, String email, String phonenumber, String username,
String password, String userID, String isAuthor) {
if (validName(username) && validName(lastname) && validEmail(email) && validPhoneNumber(phonenumber)
&& validUsername(username) && validPassword(password) && validUserID(userID)) {
return true;
}
return false;
}
/**
* Returns first name of user.
* @return String of first name.
*/
public String getFirstName() {
if (username == null) {
return "";
}
return firstname;
}
/**
* Returns last name of user.
* @return String of last name.
*/
public String getLastName() {
if (username == null) {
return "";
}
return lastname;
}
/**
* Returns email of user.
* @return String of email.
*/
public String getEmail() {
return email;
}
/**
* Returns user phone number.
* @return String of phone number.
*/
public String getPhoneNumber() {
return phonenumber;
}
/**
* Returns the user password.
* @return String of password.
*/
public String getPassword(){
return password;
}
/**
* Returns if author or not.
* @return true if author, false otherwise.
*/
public boolean isAuthor() {
return isAuthor;
}
/**
* Returns username.
* @return String of username.
*/
public String getUserName() {
if (username == null) {
return "";
}
return username;
}
/**
* Returns user ID.
* @return String of user id.
*/
public String getUserID() {
return userID;
}
/**
* Checks if name is valid.
* @param name String name from user.
* @return true if valid, false otherwise.
*/
private boolean validName(String name) {
return name != null;
}
/**
* Checks if username is valid.
* @param userName String of username.
* @return true if valid, false otherwise.
*/
private boolean validUsername(String userName) {
return userName != null && userName.length() >= 6;
}
/**
* Checks if phone number is valid.
* @param phonenumber String of phone number.
* @return true if valid, false otherwise.
*/
private boolean validPhoneNumber(String phonenumber) {
return phonenumber != null && phonenumber.length() == 10;
}
/**
* Checks if valid email.
* @param email String of email.
* @return true if valid, false otherwise.
*/
private boolean validEmail(String email) {
return email != null && email.contains("@");
}
/**
* Checks if valid password.
* @param password String of password.
* @return true if valid, false otherwise.
*/
private boolean validPassword(String password) {
return password.length() >= 8 && containsNumber(password) && containsSpecialCharacter(password);
}
/**
* Checks if valid user ID.
* @param userID String of user ID.
* @return true if valid, false otherwise.
*/
private boolean validUserID(String userID) {
return userID != null && !userID.isEmpty();
}
/**
* Checks if author inputs is valid.
* @param isAuthor String of "yes" or "no"
* @return true if yes, false otherwise.
*/
private boolean validAuthor(String isAuthor) {
if (isAuthor.equalsIgnoreCase("yes"))
return true;
return false;
}
/**
* Checks if contains number.
* @param password String of password.
* @return true if contains number, false otherwise.
*/
private boolean containsNumber(String password) {
return password.matches(".*\\d.*");
}
/**
* Checks if contains special character.
* @param password String of password.
* @return true if contains special character, false otherwise.
*/
private boolean containsSpecialCharacter(String password) {
return password.matches(".*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?].*");
}
/**
* accesing this in the UI, and that's why it's public
* @return all the courses that user is taking
*/
public ArrayList<Course> getCourses() {
return courses;
}
/**
*
* @returns createdCourses
*/
public ArrayList<Course> getCreatedCourses(){
return createdCourses;
}
}