update: use HashMap instead of ArrayList
parent
73ab6ee022
commit
eea5ccba6b
|
@ -21,8 +21,8 @@ public class EasyMail {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void userRegister(String firstname, String lastName, String username, int year, int day, String monthName,char[] password, char[] passwordConfirmation) throws Exception {
|
public void userRegister(String firstname, String lastName, String email, int year, int day, String monthName,char[] password, char[] passwordConfirmation) throws Exception {
|
||||||
this.currentUser = userManager.addUser(firstname, lastName, username, year, day, monthName, password,passwordConfirmation);
|
this.currentUser = userManager.addUser(firstname, lastName, email, year, day, monthName, password,passwordConfirmation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean userSignIn(String username, char[] password) throws Exception {
|
public boolean userSignIn(String username, char[] password) throws Exception {
|
||||||
|
@ -73,7 +73,7 @@ public class EasyMail {
|
||||||
public String[] sendUserDetails() {
|
public String[] sendUserDetails() {
|
||||||
String[] details = new String[2];
|
String[] details = new String[2];
|
||||||
String name = this.currentUser.getFirstname() + " " + this.currentUser.getLastname();
|
String name = this.currentUser.getFirstname() + " " + this.currentUser.getLastname();
|
||||||
String username = this.currentUser.getUsermail().getUsername();
|
String username = this.currentUser.getUsermail().getUserEmail();
|
||||||
details[0] = name;
|
details[0] = name;
|
||||||
details[1] = username;
|
details[1] = username;
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ public class EasyMail {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsernameFromCurrentUser() {
|
public String getUsernameFromCurrentUser() {
|
||||||
return this.currentUser.getUsermail().getUsername();
|
return this.currentUser.getUsermail().getUserEmail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -42,10 +42,10 @@ public class Email {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String showEmailsInSent() {
|
public String showEmailsInSent() {
|
||||||
return receiver.getUsermail().getUsername() + "," + subject + "," + formattDate() + "," + content ;
|
return receiver.getUsermail().getUserEmail() + "," + subject + "," + formattDate() + "," + content ;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String showEmails() {
|
public String showEmails() {
|
||||||
return sender.getUsermail().getUsername() + "," + subject + "," + formattDate() + "," + content ;
|
return sender.getUsermail().getUserEmail() + "," + subject + "," + formattDate() + "," + content ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,14 @@ public class User {
|
||||||
private String firstname;
|
private String firstname;
|
||||||
private String lastname;
|
private String lastname;
|
||||||
private LocalDate birthdate;
|
private LocalDate birthdate;
|
||||||
private UserEmail usermail;
|
private UserEmail userEmail;
|
||||||
|
|
||||||
public User(String firstname, String lastname, LocalDate birthdate,String nutzername, char[] password) {
|
public User(String firstname, String lastname, LocalDate birthdate,String email, char[] password) {
|
||||||
this.userID = counter++;
|
this.userID = counter++;
|
||||||
this.firstname = firstname;
|
this.firstname = firstname;
|
||||||
this.lastname = lastname;
|
this.lastname = lastname;
|
||||||
this.birthdate = birthdate;
|
this.birthdate = birthdate;
|
||||||
this.usermail = new UserEmail(nutzername,password);
|
this.userEmail = new UserEmail(email,password);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFirstname() {
|
public String getFirstname() {
|
||||||
|
@ -48,7 +48,7 @@ public class User {
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEmail getUsermail() {
|
public UserEmail getUsermail() {
|
||||||
return usermail;
|
return userEmail;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ public class UserEmail {
|
||||||
|
|
||||||
private static int counter = 1000;
|
private static int counter = 1000;
|
||||||
private int accountID;
|
private int accountID;
|
||||||
private String username;
|
private String email;
|
||||||
private boolean status;
|
private boolean status;
|
||||||
private char[] password;
|
private char[] password;
|
||||||
private SentFolder sentFolder;
|
private SentFolder sentFolder;
|
||||||
|
@ -14,7 +14,7 @@ public class UserEmail {
|
||||||
private Inbox inbox;
|
private Inbox inbox;
|
||||||
|
|
||||||
public UserEmail(String username, char[] password) {
|
public UserEmail(String username, char[] password) {
|
||||||
this.username = username;
|
this.email = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.accountID = counter++;
|
this.accountID = counter++;
|
||||||
this.status = true;
|
this.status = true;
|
||||||
|
@ -23,8 +23,8 @@ public class UserEmail {
|
||||||
this.inbox = new Inbox();
|
this.inbox = new Inbox();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUserEmail() {
|
||||||
return username;
|
return email;
|
||||||
}
|
}
|
||||||
|
|
||||||
public char[] getPassword() {
|
public char[] getPassword() {
|
||||||
|
|
|
@ -3,21 +3,20 @@ package domain.user;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
|
|
||||||
private ArrayList<User> users;
|
private HashMap<String,User> users;
|
||||||
|
|
||||||
public UserManager(){
|
public UserManager(){
|
||||||
this.users = new ArrayList<>();
|
this.users = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
public User addUser(String firstName, String lastName, String username, int year, int day, String monthName,
|
public User addUser(String firstName, String lastName, String email, int year, int day, String monthName,
|
||||||
char[] password, char[] passwordConfirmation) throws Exception {
|
char[] password, char[] passwordConfirmation) throws Exception {
|
||||||
|
|
||||||
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() || password.length == 0
|
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || email.trim().isEmpty() || password.length == 0
|
||||||
|| passwordConfirmation.length == 0) {
|
|| passwordConfirmation.length == 0) {
|
||||||
throw new IllegalArgumentException("All fields are required!");
|
throw new IllegalArgumentException("All fields are required!");
|
||||||
}
|
}
|
||||||
|
@ -26,11 +25,13 @@ public class UserManager {
|
||||||
|
|
||||||
if (!Arrays.equals(password, passwordConfirmation))
|
if (!Arrays.equals(password, passwordConfirmation))
|
||||||
throw new IllegalArgumentException("Passwords do not match!");
|
throw new IllegalArgumentException("Passwords do not match!");
|
||||||
|
|
||||||
String email = username + "@easymail.de";
|
String domain = "@easymail.de";
|
||||||
if (findUserByUsername(email) != null)
|
email += domain;
|
||||||
throw new UserAlreadyExistsException("This email address is already taken!");
|
|
||||||
|
if (users.containsKey(email))
|
||||||
|
throw new UserAlreadyExistsException("Email already registered!");
|
||||||
|
|
||||||
int month = getMonthNumber(monthName);
|
int month = getMonthNumber(monthName);
|
||||||
if (month == 0)
|
if (month == 0)
|
||||||
throw new IllegalArgumentException("Invalid month name: " + monthName);
|
throw new IllegalArgumentException("Invalid month name: " + monthName);
|
||||||
|
@ -39,48 +40,57 @@ public class UserManager {
|
||||||
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
char[] passwordCopy = Arrays.copyOf(password, password.length);
|
||||||
|
|
||||||
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
|
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
|
||||||
|
users.put(email, newUser);
|
||||||
users.add(newUser);
|
|
||||||
|
|
||||||
Arrays.fill(password, ' ');
|
Arrays.fill(password, ' ');
|
||||||
Arrays.fill(passwordConfirmation, ' ');
|
Arrays.fill(passwordConfirmation, ' ');
|
||||||
return newUser;
|
return newUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User checkLogin(String username, char[] password)throws Exception{
|
public User checkLogin(String userEmail, char[] password) throws Exception {
|
||||||
if (username.trim().isEmpty() || password.length == 0)
|
|
||||||
throw new UserAlreadyExistsException("All fields are required!");
|
if (userEmail.trim().isEmpty()|| password.length == 0)
|
||||||
|
throw new IllegalArgumentException("All fields are required!");
|
||||||
|
|
||||||
|
// Email generieren (case-insensitive)
|
||||||
|
userEmail = userEmail.toLowerCase();
|
||||||
|
User user = users.get(userEmail);
|
||||||
|
if (user == null) {
|
||||||
|
Arrays.fill(password, ' ');
|
||||||
|
throw new UserNotFoundException("User not found!");
|
||||||
|
}
|
||||||
|
|
||||||
for (User user : users)
|
if (!Arrays.equals(user.getUsermail().getPassword(), password)) {
|
||||||
if (user.getUsermail().getUsername().equalsIgnoreCase(username)
|
Arrays.fill(password, ' ');
|
||||||
&& Arrays.equals(user.getUsermail().getPassword(), password)) {
|
throw new SecurityException("Invalid password!");
|
||||||
Arrays.fill(password, ' ');
|
}
|
||||||
return user;
|
|
||||||
}
|
|
||||||
Arrays.fill(password, ' ');
|
|
||||||
throw new UserNotFoundException("This email address is not found!");
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean removeUser(String username) throws UserNotFoundException {
|
Arrays.fill(password, ' ');
|
||||||
if (username == null || username.trim().isEmpty())
|
return user;
|
||||||
throw new IllegalArgumentException("Username cannot be null or empty!");
|
}
|
||||||
|
|
||||||
User userToBeRemoved = findUserByUsername(username);
|
|
||||||
if (userToBeRemoved == null)
|
|
||||||
throw new UserNotFoundException("This email address is not found!");
|
|
||||||
|
|
||||||
users.remove(userToBeRemoved);
|
public boolean removeUser(String userEmail) throws UserNotFoundException {
|
||||||
return true;
|
if (userEmail.trim().isEmpty())
|
||||||
}
|
throw new IllegalArgumentException("email is required!");
|
||||||
|
|
||||||
|
userEmail = userEmail.toLowerCase();
|
||||||
|
User removed = users.remove(userEmail);
|
||||||
|
|
||||||
|
if (removed == null)
|
||||||
|
throw new UserNotFoundException("User not found!");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public int getNumberOfUsers() {
|
public int getNumberOfUsers() {
|
||||||
return users.size();
|
return users.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public User updateUser(String firstName, String lastName, String username, int year, int day, String monthName,
|
public User updateUser(String firstName, String lastName, String userEmail, int year, int day, String monthName,
|
||||||
char[] password, char[] passwordConfirmation) throws Exception {
|
char[] password, char[] passwordConfirmation) throws Exception {
|
||||||
|
|
||||||
User userToBeUpdated = findUserByUsername(username);
|
User userToBeUpdated = findUserByUsername(userEmail);
|
||||||
if (userToBeUpdated == null)
|
if (userToBeUpdated == null)
|
||||||
throw new UserNotFoundException("This email address is not found!");
|
throw new UserNotFoundException("This email address is not found!");
|
||||||
|
|
||||||
|
@ -126,12 +136,8 @@ public class UserManager {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public User findUserByUsername(String username) {
|
public User findUserByUsername(String userEmail) {
|
||||||
for (User tempUser : users)
|
return users.get(userEmail);
|
||||||
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(username))
|
|
||||||
return tempUser;
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getMonthNumber(String txtMonth) {
|
private int getMonthNumber(String txtMonth) {
|
||||||
|
|
Loading…
Reference in New Issue