From 95deb5c64073c9138ca9e13a664edf336af02158 Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Thu, 8 Feb 2024 10:33:44 +0100 Subject: [PATCH] Added a method that changes the stayLoggedIn variable in the JSON file --- src/main/java/CreateUser.java | 24 +++++++++++++++++++++++- user.json | 14 ++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main/java/CreateUser.java b/src/main/java/CreateUser.java index 6bccbd9..c799654 100644 --- a/src/main/java/CreateUser.java +++ b/src/main/java/CreateUser.java @@ -110,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()); @@ -126,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/user.json b/user.json index 1f77562..893012b 100644 --- a/user.json +++ b/user.json @@ -12,5 +12,19 @@ "password": "123456", "birthday": "01.01.2000", "stayLoggedIn": false + }, + { + "id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false + }, + { + "id": "503f2d6a-389c-4675-8044-3ec9ca73f1b5", + "userName": "Test User", + "password": "TestPasswort123", + "birthday": "01.01.2000", + "stayLoggedIn": false } ] \ No newline at end of file