Browse Source

refactoring: actionPerformed in SignUpGUI

main
Richard Schmidt 11 months ago
parent
commit
60a1fe794c
  1. 7
      src/main/java/LoginGUI.java
  2. 70
      src/main/java/SignUpGUI.java
  3. 21
      user.json

7
src/main/java/LoginGUI.java

@ -9,7 +9,11 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
public class LoginGUI extends JFrame implements ActionListener { public class LoginGUI extends JFrame implements ActionListener {
private JTextField usernameField;
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField usernameField;
private JPasswordField passwordField; private JPasswordField passwordField;
private JButton loginButton; private JButton loginButton;
private JCheckBox stayLoggedInCheckbox; private JCheckBox stayLoggedInCheckbox;
@ -78,7 +82,6 @@ public class LoginGUI extends JFrame implements ActionListener {
stayLoggedInCheckbox.addActionListener(e -> { stayLoggedInCheckbox.addActionListener(e -> {
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); boolean stayLoggedIn = stayLoggedInCheckbox.isSelected();
String username = usernameField.getText(); String username = usernameField.getText();
// Set stayLoggedIn to false if the checkbox is unchecked
if (!stayLoggedInCheckbox.isSelected()) { if (!stayLoggedInCheckbox.isSelected()) {
stayLoggedIn = false; stayLoggedIn = false;
} }

70
src/main/java/SignUpGUI.java

@ -96,36 +96,56 @@ public class SignUpGUI extends JFrame implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == signUpButton) {
String username = usernameField.getText();
String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText();
String birthday = birthdayField.getText();
String firstName = firstNameField.getText();
String surname = surnameField.getText();
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
return;
}
if (e.getSource() == signUpButton) {
signUpUser();
}
}
if (!isUsernameAvailable("user.json", username)) {
JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
return;
}
private void signUpUser() {
String username = usernameField.getText();
String password = passwordField.getText();
String confirmPassword = confirmPasswordField.getText();
String birthday = birthdayField.getText();
String firstName = firstNameField.getText();
String surname = surnameField.getText();
if (!validatePasswordConfirmation(password, confirmPassword)) {
showErrorMessage("Passwords do not match!");
return;
}
try {
UUID randomUUID = UUID.randomUUID();
CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname);
user.saveToJsonFile("user.json");
JOptionPane.showMessageDialog(this, "User signed up successfully!");
dispose();
} catch (IllegalArgumentException ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Sign Up Error", JOptionPane.ERROR_MESSAGE);
}
if (!isUsernameAvailable("user.json", username)) {
showErrorMessage("Username already exists!");
return;
}
try {
createUserAndSave(username, password, birthday, firstName, surname);
showSuccessMessage("User signed up successfully!");
dispose();
} catch (IllegalArgumentException ex) {
showErrorMessage("Error: " + ex.getMessage());
} }
} }
private boolean validatePasswordConfirmation(String password, String confirmPassword) {
return password.equals(confirmPassword);
}
private void showErrorMessage(String message) {
JOptionPane.showMessageDialog(this, message, "Sign Up Error", JOptionPane.ERROR_MESSAGE);
}
private void showSuccessMessage(String message) {
JOptionPane.showMessageDialog(this, message);
}
private void createUserAndSave(String username, String password, String birthday, String firstName, String surname) {
UUID randomUUID = UUID.randomUUID();
CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday, firstName, surname);
user.saveToJsonFile("user.json");
}
// Function to check if the input username doesn't already exist in the JSON file // Function to check if the input username doesn't already exist in the JSON file
private boolean isUsernameAvailable(String filename, String username) { private boolean isUsernameAvailable(String filename, String username) {
List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename); List<CreateUser> userList = CreateUser.readUserListFromJsonFile(filename);

21
user.json

@ -1 +1,20 @@
[]
[
{
"id": "e365e96f-6c6c-48e5-96e0-7b5e058e7198",
"userName": "existinguser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
},
{
"id": "61ef4208-9077-42c4-983f-f1e234f7e5f4",
"userName": "testUser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
}
]
Loading…
Cancel
Save