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

@ -111,11 +111,31 @@ class CreateUser {
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) {
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());
}

14
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
}
]
Loading…
Cancel
Save