-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
223 lines (188 loc) · 8.97 KB
/
main.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
/**
* Created By: Tanishka Ghosh
* Password Maker and Checker
* Created using Swing GUI and Java
* Started on: 2024-01-09
* Finished on: 2024-01-10
* Description: A simple password maker and checker application.
* User can put in the requirements of their password. The default value is 0.
* Once the requirements are put in, the user can either make their own password
* and check if it passes the requirements, or they can request the program to
* create a password that meets the requirements for them.
*/
import javax.swing.*;
public class Main {
public static void main(String[] args) throws NumberFormatException {
//setting up the frame
JFrame frame = new JFrame();
frame.setSize(800, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//asking for the password criteria
JLabel title = new JLabel("Password Checker and Generator!",
SwingConstants.CENTER);
title.setBounds(10, 20, 780, 30);
frame.add(title);
JLabel capital = new JLabel("Minimum capital letters required?");
capital.setBounds(50, 60, 350, 20);
frame.add(capital);
JTextField capitalCount = new JTextField("0");
capitalCount.setBounds(400, 60, 350, 20);
frame.add(capitalCount);
JLabel lower = new JLabel("Minimum lowercase letters required?");
lower.setBounds(50, 90, 350, 20);
frame.add(lower);
JTextField lowerCount = new JTextField("0");
lowerCount.setBounds(400, 90, 350, 20);
frame.add(lowerCount);
JLabel special = new JLabel("Minimum special characters required?");
special.setBounds(50, 120, 350, 20);
frame.add(special);
JTextField specialCount = new JTextField("0");
specialCount.setBounds(400, 120, 350, 20);
frame.add(specialCount);
JLabel number = new JLabel("Minimum numbers required?");
number.setBounds(50, 150, 350, 20);
frame.add(number);
JTextField numberCount = new JTextField("0");
numberCount.setBounds(400, 150, 350, 20);
frame.add(numberCount);
JLabel length = new JLabel("Minimum total characters required?");
length.setBounds(50, 180, 350, 20);
frame.add(length);
JTextField lengthCount = new JTextField("0");
lengthCount.setBounds(400, 180, 350, 20);
frame.add(lengthCount);
JLabel choice = new JLabel("What would you like to do?",
SwingConstants.CENTER);
choice.setBounds(10, 220, 780, 40);
frame.add(choice);
//place for the user to enter their password, if they're checking validity
JTextField password = new JTextField();
password.setBounds(100, 300, 600, 40);
frame.add(password);
//creating and placing buttons to ask what the user wants to do
JButton passwordCheck = new JButton(
"Enter password above and click here to check your password");
passwordCheck.setBounds(100, 350, 600, 50);
frame.add(passwordCheck);
JButton passwordMake = new JButton(
"Click here to generate a password");
passwordMake.setBounds(100, 400, 600, 50);
frame.add(passwordMake);
//Message on inccorect user entry
JLabel error = new JLabel("Opps! You seemed to have entered something " +
"that wasn't a number...Try again (Make sure to put 0 instead of " +
"leaving an empty box!)",
SwingConstants.CENTER);
error.setBounds(20, 500, 760, 30);
error.setVisible(false);
frame.add(error);
//message on password success
JLabel success = new JLabel("Good Job! Your password passes.",
SwingConstants.CENTER);
success.setBounds(100, 500, 600, 30);
success.setVisible(false);
frame.add(success);
//message on password fail
JLabel fail = new JLabel("Uh oh....Try again.",
SwingConstants.CENTER);
fail.setBounds(100, 500, 600, 30);
JLabel failMessage = new JLabel("",
SwingConstants.CENTER);
failMessage.setBounds(100, 550, 600, 30);
fail.setVisible(false);
failMessage.setVisible(false);
frame.add(fail);
frame.add(failMessage);
//generated password
JTextField generatedPassword = new JTextField("",
SwingConstants.CENTER);
generatedPassword.setBounds(100, 500, 600, 30);
generatedPassword.setVisible(false);
frame.add(generatedPassword);
//actions to perform if passwordCheck is clicked: checks the password
passwordCheck.addActionListener(e -> {
int capCount = 0, lowCount = 0, speCount = 0, numCount = 0, lenCount = 0;
try {
capCount = Integer.parseInt(capitalCount.getText());
lowCount = Integer.parseInt(lowerCount.getText());
speCount = Integer.parseInt(specialCount.getText());
numCount = Integer.parseInt(numberCount.getText());
lenCount = Integer.parseInt(lengthCount.getText());
String passwordText = password.getText();
for (char i : passwordText.toCharArray()){
if ((i >= 33 && i <= 47) || (i >= 58 && i <= 64) ||
(i >= 91 && i <= 96) || i >= 123 && i <= 126){
speCount = (speCount != 0)? speCount - 1 : 0;
} else if (i >= 48 && i <= 57){
numCount = (numCount != 0)? numCount - 1 : 0;
} else if (i >= 65 && i <= 90){
capCount = (capCount != 0)? capCount - 1 : 0;
} else if (i >= 97 && i <= 122) {
lowCount = (lowCount != 0)? lowCount - 1 : 0;
}
lenCount = (lenCount != 0)? lenCount - 1 : 0;
}
//reset the visibility of the success/fail messages
if (speCount + numCount + capCount + lowCount + lenCount == 0){
generatedPassword.setVisible(false);
fail.setVisible(false);
failMessage.setVisible(false);
error.setVisible(false);
success.setVisible(true);
} else {
failMessage.setText("You need to add:- capital: " + capCount +
" lowercase: " + lowCount + " special character: " + speCount
+ " numbers: " + numCount + " characters: " + lenCount);
generatedPassword.setVisible(false);
success.setVisible(false);
error.setVisible(false);
fail.setVisible(true);
failMessage.setVisible(true);
}
} catch (Exception NumberFormatException){
error.setVisible(true);
}
});
//actions to perform if passwordMake is clicked: make a password
passwordMake.addActionListener(e -> {
int capCount = 0, lowCount = 0, speCount = 0, numCount = 0, lenCount = 0;
try {
capCount = Integer.parseInt(capitalCount.getText());
lowCount = Integer.parseInt(lowerCount.getText());
speCount = Integer.parseInt(specialCount.getText());
numCount = Integer.parseInt(numberCount.getText());
lenCount = Integer.parseInt(lengthCount.getText());
String passwordText = "";
while (capCount + lowCount + speCount + numCount + lenCount != 0) {
char c = (char) (33 + Math.round(Math.random() * 89));
passwordText += c;
if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) ||
(c >= 91 && c <= 96) || c >= 123 && c <= 126){
speCount = (speCount != 0)? speCount - 1 : 0;
} else if (c >= 48 && c <= 57){
numCount = (numCount != 0)? numCount - 1 : 0;
} else if (c >= 65 && c <= 90){
capCount = (capCount != 0)? capCount - 1 : 0;
} else if (c >= 97 && c <= 122) {
lowCount = (lowCount != 0)? lowCount - 1 : 0;
}
lenCount = (lenCount != 0)? lenCount - 1 : 0;
}
generatedPassword.setText(passwordText);
//set the visibility of the password messages
success.setVisible(false);
fail.setVisible(false);
failMessage.setVisible(false);
error.setVisible(false);
generatedPassword.setVisible(true);
} catch (Exception NumberFormatException){
System.out.println("HERE!");
error.setVisible(true);
}
});
//making frame visible
frame.setLayout(null);
frame.setVisible(true);
}
}