From 723099ef4e49176294f1d7cb0d6f8fdb3b2f05c4 Mon Sep 17 00:00:00 2001 From: Marius Klepp Date: Thu, 12 Feb 2026 12:13:28 +0100 Subject: [PATCH 1/3] Put input validation before field assignment --- src/main/java/model/User.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/model/User.java b/src/main/java/model/User.java index 28590b2..4dc222e 100644 --- a/src/main/java/model/User.java +++ b/src/main/java/model/User.java @@ -9,11 +9,6 @@ public class User { private final PasswordHash password; public User(String userName, String phoneNumber, PasswordHash password, String eMail) { - this.userName = userName; - this.phoneNumber = phoneNumber; - this.eMail = eMail; - this.password = password; - if ( userName == null || userName.isBlank() ) { throw new IllegalArgumentException("Username has to be filled in"); } @@ -23,12 +18,17 @@ public User(String userName, String phoneNumber, PasswordHash password, String e } if ( phoneNumber == null || phoneNumber.isBlank() ) { - throw new IllegalArgumentException("Phonenumber has to be filled in"); + throw new IllegalArgumentException("Phone number has to be filled in"); } if ( phoneNumber.length() != 8 ) { - throw new IllegalArgumentException("Fill in a phonenumber with 8 digits"); + throw new IllegalArgumentException("Fill in a phone number with 8 digits"); } + + this.userName = userName; + this.phoneNumber = phoneNumber; + this.eMail = eMail; + this.password = password; } public String getUsername() { return userName; } From b145d80f97c0f835b5266546080a2789c6aba5c6 Mon Sep 17 00:00:00 2001 From: Marius Klepp Date: Thu, 12 Feb 2026 12:51:01 +0100 Subject: [PATCH 2/3] Organization domain class --- src/main/java/model/Organization.java | 77 ++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/model/Organization.java b/src/main/java/model/Organization.java index e39aae2..9b680ac 100644 --- a/src/main/java/model/Organization.java +++ b/src/main/java/model/Organization.java @@ -1,6 +1,79 @@ package model; +import java.util.Objects; + public class Organization { - public String name; - public String orgNr; + private final String name; + private final String orgNr; + private final Cause cause; + private final String description; + private final String contactEmail; + private final String website; + private final boolean verified; + + + public Organization(String name, String orgNr, Cause cause, String description, String contactEmail, String website, boolean verified) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Name cannot be null or blank"); + } + if (orgNr == null || !orgNr.matches("\\d{9}")) { + throw new IllegalArgumentException("Organization number must be 9 digits"); + } + if (contactEmail != null) { + contactEmail = contactEmail.trim(); + + if (contactEmail.isBlank()) { + throw new IllegalArgumentException("Email cannot be blank"); + } + + if (!contactEmail.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$" + )) //Got help from ChatGPT on regex + { + throw new IllegalArgumentException("Invalid email format"); + } + } + + if (website != null) { + website = website.trim(); + + if (website.isBlank()) { + throw new IllegalArgumentException("Website cannot be blank"); + } + + if (!website.matches("^https?://.+")) { //Got help from ChatGPT for regex + throw new IllegalArgumentException("Website must start with http:// or https://"); + } + } + + this.name = name.trim(); + this.orgNr = orgNr.trim(); + this.cause = Objects.requireNonNull(cause, "Cause cannot be null"); + this.description = description; + this.contactEmail = contactEmail; + this.website = website; + this.verified = verified; + } + + + public String getName() { + return name; + } + public String getOrgNr() { + return orgNr; + } + public Cause getCause() { + return cause; + } + public String getDescription() { + return description; + } + public String getContactEmail() { + return contactEmail; + } + public String getWebsite() { + return website; + } + public boolean isVerified() { + return verified; + } } From 02c48681bbc4ceacb99f5ab996693aa4245a0b11 Mon Sep 17 00:00:00 2001 From: Marius Klepp Date: Thu, 26 Feb 2026 10:45:52 +0100 Subject: [PATCH 3/3] Made removeUser method --- src/main/java/register/UserRegister.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/register/UserRegister.java b/src/main/java/register/UserRegister.java index 398cee3..6875c8f 100644 --- a/src/main/java/register/UserRegister.java +++ b/src/main/java/register/UserRegister.java @@ -13,8 +13,18 @@ public UserRegister(String userName, User user) { } public void addUser(User user) { + if (user == null) { + throw new IllegalArgumentException("User cannot be null"); + } if (!users.containsKey(user.getUsername())) { users.put(user.getUsername(), user); } } + + public void removeUser(User user) { + if (user == null) { + throw new IllegalArgumentException("User cannot be null"); + } + users.remove(user.getUsername()); + } }