From 1c015f89ca73082851390e8db914f64cfabbeb16 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 25 May 2026 19:49:18 +0200 Subject: [PATCH] Feat: Updated validation for Stock --- .../idi/idatt2003/g40/mappe/model/Stock.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java index 0aff105..d7ff05a 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java @@ -40,6 +40,8 @@ public final class Stock { * @param symbol the unique stock symbol * @param company the name of the company * @param salesPrice the initial sales price of the stock + * + * @throws IllegalArgumentException if parameters are null or invalid. */ public Stock(final String symbol, final String company, @@ -47,12 +49,17 @@ public Stock(final String symbol, if (!Validator.VALID_STOCK_SYMBOL.isValid(symbol)) { throw new IllegalArgumentException( Validator.VALID_STOCK_SYMBOL.getErrorMessage()); - } else { - this.symbol = symbol; - this.company = company; - this.fortune = 0; - prices.add(salesPrice); } + if (!Validator.NOT_EMPTY.isValid(company)) { + throw new IllegalArgumentException(Validator.NOT_EMPTY.getErrorMessage()); + } + if (salesPrice == null || salesPrice.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Sales price of cannot be negative or zero!"); + } + this.symbol = symbol; + this.company = company; + this.fortune = 0; + prices.add(salesPrice); } /** @@ -109,11 +116,11 @@ public BigDecimal getSalesPrice() { */ public void addNewSalesPrice(final BigDecimal price) throws IllegalArgumentException { - if (price != null && price.intValue() != 0) { - prices.add(price); - } else { - throw new IllegalArgumentException("Invalid price to add to stock: " + getSymbol()); + if (price == null || price.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Invalid price to add to stock: " + + getSymbol()); } + prices.add(price); } /**