-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
359 lines (271 loc) · 9.92 KB
/
User.java
File metadata and controls
359 lines (271 loc) · 9.92 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import java.io.Serializable;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Vector;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private HashPassword password;
private Vector<Account> accounts;
private Account currentAcct;
public static void main(String[] args) {
User user = new User("testPassword1");
// should print true
System.out.println(user.checkPassword("testPassword1"));
// should print false
System.out.println(user.checkPassword("test"));
// change password and test, should print true
user.setPassword();
System.out.println(user.checkPassword("newPassword"));
// create new account
user.createAccount();
// set current account
user.setCurrentAcct();
// print account name
System.out.println(user.getAccountName());
// change name and print
user.setAccountName();
System.out.println(user.getAccountName());
// print account url
System.out.println(user.getAccountURL());
// change url and print
user.setAccountURL();
System.out.println(user.getAccountURL());
// print account password
System.out.println(user.getAccountPass());
// change account password and print
user.setAccountPass();
System.out.println(user.getAccountPass());
// generate account password and print
user.generateAccountPass();
System.out.println(user.getAccountPass());
// test checkAccountPassAge
user.checkAccountPassAge();
// test checkPassAge
user.checkPassAge();
// delete user
user.deleteAccount();
// check deletion worked
user.listAccounts();
} // end main
public User(String pass) {
this.password = new HashPassword(pass);
this.accounts = new Vector<Account>();
} // end constructor
public int getTotalAccounts() {
return this.accounts.size();
} // end getTotalAccounts
public void setPassword() {
boolean keepGoing = true;
while (keepGoing) {
char[] firstEntry = System.console().readPassword("What would you like the new password to be?\n");
while (firstEntry.length == 0) {
System.out.println("Error: password cannot be empty");
firstEntry = System.console().readPassword("\nWhat would you like the new password to be?\n");
} // end while
char[] secondEntry = System.console().readPassword("Retype new password:\n");
if (Arrays.equals(firstEntry, secondEntry)) {
String newPass = new String(firstEntry);
this.password.setPassword(newPass);
keepGoing = false;
} else {
System.out.println("Error: Passwords do not match\n");
} // end if else
} // end while
}// end setPassword
public boolean checkPassword(String pass) {
return this.password.checkPassword(pass);
} // end checkPassword
public void listAccounts() {
int length = this.accounts.size();
System.out.println("Accounts:\n");
for (int i = 0; i < length; i++) {
System.out.println(Integer.toString(i) + ") " + this.accounts.get(i).getName());
} // end for
System.out.println();
} // end listAccounts
public void setCurrentAcct() {
// select account
System.out.println("Which account would you like to view?\n");
int index = this.selectAccount();
// assign chosen account to currentAcct
this.currentAcct = this.accounts.get(index);
// check how old the password is
this.checkAccountPassAge();
} // end setCurrentAcct
public int selectAccount() {
this.listAccounts();
int length = this.accounts.size();
Vector<String> validResponses = new Vector<String>();
for (int i = 0; i < length; i++) {
validResponses.add(Integer.toString(i));
} // end for
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
int index = -1;
while (keepGoing) {
System.out.println("Enter a number to select an account");
String response = scanner.nextLine();
if (response.equals("-1")) {
this.listAccounts();
} else if (validResponses.contains(response)) {
// convert response to int
index = Integer.parseInt(response);
keepGoing = false;
} else {
System.out.println("Error: Invalid input\n");
} // end if else
} // end while
return index;
} // end selectAccount
public void setAccountName() {
Scanner scanner = new Scanner(System.in);
String name = "";
while (name.length() == 0) {
System.out.println("Current name: " + this.getAccountName());
System.out.println("Enter new name:");
name = scanner.nextLine();
if (name.equals("")) {
name = this.getAccountName();
} // end if
} // end while
this.currentAcct.setName(name);
} // end setAccountName
public void setAccountURL() {
Scanner scanner = new Scanner(System.in);
String url = "";
while (url.length() == 0) {
System.out.println("Current url: " + this.getAccountURL());
System.out.println("Enter new url:");
url = scanner.nextLine();
if (url.equals("")) {
url = this.getAccountURL();
} // end if
} // end while
this.currentAcct.setURL(url);
} // end setAccountURL
public void setAccountPass() {
Scanner scanner = new Scanner(System.in);
String pass = "";
while (pass.length() == 0) {
System.out.println("Current password: " + this.getAccountPass());
System.out.println("Enter new password:");
pass = scanner.nextLine();
} // end while
this.currentAcct.setPassword(pass);
} // end setAccountPass
public String getAccountName() {
return this.currentAcct.getName();
} // end getAccountName
public String getAccountURL() {
return this.currentAcct.getURL();
} // end getAccountURL
public String getAccountPass() {
return this.currentAcct.getPassword();
} // end getAccountPass
public void generateAccountPass() {
Scanner scanner = new Scanner(System.in);
String response = "";
int length = 0;
while (response.length() == 0) {
System.out.println("Enter the length of password you want: (12-50)");
response = scanner.nextLine();
try {
length = Integer.parseInt(response);
if (length < 12 || length > 50) {
System.out.println("\nError: Please enter a number between 12-50");
response = "";
} // end if
} catch (Exception e) {
System.out.println("\nError: Please enter an integer");
response = "";
} // end try catch
} // end while
this.currentAcct.generatePass(length);
} // end generateAccountPass
public void checkAccountPassAge() {
this.currentAcct.checkPassAge();
} // end checkAccountPassAge
public void generatePass() {
Scanner scanner = new Scanner(System.in);
String response = "";
int length = 0;
while (response.length() == 0) {
System.out.println("\nEnter the length of password you want: (12-50)");
response = scanner.nextLine();
try {
length = Integer.parseInt(response);
if (length < 12 || length > 50) {
System.out.println("\nError: Please enter a number between 12-50");
response = "";
} // end if
} catch (Exception e) {
System.out.println("\nError: Please enter an integer");
response = "";
} // end try catch
} // end while
this.password.generatePass(length);
} // end generatePass
public void createAccount() {
Scanner scanner = new Scanner(System.in);
String response = "";
// get account name from user
while (response.length() == 0) {
System.out.println("Enter name for the account:");
response = scanner.nextLine();
} // end while
String name = response;
// get account url
response = "";
while (response.length() == 0) {
System.out.println("\nEnter url for the account:");
response = scanner.nextLine();
} // end while
String url = response;
// get account password
response = "";
while (response.length() == 0) {
System.out.println("\nEnter password for the account:");
response = scanner.nextLine();
} // end while
String password = response;
Account newAcct = new Account(name, url, password);
this.accounts.add(newAcct);
} // end createAccount
public void deleteAccount() {
// select an account
System.out.println("Which account would you like to delete?");
int index = this.selectAccount();
Account selectedAcct = this.accounts.get(index);
Scanner scanner = new Scanner(System.in);
System.out.println("\nWarning:");
System.out.println("You are about to delete the following account:");
System.out.println("Account Name: " + selectedAcct.getName());
System.out.println("Account url: " + selectedAcct.getURL());
System.out.println("\nThis action cannot be undone");
System.out.println("Please enter the name of the account to confirm deletion");
System.out.println("To cancel press enter");
String response = scanner.nextLine();
if (response.equals(selectedAcct.getName())) {
this.accounts.remove(index);
System.out.println("\nAccount deleted");
} else {
System.out.println("\nDelete cancelled\n");
} // end if else
} // end deleteAccount
public void checkPassAge() {
Calendar dateSet = new GregorianCalendar();
dateSet.setTime(this.password.getDate());
Calendar today = new GregorianCalendar();
today.setTime(new Date());
int yearsDiff = today.get(Calendar.YEAR) - dateSet.get(Calendar.YEAR);
int monthsDiff = today.get(Calendar.MONTH) - dateSet.get(Calendar.MONTH);
long diffInMonths = yearsDiff * 12 + monthsDiff;
if (diffInMonths >= 6) {
System.out.println("The master password is over 6 months old");
System.out.println("It is recommended to change your password every 6 months");
} // end if
} // end checkPassAge
} // end class