-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathForgotPasswordApp.java
73 lines (54 loc) · 2.38 KB
/
ForgotPasswordApp.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ForgotPasswordApp extends JFrame implements ActionListener {
private JLabel securityQuestionLabel;
private JTextField securityAnswerField;
private JButton retrievePasswordButton;
private User[] users;
public ForgotPasswordApp(User[] users) {
setTitle("Forgot Password");
setLayout(new BorderLayout(20, 20));
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
int randomIndex = (int) (Math.random() * users.length);
User user = users[randomIndex];
securityQuestionLabel = new JLabel("Security Question: " + user.getSecurityQuestion());
mainPanel.add(securityQuestionLabel, BorderLayout.NORTH);
securityAnswerField = new JTextField();
mainPanel.add(securityAnswerField, BorderLayout.CENTER);
retrievePasswordButton = new JButton("Retrieve Password");
retrievePasswordButton.addActionListener(this);
mainPanel.add(retrievePasswordButton, BorderLayout.SOUTH);
this.users = users;
add(mainPanel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == retrievePasswordButton) {
retrievePassword();
}
}
private void retrievePassword() {
String answer = securityAnswerField.getText();
for (User user : users) {
if (answer.equals(user.getSecurityAnswer())) {
JOptionPane.showMessageDialog(this, "Your password is: " + new String(user.getPassword()));
dispose();
return;
}
}
JOptionPane.showMessageDialog(this, "Incorrect answer to security question or user not found.");
}
public static void main(String[] args) {
User[] testUsers = { new User("testuser1", "testpassword1".toCharArray(), "What is your favorite color?", "Green"),
new User("testuser2", "testpassword2".toCharArray(), "What is your pet's name?", "Rex"),
new User("testuser3", "testpassword3".toCharArray(), "Where were you born?", "New York") };
SwingUtilities.invokeLater(() -> {
new ForgotPasswordApp(testUsers);
});
}
}