Skip to content

Commit

Permalink
Feat: Updated validation for Stock
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 25, 2026
1 parent 706db1d commit 1c015f8
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,26 @@ 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,
final BigDecimal salesPrice) throws IllegalArgumentException {
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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 1c015f8

Please sign in to comment.