diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java index 3cf9f1c..281a9d4 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java @@ -23,7 +23,6 @@ public class Exchange { private int week; private Map stockMap; private Random random; - private List stocks; /** * Constructs a new {@code Exchange} with a name and a list of stocks. @@ -36,7 +35,6 @@ public Exchange(String name, List stocks) { this.week = 0; this.stockMap = new HashMap(); this.random = new Random(); - this.stocks = stocks; for (Stock stock : stocks) { stockMap.put(stock.getSymbol(), stock); } @@ -79,7 +77,7 @@ public Stock getStock(String symbol) { public List findStocks(String searchTerm) { List foundStocks = new ArrayList<>(); - for (Stock stock : stocks) { + for (Stock stock : stockMap.values()) { if (stock.getCompany().equals(searchTerm)) { foundStocks.add(stock); } @@ -116,7 +114,7 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) { * * @param share the share to sell * @param player the player performing the sale - * @return a {@code Sale} transaction representing the sale + * @return a sale transaction representing the sale */ public Transaction sell(Share share, Player player) { SaleCalculator saleCalculator = new SaleCalculator(share); @@ -130,4 +128,26 @@ public Transaction sell(Share share, Player player) { public void advance() { week += 1; } + + /** + * Returns the top stocks by price change given a limit. + * + * @param limit the number of stocks to return + * @return a list of stocks sorted by price change + */ + public List getGainers(int limit) { + return stockMap.values().stream().sorted((a, b) -> b.getLatestPriceChange() + .compareTo(a.getLatestPriceChange())).limit(limit).toList(); + } + + /** + * Returns the bottom stocks by price change given a limit. + * + * @param limit the number of stocks to return + * @return a list of stocks sorted by price change + */ + public List getLosers(int limit) { + return stockMap.values().stream().sorted((a, b) -> a.getLatestPriceChange() + .compareTo(b.getLatestPriceChange())).limit(limit).toList(); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java index c3f33af..ffbc303 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java @@ -37,7 +37,7 @@ public String getCompany() { } public BigDecimal getSalesPrice() { - return prices.get(prices.size() - 1); + return prices.isEmpty() ? BigDecimal.ZERO : prices.get(prices.size() - 1); } /** @@ -60,7 +60,11 @@ public BigDecimal getLowestPrice() { return prices.stream().min(BigDecimal::compareTo).orElse(BigDecimal.ZERO); } - public BigDecimal getLatestPrice() { - return prices.isEmpty() ? BigDecimal.ZERO : prices.get(prices.size() - 1); + public BigDecimal getLatestPriceChange() { + if (prices.size() < 2) { + return BigDecimal.ZERO; + } + return prices.get(prices.size() - 1) + .subtract(prices.get(prices.size() - 2)); } }