Browse Source

Added a method that changes the stayLoggedIn variable in the JSON file

remotes/origin/server
Richard Schmidt 11 months ago
parent
commit
95deb5c640
  1. 24
      src/main/java/CreateUser.java
  2. 14
      user.json

24
src/main/java/CreateUser.java

@ -110,12 +110,32 @@ class CreateUser {
} }
return userList; return userList;
} }
// Function to update stayLoggedIn variable in the JSON file
public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) {
List<CreateUser> 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) { public static void main(String[] args) {
try { try {
// Example usage // Example usage
UUID randomUUID = UUID.randomUUID(); 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 // Example of accessing properties
System.out.println("UserID: " + user.getId()); System.out.println("UserID: " + user.getId());
@ -126,6 +146,8 @@ class CreateUser {
// Save user information to a JSON file // Save user information to a JSON file
user.saveToJsonFile("user.json"); user.saveToJsonFile("user.json");
updateStayLoggedIn("user.json", "Test User", true);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage()); System.out.println("Error: " + e.getMessage());
} }

14
user.json

@ -12,5 +12,19 @@
"password": "123456", "password": "123456",
"birthday": "01.01.2000", "birthday": "01.01.2000",
"stayLoggedIn": false "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
} }
] ]
Loading…
Cancel
Save