From 5ed46ad955ff2652834610cb9c8091cf5dbd3444 Mon Sep 17 00:00:00 2001 From: Robin Strand Prestmo Date: Tue, 3 Mar 2026 19:04:20 +0100 Subject: [PATCH] Added exeption handling in User --- .../team6/models/user/User.java | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/user/User.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/user/User.java index 654fff6..28c5934 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/user/User.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/user/User.java @@ -4,7 +4,6 @@ import java.util.UUID; -// TODO: Unntakshåndtering mangler // TODO: Enhetstesting mangler /** @@ -19,7 +18,7 @@ public class User { private String name; private String email; private String passwordHash; - private final String role; + private final Role role; private final Settings settings; private final Inbox inbox; @@ -39,9 +38,32 @@ public User(UUID id, String name, String email, String password, - String role, + Role role, Settings settings, Inbox inbox) { + if (id == null) { + throw new IllegalArgumentException("ID cannot be null."); + } + + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Name cannot be null or blank."); + } + + if (email == null || email.isBlank() || !email.contains("@") || !email.contains(".")) { + throw new IllegalArgumentException("Email cannot be null or blank, and must contain '@' and '.'"); + } + + if (role == null) { + throw new IllegalArgumentException("Role cannot be null"); + } + + if (settings == null) { + throw new IllegalArgumentException("Settings cannot be null"); + } + + if (inbox == null) { + throw new IllegalArgumentException("Inbox cannot be null"); + } this.id = id; this.name = name; @@ -66,7 +88,7 @@ public String getEmail() { return email; } - public String getRole() { + public Role getRole() { return role; } @@ -81,6 +103,9 @@ public Inbox getInbox() { // Add Setters public void setName(String name) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Name cannot be null or blank."); + } this.name = name; } @@ -89,6 +114,9 @@ public void setPassword(String password) { } public void setEmail(String email) { + if (email == null || email.isBlank() || !email.contains("@") || !email.contains(".")) { + throw new IllegalArgumentException("Email cannot be null or blank, and must contains '@' and '.'"); + } this.email = email; }