diff --git a/pom.xml b/pom.xml
index 05188ae..21bd5b6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,4 +3,14 @@
org.progmethoden
java-chat
0.0.1-SNAPSHOT
+
+
+
+
+ com.google.code.gson
+ gson
+ 2.10.1
+
+
+
\ No newline at end of file
diff --git a/src/main/java/SignUp.java b/src/main/java/SignUp.java
index 1eefbfd..ca443dd 100644
--- a/src/main/java/SignUp.java
+++ b/src/main/java/SignUp.java
@@ -1,3 +1,7 @@
+import com.google.gson.Gson;
+import java.io.FileWriter;
+import java.io.IOException;
+
public class SignUp {
private String name;
private String password;
@@ -25,25 +29,43 @@ public class SignUp {
this.password = password;
}
- // Function to create user
+ // Function to create user with validation
public static SignUp createUser(String name, String password) {
if (name == null || name.isEmpty()) {
- throw new IllegalArgumentException("Username cannot be empty");
+ throw new IllegalArgumentException("Username cannot be empty");
}
if (password == null || password.isEmpty()) {
- throw new IllegalArgumentException("Password cannot be empty");
+ throw new IllegalArgumentException("Password cannot be empty");
}
if (password.length() < 6) {
- throw new IllegalArgumentException("Password must be at least 6 characters long");
+ throw new IllegalArgumentException("Password must be at least 6 characters long");
} return new SignUp(name, password);
}
+
+ // Function to save to JSON file, replace with database call later
+ public void saveToJsonFile(String filename) {
+ try (FileWriter fileWriter = new FileWriter(filename)) {
+ Gson gson = new Gson();
+ gson.toJson(this, fileWriter);
+ System.out.println("User information saved to " + filename);
+ } catch (IOException e) {
+ System.out.println("Error occurred while saving user information to JSON file: " + e.getMessage());
+ }
+ }
public static void main(String[] args) {
- // Example
- SignUp user = createUser("TestUser", "TestPasswort");
+ try {
+ // Example usage
+ SignUp user = createUser("Test User", "TestPasswort");
- // Example of accessing properties
- System.out.println("User Name: " + user.getName());
- System.out.println("User Password: " + user.getPassword());
+ // Example of accessing properties
+ System.out.println("User Name: " + user.getName());
+ System.out.println("User Password: " + user.getPassword());
+
+ // Save user information to a JSON file
+ user.saveToJsonFile("user.json");
+ } catch (IllegalArgumentException e) {
+ System.out.println("Error: " + e.getMessage());
+ }
}
}
\ No newline at end of file