|
@ -27,70 +27,70 @@ class CreateUser { |
|
|
|
|
|
|
|
|
// Constructor |
|
|
// Constructor |
|
|
public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { |
|
|
public CreateUser(String id, String name, String password, String birthday, String firstName, String surname) { |
|
|
this.id = id; |
|
|
|
|
|
|
|
|
this.id = id; |
|
|
this.userName = name; |
|
|
this.userName = name; |
|
|
this.password = hashPassword(password); |
|
|
|
|
|
this.birthday = birthday; |
|
|
|
|
|
this.firstName = firstName; |
|
|
|
|
|
this.surname = surname; |
|
|
|
|
|
|
|
|
this.password = hashPassword(password); |
|
|
|
|
|
this.birthday = birthday; |
|
|
|
|
|
this.firstName = firstName; |
|
|
|
|
|
this.surname = surname; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Getters and Setters |
|
|
// Getters and Setters |
|
|
public String getId() { |
|
|
public String getId() { |
|
|
return id; |
|
|
return id; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setId(String id) { |
|
|
|
|
|
|
|
|
public void setId(String id) { |
|
|
this.id = id; |
|
|
this.id = id; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String getUserName() { |
|
|
public String getUserName() { |
|
|
return userName; |
|
|
|
|
|
|
|
|
return userName; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void setUserName(String userName) { |
|
|
public void setUserName(String userName) { |
|
|
this.userName = userName; |
|
|
|
|
|
|
|
|
this.userName = userName; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public String getPassword() { |
|
|
public String getPassword() { |
|
|
return password; |
|
|
|
|
|
|
|
|
return password; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void setPassword(String password) { |
|
|
public void setPassword(String password) { |
|
|
this.password = password; |
|
|
|
|
|
|
|
|
this.password = password; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public String getBirthday() { |
|
|
public String getBirthday() { |
|
|
return birthday; |
|
|
return birthday; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void setBirthday(String birthday) { |
|
|
|
|
|
|
|
|
public void setBirthday(String birthday) { |
|
|
this.birthday = birthday; |
|
|
this.birthday = birthday; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String getFirstName() { |
|
|
|
|
|
return firstName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String getFirstName() { |
|
|
|
|
|
return firstName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setFirstName(String firstName) { |
|
|
|
|
|
this.firstName = firstName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setFirstName(String firstName) { |
|
|
|
|
|
this.firstName = firstName; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String surname() { |
|
|
|
|
|
return surname; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public String surname() { |
|
|
|
|
|
return surname; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setSurname(String surname) { |
|
|
|
|
|
this.surname = surname; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setSurname(String surname) { |
|
|
|
|
|
this.surname = surname; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public boolean isStayLoggedIn() { |
|
|
|
|
|
|
|
|
public boolean isStayLoggedIn() { |
|
|
return stayLoggedIn; |
|
|
return stayLoggedIn; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public void setStayLoggedIn(boolean stayLoggedIn) { |
|
|
|
|
|
|
|
|
public void setStayLoggedIn(boolean stayLoggedIn) { |
|
|
this.stayLoggedIn = stayLoggedIn; |
|
|
this.stayLoggedIn = stayLoggedIn; |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// Function to create user with validation |
|
|
// Function to create user with validation |
|
|
public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { |
|
|
public static CreateUser createUser(String id, String userName, String password, String birthday, String firstName, String surname) { |
|
@ -105,7 +105,7 @@ class CreateUser { |
|
|
} return new CreateUser(id, userName, password, birthday, firstName, surname); |
|
|
} return new CreateUser(id, userName, password, birthday, firstName, surname); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Function to hash the password using SHA-256 algorithm |
|
|
|
|
|
|
|
|
// Function to hash the password using SHA-256 algorithm |
|
|
private String hashPassword(String password) { |
|
|
private String hashPassword(String password) { |
|
|
try { |
|
|
try { |
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
|
|
MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
|
@ -123,7 +123,7 @@ class CreateUser { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 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<CreateUser> userList = readUserListFromJsonFile(filename); |
|
|
List<CreateUser> userList = readUserListFromJsonFile(filename); |
|
|
userList.add(this); |
|
|
userList.add(this); |
|
@ -133,11 +133,11 @@ class CreateUser { |
|
|
gson.toJson(userList, fileWriter); |
|
|
gson.toJson(userList, fileWriter); |
|
|
System.out.println("User information appended to " + filename); |
|
|
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 |
|
|
|
|
|
|
|
|
// Function to read user information from a JSON file |
|
|
public static List<CreateUser> readUserListFromJsonFile(String filename) { |
|
|
public static List<CreateUser> readUserListFromJsonFile(String filename) { |
|
|
List<CreateUser> userList = new ArrayList<>(); |
|
|
List<CreateUser> userList = new ArrayList<>(); |
|
|
try (Reader reader = new FileReader(filename)) { |
|
|
try (Reader reader = new FileReader(filename)) { |
|
@ -152,7 +152,7 @@ class CreateUser { |
|
|
return userList; |
|
|
return userList; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Function to update stayLoggedIn variable in the JSON file |
|
|
|
|
|
|
|
|
// Function to update stayLoggedIn variable in the JSON file |
|
|
public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { |
|
|
public static void updateStayLoggedIn(String filename, String username, boolean stayLoggedIn) { |
|
|
List<CreateUser> userList = readUserListFromJsonFile(filename); |
|
|
List<CreateUser> userList = readUserListFromJsonFile(filename); |
|
|
|
|
|
|
|
|