From 7ce2816a1ec28c1d6f5952fef51348974ac3cfc5 Mon Sep 17 00:00:00 2001 From: Elisabeth Berg Date: Sun, 24 May 2026 23:56:27 +0200 Subject: [PATCH] Added Checkstyle to Exchange --- .vscode/settings.json | 3 + src/main/java/Model/Exchange.java | 338 ++++++++++++++++-------------- 2 files changed, 181 insertions(+), 160 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..319ff73 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.checkstyle.configuration": "/google_checks.xml" +} \ No newline at end of file diff --git a/src/main/java/Model/Exchange.java b/src/main/java/Model/Exchange.java index 6db979c..753e980 100644 --- a/src/main/java/Model/Exchange.java +++ b/src/main/java/Model/Exchange.java @@ -1,4 +1,5 @@ package Model; + import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; @@ -6,216 +7,233 @@ import java.util.Map; import java.util.Random; +/** + * Exchange class. + */ public class Exchange { - private final String name; - private int week; - private final Map stockMap; - private final Random random; - - // Registered observers - private final List observers = new ArrayList<>(); + private final String name; + private int week; + private final Map stockMap; + private final Random random; - 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"); - } + // Registered observers + private final List observers = new ArrayList<>(); - 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); - } + public Exchange(String name, List stocks) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("Exchange name cannot be null or blank"); } - - // ---- Observer ---- - - public void addObserver(ExchangeObserver observer) { - if (observer == null) { - throw new IllegalArgumentException("Observer cannot be null"); - } - if (!observers.contains(observer)) { - observers.add(observer); - } + if (stocks == null) { + throw new IllegalArgumentException("Stock list cannot be null"); } - - public void removeObserver(ExchangeObserver observer) { - observers.remove(observer); + + 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); } + } - private void notifyObservers() { - for (ExchangeObserver observer : observers) { - observer.onExchangeUpdated(this); - } - } + // ---- Observer ---- - public String getName() { - return name; + public void addObserver(ExchangeObserver observer) { + if (observer == null) { + throw new IllegalArgumentException("Observer cannot be null"); } - - public int getWeek() { - return week; + if (!observers.contains(observer)) { + observers.add(observer); } + } - public boolean hasStock(String symbol) { - if (symbol == null || symbol.isBlank()) { - throw new IllegalArgumentException("Symbol cannot be null or blank"); - } - return stockMap.containsKey(symbol); - } + public void removeObserver(ExchangeObserver observer) { + observers.remove(observer); + } - public Stock getStock(String symbol) { - if (symbol == null || symbol.isBlank()) { - throw new IllegalArgumentException("Symbol cannot be null or blank"); - } - return stockMap.get(symbol); + private void notifyObservers() { + for (ExchangeObserver observer : observers) { + observer.onExchangeUpdated(this); } + } - public List findStocks(String searchTerm) { - if (searchTerm == null) { - throw new IllegalArgumentException("Search term cannot be null"); - } - List result = new ArrayList<>(); - String lowerSearch = searchTerm.toLowerCase(); + public String getName() { + return name; + } - for (Stock stock : stockMap.values()) { - if (stock.getSymbol().toLowerCase().contains(lowerSearch) - || stock.getCompany().toLowerCase().contains(lowerSearch)) { - result.add(stock); - } - } + public int getWeek() { + return week; + } - return result; + 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(); + + for (Stock stock : stockMap.values()) { + if (stock.getSymbol().toLowerCase().contains(lowerSearch) + || stock.getCompany().toLowerCase().contains(lowerSearch)) { + result.add(stock); + } } - 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"); - } + return result; + } + + 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); - if (stock == null) { - throw new IllegalArgumentException("No stock found with symbol: " + symbol); - } + Stock stock = getStock(symbol); + if (stock == 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()); - Purchase purchase = new Purchase(shareToBuy, this.week); - purchase.commit(player); + // lager en ny "andel" basert på nåværende salgspris + Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice()); + Purchase purchase = new Purchase(shareToBuy, this.week); + purchase.commit(player); - notifyObservers(); + notifyObservers(); - return purchase; - } + return purchase; + } - public Transaction sell(Share share, Player player) { + public Transaction sell(Share share, Player player) { + return sell(share, share.getQuantity(), player); + } - return sell(share, share.getQuantity(), player); + public Transaction sell(Share originalShare, BigDecimal sellQuantity, Player player) { + if (originalShare == null || sellQuantity == null) { + throw new IllegalArgumentException("Share and quantity cannot be null"); + } + if (player == null) { + throw new IllegalArgumentException("Player cannot be null"); } - public Transaction sell(Share originalShare, BigDecimal sellQuantity, Player player) { - if (originalShare == null || sellQuantity == null) { - throw new IllegalArgumentException("Share and quantity cannot be null"); - } - if (player == null) { - throw new IllegalArgumentException("Player cannot be null"); - } - - // Kan ikke selge mer enn man eier - if (sellQuantity.compareTo(originalShare.getQuantity()) > 0) { - throw new IllegalArgumentException("Cannot sell more shares than owned"); - } - + // Kan ikke selge mer enn man eier + if (sellQuantity.compareTo(originalShare.getQuantity()) > 0) { + throw new IllegalArgumentException("Cannot sell more shares than owned"); + } - Share shareToSell; - if (sellQuantity.compareTo(originalShare.getQuantity()) < 0) { + Share shareToSell; - /** - * Delsalg: - * Original mengde - mengde som selges = gjenværende mengde - */ + if (sellQuantity.compareTo(originalShare.getQuantity()) < 0) { - player.getPortfolio().removeShare(originalShare); - BigDecimal remainderShare = originalShare.getQuantity().subtract(sellQuantity); + //Delsalg: + // Original mengde - mengde som selges = gjenværende mengde - player.getPortfolio().addShare(new Share(originalShare.getStock(), remainderShare, originalShare.getPurchasePrice())); + player.getPortfolio().removeShare(originalShare); - /** - * Legger delmengden midlertidig til i portfolio slik at Sale.commit() finner den - */ + BigDecimal remainderShare = originalShare.getQuantity().subtract(sellQuantity); - shareToSell = new Share(originalShare.getStock(), sellQuantity, originalShare.getPurchasePrice()); + player.getPortfolio().addShare(new Share(originalShare + .getStock(), remainderShare, originalShare.getPurchasePrice())); - player.getPortfolio().addShare(shareToSell); + + //Legger delmengden midlertidig til i portfolio slik at Sale.commit() finner den - } + shareToSell = new Share(originalShare + .getStock(), sellQuantity, originalShare.getPurchasePrice()); - else { - // Fullstendig salg: hele andelen selges som normalt - shareToSell = originalShare; - } + player.getPortfolio().addShare(shareToSell); - // Salgstransaksjon - Transaction sale = TransactionFactory.createSale(shareToSell, this.week); + } - sale.commit(player); - notifyObservers(); - return sale; + else { + // Fullstendig salg: hele andelen selges som normalt + shareToSell = originalShare; } - public void advance() { - - week++; + // Salgstransaksjon + Transaction sale = TransactionFactory.createSale(shareToSell, this.week); - for (Stock stock : stockMap.values()) { // henter stock-objektene + sale.commit(player); + notifyObservers(); + return sale; + } - BigDecimal currentPrice = stock.getSalesPrice(); // henter siste pris fra Stock + /** + * Advances to the next week. + */ + public void advance() { + + week++; - double changePercent = (random.nextDouble() - 0.5) * 0.1; + for (Stock stock : stockMap.values()) { // henter stock-objektene - BigDecimal change = currentPrice.multiply(BigDecimal.valueOf(changePercent)); + BigDecimal currentPrice = stock.getSalesPrice(); // henter siste pris fra Stock - BigDecimal newPrice = currentPrice.add(change); + double changePercent = (random.nextDouble() - 0.5) * 0.1; - if (newPrice.compareTo(BigDecimal.ZERO) > 0) { // unngå negativ pris - stock.addNewSalesPrice(newPrice); - } - } + BigDecimal change = currentPrice.multiply(BigDecimal.valueOf(changePercent)); - notifyObservers(); - } + BigDecimal newPrice = currentPrice.add(change); - public List getGainers(int limit) { // viser "vinnerne" - return stockMap.values().stream() - .sorted((s1, s2) -> s2.getLatestPriceChange().compareTo(s1.getLatestPriceChange())) - .limit(limit) - .toList(); + if (newPrice.compareTo(BigDecimal.ZERO) > 0) { // unngå negativ pris + stock.addNewSalesPrice(newPrice); + } } - public List getLosers(int limit) { // viser "taperne" - return stockMap.values().stream() - .sorted((s1, s2) -> s1.getLatestPriceChange().compareTo(s2.getLatestPriceChange())) - .limit(limit) - .toList(); - } + notifyObservers(); + } + + /** + * Shows the weeks Gainers, the stocks with the best price change. + * + * @param limit how many gainers + * @return returns the gainers + */ + public List getGainers(int limit) { // viser "vinnerne" + return stockMap.values().stream() + .sorted((s1, s2) -> s2.getLatestPriceChange().compareTo(s1.getLatestPriceChange())) + .limit(limit) + .toList(); + } + + /** + * Shows the weeks Losers, the stocks with the worst price change. + * + * @param limit how many losers + * @return returns the losers + */ + public List getLosers(int limit) { // viser "taperne" + return stockMap.values().stream() + .sorted((s1, s2) -> s1.getLatestPriceChange().compareTo(s2.getLatestPriceChange())) + .limit(limit) + .toList(); + } }