From cec86a85d870e0a978d3ecf6007ef88625885c72 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Wed, 7 Feb 2024 17:25:23 +0100 Subject: [PATCH] Added a boolean value along getter and setter methods to CreateUser that later checks if the user wants to stay logged in --- src/main/java/CreateUser.java | 9 +++++++++ src/main/java/LoginGUI.java | 8 ++++++++ user.json | 32 ++++++++------------------------ 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 8e8f15f..6bccbd9 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -17,6 +17,7 @@ class CreateUser { private String userName; private String password; private String birthday; + private boolean stayLoggedIn; // Constructor @@ -59,6 +60,14 @@ class CreateUser { public void setBirthday(String birthday) { this.birthday = birthday; } + + public boolean isStayLoggedIn() { + return stayLoggedIn; + } + + public void setStayLoggedIn(boolean stayLoggedIn) { + this.stayLoggedIn = stayLoggedIn; + } // Function to create user with validation public static CreateUser createUser(String id, String userName, String password, String birthday) { diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java index 696e3a5..b273014 100644 --- a/src/main/java/LoginGUI.java +++ b/src/main/java/LoginGUI.java @@ -44,6 +44,8 @@ public class LoginGUI extends JFrame implements ActionListener { getRootPane().setDefaultButton(loginButton); + passwordField.addKeyListener(new EnterKeyListener()); + } @Override @@ -56,6 +58,7 @@ public class LoginGUI extends JFrame implements ActionListener { private void login() { String username = usernameField.getText(); String password = new String(passwordField.getPassword()); + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state if (authenticateUser(username, password)) { JOptionPane.showMessageDialog(this, "Login successful!"); @@ -67,6 +70,11 @@ public class LoginGUI extends JFrame implements ActionListener { } } + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + // Update stayLoggedIn in the JSON file for the user + + } + // Function to authenticate the user by comparing the entered username and password with the saved user data private boolean authenticateUser(String username, String password) { List userList = CreateUser.readUserListFromJsonFile("user.json"); diff --git a/user.json b/user.json index 78ef422..1f77562 100644 --- a/user.json +++ b/user.json @@ -1,32 +1,16 @@ [ { - "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", - "userName": "asd", - "password": "123456", - "birthday": "1" - }, - { - "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", - "userName": "asd1", - "password": "123456", - "birthday": "1" - }, - { - "id": "b54391dc-c06b-496c-828a-501f9a182cf9", - "userName": "new user", - "password": "123456", - "birthday": "1" - }, - { - "id": "6db2ea00-246d-4604-bef2-0875c3cb550e", + "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", "userName": "Another Test User", "password": "TestPasswort123", - "birthday": "01.01.2000" + "birthday": "01.01.2000", + "stayLoggedIn": false }, { - "id": "864695a6-8037-41e5-9f0e-4b92744b2113", - "userName": "Another Test User", - "password": "TestPasswort123", - "birthday": "01.01.2000" + "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", + "userName": "TestUser2", + "password": "123456", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file