From 6df8acae097428e33e2b319bc0db7ff72281b30d Mon Sep 17 00:00:00 2001 From: Richard Schmidt Date: Tue, 6 Feb 2024 15:15:42 +0100 Subject: [PATCH] changed it so the user data in user.json is appended and not overwritten --- src/main/java/SignUp.java | 37 ++++++++++++++++++++++++++++++++----- user.json | 27 ++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java index 79e32a8..964375b 100644 --- a/src/main/java/SignUp.java +++ b/src/main/java/SignUp.java @@ -1,9 +1,18 @@ 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.IOException; + import java.util.UUID; -public class SignUp { +class SignUp { private String id; private String userName; private String password; @@ -65,20 +74,38 @@ public class SignUp { // Function to save to JSON file, replace with database call later public void saveToJsonFile(String filename) { + List userList = readUserListFromJsonFile(filename); + userList.add(this); + 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) { 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 readUserListFromJsonFile(String filename) { + List userList = new ArrayList<>(); + try (Reader reader = new FileReader(filename)) { + Type userListType = new TypeToken>() {}.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) { try { // Example usage 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 System.out.println("UserID: " + user.getId()); diff --git a/user.json b/user.json index 5232e18..84627ce 100644 --- a/user.json +++ b/user.json @@ -1 +1,26 @@ -{"id":"48728bbb-b924-48ce-aac0-3e38d6c7878e","userName":"Test User","password":"TestPasswort","birthday":"01.01.1900"} \ No newline at end of file +[ + { + "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" + } +] \ No newline at end of file