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