-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistration.java
244 lines (194 loc) · 7.31 KB
/
Registration.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
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
/*
* This file is the registration class for the Vehicle Rental System.
*
* TCSS 305 - Rentz
*/
package model;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.regex.Pattern;
import utility.FileLoader;
/**
* Represents User Sign-in Object.
*
* @author hphobbs
* @version Winter2020.
*/
public class Registration {
/**
* Scanner for the application.
*/
public static final Scanner SCANNER = new Scanner(System.in);
/**
* User Storage File.
*/
private static final String DEFAULT_USERFILE_NAME = "./resources/registeredusers.txt";
/**
* The registered user list for sign in.
*/
private final Map<String, User> myUserList;
/** The file of user names. */
private final String myUserfileName;
/**
* Constructs a sign in/registration system.
*/
public Registration() {
// load all the users presents in the file
this(DEFAULT_USERFILE_NAME);
}
public Registration(final String theUserfileName) {
this.myUserfileName = theUserfileName;
// Loads all the users presents in the file.
myUserList = FileLoader.readItemsFromFile(theUserfileName);
}
/**
* getter for myUserList.
*
* @return myUserList
*/
public Map<String, User> getMyUserList() {
return myUserList;
}
/**
* display sign-in or registration options.
*
* @return boolean true
*/
public boolean printSignin() {
boolean result = false;
final String printPassword = "Password:";
final String printUserName = "User Name:";
System.out.print("Enter 1 or 2 (1. New Registration 2. Login):");
final int numberInput = Integer.parseInt(SCANNER.nextLine());
//final int numberInput = Integer.parseInt(input.nextLine());
// nextInt(): reads integers entered
System.out.println("You entered option " + numberInput);
System.out.println();
System.out.println("********************** \n" + "Enter Details \n"
+ "**********************");
if (numberInput == 1) {
System.out.print(printUserName);
String userName = SCANNER.nextLine();
// Checks if there is user name input
while (myUserList.containsKey(userName)) {
System.out.print("User already exists, enter different user name:");
userName = SCANNER.nextLine();
}
System.out.print(printPassword);
String password = SCANNER.nextLine();
// checks for password input
boolean validPassword = validatingPassword(password, userName);
while (!validPassword) {
System.out.println("-----Password Does Not Comply----");
System.out.println("Password should not be username");
System.out.println("Password must contain: ");
System.out.println("at least 6 characters");
System.out.println("at least one digit");
System.out.println("at least one upper case character");
System.out.println("at least one lower case character");
System.out.println("at least one special character");
System.out.print("Enter a Valid Password:");
password = SCANNER.nextLine();
validPassword = validatingPassword(password, userName);
}
System.out.print("isVIP(true/false):");
final String vipStat = SCANNER.nextLine();
final boolean vipStatus;
if ("true".equals(vipStat)) {
vipStatus = true;
} else {
vipStatus = false;
}
final User newUser = new User(userName, password, vipStatus);
if (register(newUser)) {
System.out.println("Registration Successfull");
}
} else if (numberInput == 2) {
System.out.print(printUserName);
String userName = SCANNER.nextLine();
// Checks if there is user name input
System.out.print(printPassword);
String password = SCANNER.nextLine();
// checks for password input
while (!login(userName, password)) {
System.out.println();
System.out.println("Wrong Credentials");
System.out.print("Enter User Name:");
userName = SCANNER.nextLine();
// Checks if there is user name input
System.out.print("Enter Password");
password = SCANNER.nextLine();
// checks for password input
}
System.out.println("Login Successfull");
result = true;
}
return result;
}
/**
* Verify if the password meets the requirements.
*
* @param theUserName
* @param thePassword
* @return validPassword
*/
private boolean validatingPassword(final String thePassword, final String theUserName) {
boolean validPassword = false;
final int minLength = 6;
final String symbols = ".*[`~!@#$%^&*()\\-_=+\\\\|\\[{\\]};:'\",<.>/?].*";
if (thePassword.length() >= minLength
&& Pattern.compile(".*[a-z].*").matcher(thePassword).find()
&& Pattern.compile(".*[0-9].*").matcher(thePassword).find()
&& Pattern.compile(".*[A-Z].*").matcher(thePassword).find()
&& Pattern.compile(symbols).matcher(thePassword).find()
&& !(thePassword.contains(theUserName))) {
validPassword = true;
}
return validPassword;
}
/**
* Verify Sign-in procedure.
*
* @param theUsername user name for sign-in
* @param thePassword password for sign in
* @return sign-in success
*/
public boolean login(final String theUsername, final String thePassword) {
// check if they match and if
Objects.requireNonNull(theUsername);
Objects.requireNonNull(thePassword);
if (theUsername.isEmpty() || thePassword.isEmpty()) {
throw new IllegalArgumentException();
}
final User userInFile = myUserList.get(theUsername);
return userInFile != null && userInFile.getMyPassword().equals(thePassword);
}
/**
* Adds a user to the registered user list.
*
* @param theUser an order to add to this shopping cart
* @return true/false returns if registration is successful
*/
public boolean register(final User theUser) {
myUserList.put(theUser.getMyName(), theUser);
FileLoader.writeUserToFile(myUserfileName, theUser);
return true;
}
/**
* Empties the user list.
*/
public void clear() {
myUserList.clear();
}
public String getUserfileName() {
return myUserfileName;
}
@Override
/**
* @return the string representation of registration
*/
public String toString() {
return "Registered UserList" + myUserList.toString();
}
}