diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 51a3e47..696786b 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -36,7 +36,7 @@ public class SignUpGUI extends JFrame implements ActionListener { public SignUpGUI() { setTitle("Sign Up"); - setSize(400, 300); // Adjusted size for accommodating more fields + setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setLayout(null); @@ -64,7 +64,7 @@ public class SignUpGUI extends JFrame implements ActionListener { confirmPasswordField.setBounds(140, 80, 160, 25); add(confirmPasswordField); - JLabel firstNameLabel = new JLabel("First Name:"); // New field for first name + JLabel firstNameLabel = new JLabel("First Name:"); firstNameLabel.setBounds(20, 110, 80, 25); add(firstNameLabel); @@ -72,7 +72,7 @@ public class SignUpGUI extends JFrame implements ActionListener { firstNameField.setBounds(140, 110, 160, 25); add(firstNameField); - JLabel surnameLabel = new JLabel("Surname:"); // New field for surname + JLabel surnameLabel = new JLabel("Surname:"); surnameLabel.setBounds(20, 140, 80, 25); add(surnameLabel); @@ -89,7 +89,7 @@ public class SignUpGUI extends JFrame implements ActionListener { add(birthdayField); signUpButton = new JButton("Sign Up"); - signUpButton.setBounds(140, 210, 100, 25); // Adjusted position + signUpButton.setBounds(140, 210, 100, 25); signUpButton.addActionListener(this); add(signUpButton); } @@ -97,7 +97,7 @@ public class SignUpGUI extends JFrame implements ActionListener { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == signUpButton) { - String username = usernameField.getText(); + String username = usernameField.getText(); String password = passwordField.getText(); String confirmPassword = confirmPasswordField.getText(); String birthday = birthdayField.getText(); @@ -113,6 +113,7 @@ public class SignUpGUI extends JFrame implements ActionListener { 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); @@ -124,17 +125,18 @@ public class SignUpGUI extends JFrame implements ActionListener { } } } - // 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) { List userList = CreateUser.readUserListFromJsonFile(filename); if (userList != null) { for (CreateUser user : userList) { if (user.getUserName().equals(username)) { - return false; // Username already exists + return false; } } } - return true; // Username is available + return true; } public static void main(String[] args) { diff --git a/src/test/java/CreateUserTest.java b/src/test/java/CreateUserTest.java index f3e4c19..cc70da6 100644 --- a/src/test/java/CreateUserTest.java +++ b/src/test/java/CreateUserTest.java @@ -8,23 +8,22 @@ class CreateUserTest { @Test public void testCreateUser() { - String id = "1"; - String userName = "testUser"; - String password = "password123"; - String birthday = "2000-01-01"; - String firstName = "John"; - String surname = "Doe"; - - CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); - - assertEquals(id, user.getId()); - assertEquals(userName, user.getUserName()); - assertEquals(birthday, user.getBirthday()); - assertEquals(firstName, user.getFirstName()); - assertEquals(surname, user.surname()); - - // Password should be hashed - assertNotEquals(password, user.getPassword()); + String id = "1"; + String userName = "testUser"; + String password = "password123"; + String birthday = "2000-01-01"; + String firstName = "John"; + String surname = "Doe"; + + CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); + + assertEquals(id, user.getId()); + assertEquals(userName, user.getUserName()); + assertEquals(birthday, user.getBirthday()); + assertEquals(firstName, user.getFirstName()); + assertEquals(surname, user.surname()); + + assertNotEquals(password, user.getPassword()); } @@ -38,16 +37,12 @@ class CreateUserTest { String firstName = "Jane"; String surname = "Doe"; - // Create a user CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); - // Save user to JSON file user.saveToJsonFile(filename); - // Read users from JSON file List userList = CreateUser.readUserListFromJsonFile(filename); - // Ensure that the user has been saved and can be read correctly assertEquals(1, userList.size()); CreateUser savedUser = userList.get(0); assertEquals(id, savedUser.getId()); @@ -56,37 +51,30 @@ class CreateUserTest { assertEquals(firstName, savedUser.getFirstName()); assertEquals(surname, savedUser.surname()); - // Clean up - delete the test file after the test File file = new File(filename); file.delete(); } @Test public void testUpdateStayLoggedIn() { - String filename = "test_users.json"; - String id = "3"; - String userName = "testUser3"; - String password = "password789"; - String birthday = "2002-01-01"; - String firstName = "Jack"; - String surname = "Doe"; + String filename = "test_users.json"; + String id = "3"; + String userName = "testUser3"; + String password = "password789"; + String birthday = "2002-01-01"; + String firstName = "Jack"; + String surname = "Doe"; - // Create a user CreateUser user = new CreateUser(id, userName, password, birthday, firstName, surname); - // Save user to JSON file user.saveToJsonFile(filename); - // Update stayLoggedIn flag CreateUser.updateStayLoggedIn(filename, userName, true); - // Read updated user from JSON file List userList = CreateUser.readUserListFromJsonFile(filename); - // Ensure that the stayLoggedIn flag is updated assertTrue(userList.get(0).isStayLoggedIn()); - // Clean up - delete the test file after the test File file = new File(filename); file.delete(); }