From 9b827848cf8301ef05681b230db37b911685110b Mon Sep 17 00:00:00 2001 From: Robin Strand Prestmo Date: Fri, 20 Feb 2026 12:03:40 +0100 Subject: [PATCH] Added exeption handling for Message class --- .../sytemutvikling/team6/models/Message.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/Message.java b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/Message.java index 6a1d53e..7d0f951 100644 --- a/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/Message.java +++ b/helpmehelpapplication/src/main/java/ntnu/sytemutvikling/team6/models/Message.java @@ -3,42 +3,51 @@ import java.time.LocalDateTime; import java.util.UUID; -// Unntakshåndtering mangler // Enhetstester mangler /** * Represents a message * - * @Author Robin Strand Prestmo + * @author Robin Strand Prestmo */ public class Message { - private UUID id; - private String title; - private String from; - private String content; - private LocalDateTime timeAndDate; + private final UUID id; + private final String title; + private final String from; + private final String content; + private final LocalDateTime timeAndDate; /** - * Creates a message with an unique identifier. - * The message includes a title, a string who its from, + * Creates a message with a unique identifier. + * The message includes a title, a string who it's from, * content and the time and date. * - * @param id an unique identifier using UUID * @param title the title of the message * @param from who the message is from * @param content the content of the message - * @param timeAndDate when the message was sent. + * @throws IllegalArgumentException if title, from or content is null or blank. */ - public Message(UUID id, - String title, + public Message(String title, String from, - String content, - LocalDateTime timeAndDate) { - this.id = id; + String content) { + + if (title == null || title.isBlank()) { + throw new IllegalArgumentException("Title cannot be null or blank."); + } + + if (from == null || from.isBlank()) { + throw new IllegalArgumentException("From cannot be null or blank."); + } + + if (content == null || content.isBlank()) { + throw new IllegalArgumentException("Content cannot be null or blank."); + } + + this.id = UUID.randomUUID(); this.title = title; this.from = from; this.content = content; - this.timeAndDate = timeAndDate; + this.timeAndDate = LocalDateTime.now(); } public UUID getId() {