|
@ -1,3 +1,7 @@ |
|
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
|
import java.io.FileWriter; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
public class SignUp { |
|
|
public class SignUp { |
|
|
private String name; |
|
|
private String name; |
|
|
private String password; |
|
|
private String password; |
|
@ -25,7 +29,7 @@ public class SignUp { |
|
|
this.password = password; |
|
|
this.password = password; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Function to create user |
|
|
|
|
|
|
|
|
// Function to create user with validation |
|
|
public static SignUp createUser(String name, String password) { |
|
|
public static SignUp createUser(String name, String password) { |
|
|
if (name == null || name.isEmpty()) { |
|
|
if (name == null || name.isEmpty()) { |
|
|
throw new IllegalArgumentException("Username cannot be empty"); |
|
|
throw new IllegalArgumentException("Username cannot be empty"); |
|
@ -38,12 +42,30 @@ public class SignUp { |
|
|
} return new SignUp(name, password); |
|
|
} 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) { |
|
|
public static void main(String[] args) { |
|
|
// Example |
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// Example usage |
|
|
SignUp user = createUser("Test User", "TestPasswort"); |
|
|
SignUp user = createUser("Test User", "TestPasswort"); |
|
|
|
|
|
|
|
|
// Example of accessing properties |
|
|
// Example of accessing properties |
|
|
System.out.println("User Name: " + user.getName()); |
|
|
System.out.println("User Name: " + user.getName()); |
|
|
System.out.println("User Password: " + user.getPassword()); |
|
|
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()); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |