-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasswordModel.java
94 lines (73 loc) · 1.72 KB
/
PasswordModel.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
import java.util.Random;
// the model class
public class PasswordModel{
// attributes of the model
private int size;
private boolean caps;
private boolean specChar;
private boolean nums;
private char[] cRay;
// strings to iterate throught to make random password
private static String alpha = "abcdefghijklmnopqrstuvwxyz";
private static String numbers = "0123456789";
private static String alphaCaps = "ABCDEFGHIJKLMONPQRSTUVWXYZ";
private static String specials = "!@#$%&";
// default constructor
public PasswordModel(){
size = 8;
caps = false;
specChar = false;
nums = false;
cRay = new char[ size ];
}
// * setters and getters for attributes of the PasswordModel object
public void setSize( int size ){
this.size = size;
}
public int getSize(){
return size;
}
public void setCaps( boolean caps ){
this.caps = caps;
}
public boolean getCaps(){
return caps;
}
public void setNums( boolean nums ){
this.nums = nums;
}
public boolean getNums(){
return nums;
}
public void setSpec( boolean specChar ){
this.specChar = specChar;
}
public boolean getSpec(){
return specChar;
} // end setters and getters
// generates a random password based on size, caps, special chars and numbers
public String generate(){
String temp = "";
temp = alpha;
Random rnd = new Random();
cRay = new char[ size ];
if( caps == true ){
temp += alphaCaps;
}
if( nums == true ){
temp += numbers;
}
if( specChar == true ){
temp += specials;
}
for( int i = 0; i < getSize(); i++){
cRay[ i ] = temp.charAt( rnd.nextInt( temp.length() ) );
}
String x = new String( cRay );
return x;
}
// to String
public String toString(){
return generate();
}
}