-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
464 lines (342 loc) · 13.3 KB
/
Account.java
File metadata and controls
464 lines (342 loc) · 13.3 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import java.security.SecureRandom;
import java.util.Scanner;
/**
* Class Account models the account setup for a new user.
* <p>
* First name and last name are passed during construction and are used to create email and user names.
* <p>
* Terminal interaction is required to select the new users department from a displayed array of options.
* <p>
* A random password is generated and assigned to the user.
* <p>
* Defaults have been preassigned for company and mail box capacity.
*
*/
public class Account {
//declare attributes
private String firstName;
private String lastName;
private String department;
private String company = "ACME Corp.";
private String userName;
private String password;
private int mailBoxCapacity = 1000;
private String domainName = "ACME";
private String topDomain = "com";
private String email;
private String alternativeEmail;
private String[] possibleDepartments = {"Accounting", "Customer Service", "Production", "Sales", "Warehouse"};
private int defaultPasswordLength = 12;
//constructors
//default constructor
public Account(){
firstName = "";
lastName = "";
department = "";
userName = "";
password = "";
alternativeEmail = "";
//write to terminal account creation values
displayAccountDetails();
}
/**
* Account creates a user account object,
* <p>
* Pass the name to the constructor and user the terminal to select the new users department.
* @param nFirstName String containing first name.
* @param nLastName String containing last name.
*/
//firstName, lastName param constructer
public Account(String nFirstName, String nLastName){
firstName = nFirstName;
lastName = nLastName;
department = "";
userName = "";
password = "";
alternativeEmail = "";
//ask user for department entry
setDepartment(askForDepartment());
//set password
setPassword(randomPasswordGen(defaultPasswordLength));
//set email
setEmail(buildEmailAddress(firstName, lastName, domainName, topDomain));
//set userName
setUserName(buildUserName(getFirstName(), getLastName()));
//write to terminal account creation values
displayAccountDetails();
}
//all param constructer for future use.
public Account(String nFirstName, String nLastName, String nDepartment, String nCompany, String nUserName, String nPassword, int nMailBoxCapacity, String nAlternativeEmail){
firstName = nFirstName;
lastName = nLastName;
department = nDepartment;
company = nCompany;
userName = nUserName;
password = nPassword;
mailBoxCapacity = nMailBoxCapacity;
alternativeEmail = nAlternativeEmail;
//write to terminal account creation values
displayAccountDetails();
}
//setters - mutators
/**
* setFirstName accepts a String for the first name.
* @param nFirstName The first name.
*/
public void setFirstName(String nFirstName){
firstName = nFirstName;
} //end setFirstName
/**
* setLastName accepts a String for the last name.
* @param nLastName The last name.
*/
public void setLastName(String nLastName){
lastName = nLastName;
} //end setLastName
private void setDepartment(String nDepartment){
department = nDepartment;
} //end setDepartment
private void setCompany(String nCompany){
company = nCompany;
} //end setCompany
private void setUserName(String nUserName){
userName = nUserName;
} //end setUserName
private void setPassword(String nPassword){
password = nPassword;
}
/**
* setMailBoxCapacity accepts an int value to use as the max mail capacity.
* @param nMailBoxCapacity int value of the max mb of mail capacity.
*/
public void setMailBoxCapacity(int nMailBoxCapacity){
mailBoxCapacity = nMailBoxCapacity;
} //end setMailBoxCapacity
private void setDomainName(String nDomainName){
domainName = nDomainName;
} //end setDomainName
private void setTopDomain(String nTopDomain){
topDomain = nTopDomain;
} //end setTopDomain
private void setEmail(String nEmail){
email = nEmail;
} //end setEmail
/**
* setAlternativeEmail allows the user to have a secondary email on record.
* @param nAlternativeEmail String value of an alt email address.
*/
public void setAlternativeEmail(String nAlternativeEmail){
alternativeEmail = nAlternativeEmail;
} //end setAlternativeEmail
/**
* setPossibleDepartments accepts an array of Strings containing all department options for a new user.
* <p>
* This value feeds the menu of possible choices that displays when a new user is setup,
* @param nPossibleDepartments String array of departments.
*/
public void setPossibleDepartments(String[] nPossibleDepartments){
possibleDepartments = nPossibleDepartments;
} //end setPossibleDepartments
//getters - accessors
/**
* getFirstName returns the first name as a String.
* @return The first name.
*/
public String getFirstName(){
return firstName;
} //end getFirstName
/**
* getLastName returns the last name as a String.
* @return The last name.
*/
public String getLastName(){
return lastName;
} //end getLastName
/**
* getDepartment returns the department as a String.
* @return The department name selected.
*/
public String getDepartment(){
return department;
} //end getDepartment
/**
* getCompany returns the default company value as a String.
* @return The company name.
*/
public String getCompany(){
return company;
} //end getCompany
/**
* getUserName returns the assigned user name.
* <p>
* User name format is first inital last name.
* @return The user name.
*/
public String getUserName(){
return userName;
} //end getUserName
/**
* getPassword returns the value of the password as a String.
* @return The assigned password.
*/
public String getPassword(){
return password;
} //end getPassword
/**
* getMailBoxCapacity returns the currently set mail box capacity as an int.
* <p>
* Capacity is defined in units of MB.
* @return The mail box capaity.
*/
public int getMailBoxCapacity(){
return mailBoxCapacity;
} //end getMailBoxCapacity
/**
* getDomainName returns the default domain name.
* @return The domain name.
*/
public String getDomainName(){
return domainName;
} //end getDomainName
/**
* getTopDomain returns the top level domain.
* @return The top domain.
*/
public String getTopDomain(){
return topDomain;
}
/**
* getEmail returns the email address created for the user.
* <p>
* email address is returned as a string of FirstName.LastName.@Domain.TopDomain.
* @return The email addresss.
*/
public String getEmail(){
return email;
} //end getEmail
/**
* getAlternativeEmail returns a saved alternative email addresss if available.
* @return The alternative email.
*/
public String getAlternativeEmail(){
return alternativeEmail;
} //end getAlternativeEmail
/**
* getPossibleDepartments returns an array of String values contianing all of the currently set options for department.
* <p>
* The departments are held as full department name in a String.
* @return Array of departments.
*/
public String[] getPossibleDepartments(){
return possibleDepartments;
}
//other functions
/**
* displayAccountDetails prints to terminal the currently set account attributes.
*/
public void displayAccountDetails(){
System.out.println("=======================================");
System.out.println("Account Details");
System.out.println("=======================================");
System.out.println("First Name: " + getFirstName());
System.out.println("Last Name: " + getLastName());
System.out.println("Department: " + getDepartment());
System.out.println("Company: " + getCompany());
System.out.println("User Name: " + getUserName());
System.out.println("Password: " + getPassword());
System.out.println("Mail Box Capacity: " + getMailBoxCapacity());
System.out.println("Email: " + getEmail());
System.out.println("Alternative Email: " + getAlternativeEmail());
System.out.print("\n");
} //end displayAccountDetails
//check department
private String askForDepartment(){
//declare variables
String newDepartment = "";
String[] departmentList = getPossibleDepartments();
Scanner keyboardIn = new Scanner(System.in);
//write to terminal
System.out.println("Please select department from values below:");
//iterate through the possible departments array
for(int index = 0; index < departmentList.length; index++){
//display department
System.out.println("Option " + (index + 1) + ": " + departmentList[index]);
}
//Accept input from user for department
String departmentChoice = keyboardIn.nextLine();
//check that input is int and a number in range of choices
//control verification loop with error count flag.
int errorCount = 0;
do{
//check that department choice is numeric
if(isInteger(departmentChoice)){
//check that department choice is in range of the array
int intDepartmentChoice = Integer.parseInt(departmentChoice);
if(intDepartmentChoice < 1 || intDepartmentChoice > (departmentList.length)){
System.out.println(departmentChoice + " is not an accpetable entry.");
System.out.println("Please enter a value between 1 and " + (departmentList.length));
//take input again
departmentChoice = keyboardIn.nextLine();
//flag as an error to ensure a second pass of input verification.
errorCount = 1;
}
else{
//input was an int and is in range
//set value of choice to return in newDepartment
newDepartment = departmentList[(intDepartmentChoice - 1)];
//set errorCount to zero to allow loop to end
errorCount = 0;
//close scanner
keyboardIn.close();
}
}
else{
//input was not numeric
System.out.println(departmentChoice + " is not an accpetable entry.");
System.out.println("Please enter a value between 1 and " + (departmentList.length));
//take input again
departmentChoice = keyboardIn.nextLine();
//flag as an error to ensure a second pass of input verification.
errorCount = 1;
}
}while(errorCount > 0);
return newDepartment;
} //end askForDepartment
//Create a random password
private String randomPasswordGen(int nPasswordLength){
//String of possible password characters
String passwordSet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%";
//array of characters the length of the password length param
char[] password = new char[nPasswordLength];
//random number generator using SecureRandom
SecureRandom randomNumber = new SecureRandom();
//loop through the characters and select a random character for each position
for(int index = 0; index < password.length; index++){
//gen random index postion
int randomIndexPos = randomNumber.nextInt(passwordSet.length());
//randomly select a character for each postion of the password
password[index] = passwordSet.charAt(randomIndexPos);
}
return new String(password);
} //end randomPasswordGen
//build an email address
private String buildEmailAddress(String nFirstName, String nLastName, String nDomainName, String nTopDomain){
String newEmail = nFirstName + "." + nLastName + "@" + nDomainName + "." + nTopDomain;
return newEmail;
} //end buildEmailAddress
//build user name first intial lastname
private String buildUserName(String nFirstName, String nLastName){
String newUserName = nFirstName.substring(0, 1) + nLastName;
return newUserName;
} //end buildUserName
//check that a string is an integer - return true if it is - else return false
private boolean isInteger(String input){
try{
Integer.parseInt( input );
return true;
}
catch( Exception e ){
return false;
}
} //end isInteger
} //end class Account