Browse Source

added functionality checking if a username already exists

remotes/origin/feature/client/make-input-field-buttons-interactive
Richard Schmidt 11 months ago
parent
commit
1e69815d99
  1. 18
      src/main/java/SignUpGUI.java
  2. 30
      user.json

18
src/main/java/SignUpGUI.java

@ -1,6 +1,7 @@
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.List;
import java.util.UUID; import java.util.UUID;
public class SignUpGUI extends JFrame implements ActionListener { 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); JOptionPane.showMessageDialog(this, "Passwords do not match!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
return; return;
} }
if (!isUsernameAvailable("user.json", username)) {
JOptionPane.showMessageDialog(this, "Username already exists!", "Sign Up Error", JOptionPane.ERROR_MESSAGE);
return;
}
try { try {
UUID randomUUID = UUID.randomUUID(); UUID randomUUID = UUID.randomUUID();
CreateUser user = CreateUser.createUser(randomUUID.toString(), username, password, birthday); 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<CreateUser> 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) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {

30
user.json

@ -1,32 +1,14 @@
[ [
{ {
"id": "f9cd70c1-57cd-4943-a00b-0de521347aff",
"id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1",
"userName": "asd", "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", "password": "123456",
"birthday": "1234"
"birthday": "1"
} }
] ]
Loading…
Cancel
Save