Browse Source

changed it so the user data in user.json is appended and not overwritten

remotes/origin/feature/client/make-input-field-buttons-interactive
Richard Schmidt 11 months ago
parent
commit
6df8acae09
  1. 37
      src/main/java/SignUp.java
  2. 27
      user.json

37
src/main/java/SignUp.java

@ -1,9 +1,18 @@
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.io.*;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
public class SignUp {
class SignUp {
private String id; private String id;
private String userName; private String userName;
private String password; private String password;
@ -65,20 +74,38 @@ public class SignUp {
// Function to save to JSON file, replace with database call later // Function to save to JSON file, replace with database call later
public void saveToJsonFile(String filename) { public void saveToJsonFile(String filename) {
List<SignUp> userList = readUserListFromJsonFile(filename);
userList.add(this);
try (FileWriter fileWriter = new FileWriter(filename)) { try (FileWriter fileWriter = new FileWriter(filename)) {
Gson gson = new Gson();
gson.toJson(this, fileWriter);
System.out.println("User information saved to " + filename);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(userList, fileWriter);
System.out.println("User information appended to " + filename);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage()); System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage());
} }
} }
// Function to read user information from a JSON file
public static List<SignUp> readUserListFromJsonFile(String filename) {
List<SignUp> userList = new ArrayList<>();
try (Reader reader = new FileReader(filename)) {
Type userListType = new TypeToken<List<SignUp>>() {}.getType();
Gson gson = new Gson();
userList = gson.fromJson(reader, userListType);
} catch (FileNotFoundException e) {
// File does not exist yet, so return an empty list
} catch (IOException e) {
System.out.println("Error occurred while reading user information from JSON file: " + e.getMessage());
}
return userList;
}
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();
SignUp user = createUser(randomUUID.toString(), "Test User", "TestPasswort", "01.01.1900");
SignUp user = createUser(randomUUID.toString(), "Another 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());

27
user.json

@ -1 +1,26 @@
{"id":"48728bbb-b924-48ce-aac0-3e38d6c7878e","userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"}
[
{
"id": "48728bbb-b924-48ce-aac0-3e38d6c7878e",
"userName": "Test User",
"password": "TestPasswort",
"birthday": "01.01.1900"
},
{
"id": "bacd2bf9-9486-4cc3-92fe-9a24b5cf47a4",
"userName": "Test User",
"password": "TestPasswort",
"birthday": "01.01.1900"
},
{
"id": "15474da8-c2cd-4c1c-ba57-cd1b8657140e",
"userName": "Test User",
"password": "TestPasswort",
"birthday": "01.01.1900"
},
{
"id": "be83e49a-bb17-4be2-a50f-0b846d7de3ea",
"userName": "Another Test User",
"password": "TestPasswort123",
"birthday": "01.01.2000"
}
]
Loading…
Cancel
Save