|
@ -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); |
|
|