From 1e69815d996d797f112d60d4e508a64c02e9b810 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 17:59:18 +0100 Subject: [PATCH] added functionality checking if a username already exists --- src/main/java/SignUpGUI.java | 18 ++++++++++++++++++ user.json | 30 ++++++------------------------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/SignUpGUI.java b/src/main/java/SignUpGUI.java index 6e3f306..b19cf8e 100644 --- a/src/main/java/SignUpGUI.java +++ b/src/main/java/SignUpGUI.java @@ -1,6 +1,7 @@ import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.List; import java.util.UUID; public class SignUpGUI extends JFrame implements ActionListener { @@ -64,6 +65,11 @@ public class SignUpGUI extends JFrame implements ActionListener { 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); @@ -75,6 +81,18 @@ public class SignUpGUI extends JFrame implements ActionListener { } } } + // 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 true; // Username is available + } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { diff --git a/user.json b/user.json index 01ececf..0201a67 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,14 @@ [ { - "id": "f9cd70c1-57cd-4943-a00b-0de521347aff", + "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", "userName": "asd", - "password": "test1234", - "birthday": "01.01.2000" - }, - { - "id": "69e4b1d3-3089-4f98-936f-865dfe40e6a0", - "userName": "asd", - "password": "test1234", - "birthday": "01.01.2013" - }, - { - "id": "4165bfe6-9851-480f-90e1-8229415fa44e", - "userName": "asdasdasd", - "password": "1231412512", - "birthday": "1111111" - }, - { - "id": "f2775f81-564b-4101-8e86-ce879a9e7737", - "userName": "asd", - "password": "asdasdasd", - "birthday": "asd" + "password": "123456", + "birthday": "1" }, { - "id": "1e035bce-37b5-4f1e-894e-8136b8021c4d", - "userName": "asd", + "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", + "userName": "asd1", "password": "123456", - "birthday": "1234" + "birthday": "1" } ] \ No newline at end of file