Skip to content

Commit

Permalink
Update Exchange class with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 9225c8c commit 7ea77dd
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions src/main/java/Model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,33 @@ public class Exchange {
private final List<ExchangeObserver> observers = new ArrayList<>();

public Exchange(String name, List<Stock> 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);
}
}

// ---- 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);
}
}
Expand All @@ -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<Stock> findStocks(String searchTerm) {
if (searchTerm == null) {
throw new IllegalArgumentException("Search term cannot be null");
}
List<Stock> result = new ArrayList<>();
String lowerSearch = searchTerm.toLowerCase();

Expand All @@ -76,20 +98,27 @@ public List<Stock> 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();
Expand All @@ -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();
Expand Down Expand Up @@ -138,14 +166,14 @@ public void advance() {

public List<Stock> 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<Stock> 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();
}
Expand Down

0 comments on commit 7ea77dd

Please sign in to comment.