diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 8e8f15f..c799654 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) { @@ -101,12 +110,32 @@ class CreateUser { } return userList; } + + // Function to update stayLoggedIn variable in the JSON file + public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { + List userList = readUserListFromJsonFile(filename); + + for (CreateUser user : userList) { + if (user.getUserName().equals(username)) { + user.setStayLoggedIn(stayLoggedIn); + break; + } + } + + try (FileWriter fileWriter = new FileWriter(filename)) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(userList, fileWriter); + System.out.println("StayLoggedIn updated in " + filename); + } catch (IOException e) { + System.out.println("Error occurred while updating StayLoggedIn in JSON file: " + e.getMessage()); + } + } public static void main(String[] args) { try { // Example usage UUID randomUUID = UUID.randomUUID(); - CreateUser user = createUser(randomUUID.toString(), "Another Test User", "TestPasswort123", "01.01.2000"); + CreateUser user = createUser(randomUUID.toString(), "Test User", "TestPasswort123", "01.01.2000"); // Example of accessing properties System.out.println("UserID: " + user.getId()); @@ -117,6 +146,8 @@ class CreateUser { // Save user information to a JSON file user.saveToJsonFile("user.json"); + + updateStayLoggedIn("user.json", "Test User", true); } catch (IllegalArgumentException e) { System.out.println("Error: " + e.getMessage()); } diff --git a/src/main/java/LoginGUI.java b/src/main/java/LoginGUI.java new file mode 100644 index 0000000..675ffa5 --- /dev/null +++ b/src/main/java/LoginGUI.java @@ -0,0 +1,122 @@ +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.List; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class LoginGUI extends JFrame implements ActionListener { + private JTextField usernameField; + private JPasswordField passwordField; + private JButton loginButton; + private JCheckBox stayLoggedInCheckbox; + + public LoginGUI() { + setTitle("Login"); + setSize(300, 180); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(null); + + JLabel usernameLabel = new JLabel("Username:"); + usernameLabel.setBounds(20, 20, 80, 25); + add(usernameLabel); + + usernameField = new JTextField(); + usernameField.setBounds(100, 20, 160, 25); + add(usernameField); + + JLabel passwordLabel = new JLabel("Password:"); + passwordLabel.setBounds(20, 50, 80, 25); + add(passwordLabel); + + passwordField = new JPasswordField(); + passwordField.setBounds(100, 50, 160, 25); + add(passwordField); + + stayLoggedInCheckbox = new JCheckBox("Stay Logged In"); + stayLoggedInCheckbox.setBounds(20, 80, 150, 25); + add(stayLoggedInCheckbox); + + loginButton = new JButton("Login"); + loginButton.setBounds(100, 110, 100, 25); + loginButton.addActionListener(this); + add(loginButton); + + getRootPane().setDefaultButton(loginButton); + + passwordField.addKeyListener(new EnterKeyListener()); + + stayLoggedInCheckbox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); + String username = usernameField.getText(); + updateStayLoggedIn(username, stayLoggedIn); + } + }); + + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == loginButton) { + login(); + } + } + + 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!"); + // Perform actions after successful login + + dispose(); + } else { + JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); + } + } + + private void updateStayLoggedIn(String username, boolean stayLoggedIn) { + // Update stayLoggedIn in the JSON file for the user + CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); + } + + // 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"); + if (userList != null) { + for (CreateUser user : userList) { + if (user.getUserName().equals(username) && user.getPassword().equals(password)) { + return true; //Success + } + } + } + return false; // Fail + } + + + private class EnterKeyListener implements KeyListener { + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KeyEvent.VK_ENTER) { + login(); + } + } + + @Override + public void keyReleased(KeyEvent e) {} + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + LoginGUI loginGUI = new LoginGUI(); + loginGUI.setVisible(true); + }); + } +} \ No newline at end of file diff --git a/user.json b/user.json index 0201a67..5437c96 100644 --- a/user.json +++ b/user.json @@ -1,14 +1,23 @@ [ { - "id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", - "userName": "asd", - "password": "123456", - "birthday": "1" + "id": "a2864d79-1079-4cbb-8d77-f5f84995580d", + "userName": "Another Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false }, { - "id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", - "userName": "asd1", + "id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", + "userName": "TestUser2", "password": "123456", - "birthday": "1" + "birthday": "01.01.2000", + "stayLoggedIn": false + }, + { + "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", + "userName": "Test User", + "password": "Test", + "birthday": "01.01.2000", + "stayLoggedIn": true } ] \ No newline at end of file