From 7ea77dd3d992fba4b8bbfd4b8b1bb5d78d6bc8ff Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 19:57:10 +0200 Subject: [PATCH] Update Exchange class with exceptions --- src/main/java/Model/Exchange.java | 62 ++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/main/java/Model/Exchange.java b/src/main/java/Model/Exchange.java index a15512f..40646b7 100644 --- a/src/main/java/Model/Exchange.java +++ b/src/main/java/Model/Exchange.java @@ -17,12 +17,22 @@ public class Exchange { private final List observers = new ArrayList<>(); public Exchange(String name, List stocks) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Exchange name cannot be null or blank"); + } + if (stocks == null) { + throw new IllegalArgumentException("Stock list cannot be null"); + } + this.name = name; this.week = 1; this.stockMap = new HashMap<>(); this.random = new Random(); for (Stock stock : stocks) { + if (stock == null) { + throw new IllegalArgumentException("Stock list must not contain null entries"); + } stockMap.put(stock.getSymbol(), stock); } } @@ -30,7 +40,10 @@ public Exchange(String name, List stocks) { // ---- Observer ---- public void addObserver(ExchangeObserver observer) { - if (observer != null && !observers.contains(observer)) { + if (observer == null) { + throw new IllegalArgumentException("Observer cannot be null"); + } + if (!observers.contains(observer)) { observers.add(observer); } } @@ -54,14 +67,23 @@ public int getWeek() { } public boolean hasStock(String symbol) { + if (symbol == null || symbol.isBlank()) { + throw new IllegalArgumentException("Symbol cannot be null or blank"); + } return stockMap.containsKey(symbol); } public Stock getStock(String symbol) { + if (symbol == null || symbol.isBlank()) { + throw new IllegalArgumentException("Symbol cannot be null or blank"); + } return stockMap.get(symbol); } public List findStocks(String searchTerm) { + if (searchTerm == null) { + throw new IllegalArgumentException("Search term cannot be null"); + } List result = new ArrayList<>(); String lowerSearch = searchTerm.toLowerCase(); @@ -76,20 +98,27 @@ public List findStocks(String searchTerm) { } public Transaction buy(String symbol, BigDecimal quantity, Player player) { + if (symbol == null || symbol.isBlank()) { + throw new IllegalArgumentException("Symbol cannot be null or blank"); + } + if (quantity == null) { + throw new IllegalArgumentException("Quantity cannot be null"); + } + if (quantity.compareTo(BigDecimal.ZERO) <= 0) { + throw new IllegalArgumentException("Quantity must be greater than zero"); + } + if (player == null) { + throw new IllegalArgumentException("Player cannot be null"); + } + Stock stock = getStock(symbol); - - // unngå nullpointerexception if (stock == null) { - return null; + throw new IllegalArgumentException("No stock found with symbol: " + symbol); } // lager en ny "andel" basert på nåværende salgspris Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice()); - - // oppretter kjøpstransaksjonen for den uka via fabrikken - Transaction purchase = TransactionFactory.createPurchase(shareToBuy, this.week); - - // committer til player + Purchase purchase = new Purchase(shareToBuy, this.week); purchase.commit(player); notifyObservers(); @@ -98,15 +127,14 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) { } public Transaction sell(Share share, Player player) { - // unngå nullpointerexception if (share == null) { - return null; + throw new IllegalArgumentException("Share cannot be null"); + } + if (player == null) { + throw new IllegalArgumentException("Player cannot be null"); } - // oppretter salgstransaksjonen for den uka via fabrikken - Transaction sale = TransactionFactory.createSale(share, this.week); - - // commiter til player + Sale sale = new Sale(share, this.week); sale.commit(player); notifyObservers(); @@ -138,14 +166,14 @@ public void advance() { public List getGainers(int limit) { // viser "vinnerne" return stockMap.values().stream() - .sorted((stock1, stock2) -> stock2.getLatestPriceChange().compareTo(stock1.getLatestPriceChange())) + .sorted((s1, s2) -> s2.getLatestPriceChange().compareTo(s1.getLatestPriceChange())) .limit(limit) .toList(); } public List getLosers(int limit) { // viser "taperne" return stockMap.values().stream() - .sorted((stock1, stock2) -> stock1.getLatestPriceChange().compareTo(stock2.getLatestPriceChange())) + .sorted((s1, s2) -> s1.getLatestPriceChange().compareTo(s2.getLatestPriceChange())) .limit(limit) .toList(); }