Skip to content

add methods for returning top gainers and bottom losers of stocks in … #61

Merged
merged 1 commit into from
Mar 23, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class Exchange {
private int week;
private Map<String, Stock> stockMap;
private Random random;
private List<Stock> stocks;

/**
* Constructs a new {@code Exchange} with a name and a list of stocks.
Expand All @@ -36,7 +35,6 @@ public Exchange(String name, List<Stock> stocks) {
this.week = 0;
this.stockMap = new HashMap<String, Stock>();
this.random = new Random();
this.stocks = stocks;
for (Stock stock : stocks) {
stockMap.put(stock.getSymbol(), stock);
}
Expand Down Expand Up @@ -79,7 +77,7 @@ public Stock getStock(String symbol) {
public List<Stock> findStocks(String searchTerm) {
List<Stock> foundStocks = new ArrayList<>();

for (Stock stock : stocks) {
for (Stock stock : stockMap.values()) {
if (stock.getCompany().equals(searchTerm)) {
foundStocks.add(stock);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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<Stock> 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<Stock> getLosers(int limit) {
return stockMap.values().stream().sorted((a, b) -> a.getLatestPriceChange()
.compareTo(b.getLatestPriceChange())).limit(limit).toList();
}
}
10 changes: 7 additions & 3 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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