Skip to content

Commit

Permalink
Added exeption handling in User
Browse files Browse the repository at this point in the history
  • Loading branch information
robinsp committed Mar 3, 2026
1 parent ab2de3d commit 5ed46ad
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.UUID;

// TODO: Unntakshåndtering mangler
// TODO: Enhetstesting mangler

/**
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -66,7 +88,7 @@ public String getEmail() {
return email;
}

public String getRole() {
public Role getRole() {
return role;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit 5ed46ad

Please sign in to comment.