-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
312 lines (233 loc) · 9.24 KB
/
User.java
File metadata and controls
312 lines (233 loc) · 9.24 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package teamprojectsavecare;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* @author seanr
*/
public class User {
protected String UserId;
protected String userFirstName;
protected String userLastName;
protected String userPPSNumber;
protected String userEmail;
protected String userPassword;
protected String userType;
final private String url;
protected String userTableId;
protected Connection myConn;
public User() {
url = "jdbc:sqlite:c:/mydb/saveCare.db";
}
public User(String UserId, String userFirstName, String userLastName, String userPPSNumber, String userEmail, String userPassword, String userType) {
this.UserId = UserId;
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.userPPSNumber = userPPSNumber;
this.userEmail = userEmail;
this.userPassword = userPassword;
this.userType = userType;
url = "jdbc:sqlite:c:/mydb/saveCare.db";
}
public String getUserTableId() {
return userTableId;
}
public void setUserTableId(String userTableId) {
this.userTableId = userTableId;
}
public String getUserId() {
return UserId;
}
public void setUserId(String UserId) {
this.UserId = UserId;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getUserFirstName() {
return userFirstName;
}
public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}
public String getUserLastName() {
return userLastName;
}
public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}
public String getUserPPSNumber() {
return userPPSNumber;
}
public void setUserPPSNumber(String userPPSNumber) {
this.userPPSNumber = userPPSNumber;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Connection getMyConn() {
return myConn;
}
public void setMyConn(Connection myConn) {
this.myConn = myConn;
}
public boolean signIn(String email, String password) {
try {
//This method is used sign the user into the application
//the email and password entered are passed to the method
//the email,password and User type is retrieved.
//if the password and the email match each other then the user is signed into the application by the returned boolean value
Statement stmt = myConn.createStatement();
ResultSet rs = stmt.executeQuery(
"select UserEmail, UserType, UserPassword from Users where UserEmail='"
+ email + "';");
// while loop to iterate through all records of customer table and display them
// in the text field
while (rs.next()) {
if (email.equals(rs.getString(1)) && password.equals(rs.getString(3))) {
userType = rs.getString(2);
return true;
}
if (!email.equals(rs.getString(1)) || !password.equals(rs.getString(3))) {
userType = rs.getString(2);
return false;
}
}
return false;
} catch (SQLException ex) {
System.out.println(ex);
} catch (Exception ex) {
System.out.println(ex);
}
return false;
}
// this methd is used to get a Java DataBase Connectivity connection to the MySQL Database.
//Driver Manager is the library that manages the JDBC drivers
//JDBC url is entered to detemine the port number to connect to
//the roo is the username of the database
// the satabasepassword2 is used connect to the database
public Connection getConnection() {
try {
myConn = DriverManager.getConnection(url);
return myConn;
} catch (SQLException ex) {
System.out.println(ex);
} catch (Exception ex) {
System.out.println(ex);
}
return null;
}
//Regex pattern is used to ensure the format entered phone number format is a valid number
public boolean validatePhoneNumber(String number) {
if (!number.matches("^0(83|85|86|87|88|89)\\s?\\d{1}\\s?\\d{1}\\s?\\d{1}\\s?\\d{1}\\s?\\d{1}\\s?\\d{1}\\s?\\d{1}$")) {
return false;
}
return true;
}
//https://regex101.com/library/rP6sA9
//The below method is used to validate an input email
//regex was used to validat the email
public boolean validateEmail(String email) {
String regex = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";
if (!email.matches(regex)) {
return false;
}
return true;
}
//Below is amethod to validate a password entered to ensure that it is at least 8 characters
//Also must have a number, special caracter and a captital letter.
//Below is the link where the regex pattern was found
// https://stackoverflow.com/questions/38118869/password-regex-issue
public boolean validatePassword(String createPassword) {
String regex = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$";
if (!createPassword.matches(regex)) {
return false;
}
return true;
}
//The method below validates and eircode using regex.
//The email must consist of a valid email format.
//https://help.salesforce.com/s/articleView?id=000390158&type=1
public boolean validateEircode(String eircode) {
if (!eircode.matches("^[A-Za-z]\\d{2}\\s?[A-Za-z\\d]{4}$")) {
return false;
}
return true;
}
//The below method vvalidates a pps number using regex.
//https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html
public boolean ppsNumberValidation(String ppsNumber) {
String regex = "\\d{7}[A-Z]{1,2}";
if (!ppsNumber.matches(regex)) {
return false;
}
return true;
}
//This method is used to check if entered email is an existing
//validation to ensure when a user is creating account they can register and aready existing email
//email is a unique value of the database thus why this validation was needed
public boolean validateExistingEmail(String email) {
try {
Statement stmt = myConn.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT UserEmail FROM Users WHERE UserEmail='" + email + "';");
if (rs.next()) {
//rs.getString(1);
if (email.equals(rs.getString(1))) {
return false;
}
}
return true;
} catch (SQLException ex) {
System.out.println(ex);
} catch (Exception ex) {
System.out.println(ex);
}
return true;
}
public boolean validateExistingPPSNumber(String ppsNumber) {
//this method is used to ensure that a pps number ixisting in the databse is not added to the databse
//The pps is a unique value thus why ths validation is also needed.
try {
Statement stmt = myConn.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT UserPPSNumber FROM Users WHERE UserPPSNumber='" + ppsNumber + "';");
if (rs.next()) {
//rs.getString(1);
if (ppsNumber.equals(rs.getString(1))) {
return false;
}
}
return true;
} catch (SQLException ex) {
System.out.println(ex);
} catch (Exception ex) {
System.out.println(ex);
}
return true;
}
@Override
public String toString() {
return "User{" + "UserId=" + UserId + ", userFirstName=" + userFirstName + ", userLastName=" + userLastName + ", userPPSNumber=" + userPPSNumber + ", userEmail=" + userEmail + ", userPassword=" + userPassword + ", userType=" + userType + ", url=" + url + ", myConn=" + myConn + '}';
}
}