diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index a05d9ca..1c4ef8f 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -9,7 +9,11 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class LoginGUI extends JFrame implements ActionListener { - private JTextField usernameField; + /** + * + */ + private static final long serialVersionUID = 1L; + private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; private JCheckBox stayLoggedInCheckbox; @@ -78,7 +82,6 @@ public class LoginGUI extends JFrame implements ActionListener { stayLoggedInCheckbox.addActionListener(e -> { boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); String username = usernameField.getText(); - // Set stayLoggedIn to false if the checkbox is unchecked if (!stayLoggedInCheckbox.isSelected()) { stayLoggedIn = false; } diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 696786b..70aa72f 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -96,35 +96,55 @@ public class SignUpGUI extends JFrame implements ActionListener { @Override 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 (!isUsernameAvailable("user.json", username)) { - JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE); - 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 (e.getSource() == signUpButton) { + signUpUser(); + } + } + + 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; + } + + 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 private boolean isUsernameAvailable(String filename, String username) { diff --git a/user.json b/user.json index 0637a08..cfc325c 100644 --- a/user.json +++ b/user.json @@ -1 +1,20 @@ -[] \ No newline at end of file +[ + { + "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 + } +] \ No newline at end of file