Implement user registration and login functionality

feature/login-register
Obai Albek 2025-05-30 01:37:18 +02:00
parent 502008c9c4
commit b643d2d687
7 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package domain;
import java.time.LocalDate;
public class User {
private static int counter = 1000;
private int userID;
private String firstname;
private String lastname;
private LocalDate birthdate;
private UserEmail usermail;
public User(String firstname, String lastname, LocalDate birthdate,String nutzername, char[] password) {
this.userID = counter++;
this.firstname = firstname;
this.lastname = firstname;
this.birthdate = birthdate;
this.usermail = new UserEmail(nutzername,password);
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public LocalDate getBirthdate() {
return birthdate;
}
public void setBirthdate(LocalDate birthdate) {
this.birthdate = birthdate;
}
public int getUserID() {
return userID;
}
public UserEmail getUsermail() {
return usermail;
}
}

View File

@ -0,0 +1,32 @@
package domain;
public class UserEmail {
private static int counter = 1000;
private int accountID;
private String username;
private char[] password;
public UserEmail(String username, char[] password) {
this.username = username;
this.password = password;
this.accountID = counter++;
}
public String getUsername() {
return username;
}
public char[] getPassword() {
return password;
}
public void setPassword(char[] password) {
this.password = password;
}
public int getAccountID() {
return accountID;
}
}

View File

@ -0,0 +1,86 @@
package domain;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import domain.exception.UserAlreadyExistsException;
public class UserManager {
private ArrayList<User> users;
public UserManager() {
this.users = new ArrayList<>();
}
public boolean addUser(String firstName, String lastName, String username,
int year, int day, String monthName,
char[] password, char[] passwordConfirmation) throws Exception {
if (firstName == null || lastName == null || username == null || password == null || passwordConfirmation == null) {
throw new IllegalArgumentException("No input should be null!");
}
if (firstName.trim().isEmpty() || lastName.trim().isEmpty() || username.trim().isEmpty() ||
password.length == 0 || passwordConfirmation.length == 0) {
throw new IllegalArgumentException("All fields are required!");
}
if (!Arrays.equals(password, passwordConfirmation))
throw new IllegalArgumentException("Passwords do not match!");
String email = username + "@easymail.de";
for (User tempUser : users)
if (tempUser.getUsermail().getUsername().equalsIgnoreCase(email))
throw new UserAlreadyExistsException("This email address is already taken!");
int month = getMonthNumber(monthName);
if (month == 0)
throw new IllegalArgumentException("Invalid month name: " + monthName);
LocalDate birthDate = LocalDate.of(year, month, day);
char[] passwordCopy = Arrays.copyOf(password, password.length);
User newUser = new User(firstName, lastName, birthDate, email, passwordCopy);
users.add(newUser);
Arrays.fill(password, ' ');
Arrays.fill(passwordConfirmation, ' ');
return true;
}
public boolean checkLogin(String username, char[] password) {
if (username == null || password == null) return false;
for (User user : users)
if (user.getUsermail().getUsername().equalsIgnoreCase(username)
&& Arrays.equals(user.getUsermail().getPassword(), password)) {
Arrays.fill(password, ' ');
return true;
}
Arrays.fill(password, ' ');
return false;
}
private int getMonthNumber(String txtMonth) {
switch (txtMonth.toLowerCase()) {
case "januar": return 1;
case "februar": return 2;
case "märz": return 3;
case "april": return 4;
case "mai": return 5;
case "juni": return 6;
case "juli": return 7;
case "august": return 8;
case "september": return 9;
case "oktober": return 10;
case "november": return 11;
case "dezember": return 12;
default: return 0;
}
}
}

View File

@ -0,0 +1,7 @@
package domain.exception;
public class UserAlreadyExistsException extends Exception {
public UserAlreadyExistsException(String msg) {
super(msg);
}
}

View File

@ -0,0 +1,75 @@
package domain.unitTest;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import domain.UserManager;
class AddUserTest {
private UserManager userManager;
@BeforeEach
void setUp() {
userManager = new UserManager();
}
@Test
void testLeererVorname() {
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("", "Test","nutzer", 2000, 1, "Januar", new char[]{'1'}, new char[]{'1'});
});
assertEquals("Alle Felder sind erforderlich!", ex.getMessage());
}
@Test
void testLeererNachname() {
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("Max", "","nutzer", 2000, 1, "Januar", new char[]{'1'}, new char[]{'1'});
});
assertEquals("Alle Felder sind erforderlich!", ex.getMessage());
}
@Test
void testLeererNutzername() {
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("Max", "Mustermann","", 2000, 1, "Januar", new char[]{'1'}, new char[]{'1'});
});
assertEquals("Alle Felder sind erforderlich!", ex.getMessage());
}
@Test
void testLeeresPasswort() {
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("Max", "Mustermann","nutzer",2000, 1, "Januar", new char[]{}, new char[]{});
});
assertEquals("Alle Felder sind erforderlich!", ex.getMessage());
}
@Test
void testPasswoerterUnterschiedlich() {
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("Max", "Mustermann","nutzer",2000, 1, "Januar", new char[]{'1','2'}, new char[]{'1','3'});
});
assertEquals("Passwörter stimmen nicht überein!", ex.getMessage());
}
@Test
void testNutzernameBereitsVergeben() throws Exception {
// Ersten Nutzer hinzufügen
userManager.addUser("Max", "Mustermann","nutzer",2000, 1, "Januar" , new char[]{'1'}, new char[]{'1'});
// Zweiter Nutzer mit gleicher E-Mail
Exception ex = assertThrows(Exception.class, () -> {
userManager.addUser("Moritz", "Muster","nutzer", 2001, 2, "Februar", new char[]{'1'}, new char[]{'1'});
});
assertEquals("Diese E-Mail-Adresse ist bereits vergeben!", ex.getMessage());
}
@Test
void testErfolgreichHinzufuegen() throws Exception {
boolean result = userManager.addUser("Anna", "Beispiel","anna",1995, 15, "Mai", new char[]{'1'}, new char[]{'1'});
assertTrue(result);
}
}

View File

@ -0,0 +1,34 @@
package domain.unitTest;
import static org.junit.Assert.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import domain.UserManager;
class CheckLoginTest {
private UserManager userManager;
@BeforeEach
void setUp() throws Exception {
userManager = new UserManager();
char[] pw = {'1','2','3','4'};
userManager.addUser("Ali", "Test","ali.test",2000, 1, "Januar", pw, pw.clone());
}
@Test
void testLoginMitKorrektenDaten() {
assertTrue(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','2','3','4'}));
}
@Test
void testLoginMitFalschemPasswort() {
assertFalse(userManager.checkLogin("ali.test@easymail.de", new char[]{'1','1','1','1'}));
}
@Test
void testLoginMitFalschemNutzername() {
assertFalse(userManager.checkLogin("nicht.vorhanden@easymail.de", new char[]{'1','2','3','4'}));
}
}

View File

@ -5,4 +5,7 @@
*
*/
module MailSystem {
requires java.desktop;
requires junit;
requires org.junit.jupiter.api;
}