Skip to content

Finished issue get-gainers&losers #18

Merged
merged 2 commits into from
Mar 24, 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
39 changes: 39 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
* Exchange class
Expand Down Expand Up @@ -99,6 +100,44 @@ public List<Stock> findStocks(String searchTerm) {
return stocksFound;
}

/**
* Method for obtaining gainers
*
* <p>
* Returns the stocks that have done it the best
* in the latest week.
* </p>
*
* @param limit - Amount of stocks to be returned.
* @return A list of stocks sorted in declining order.
*/
public List<Stock> getGainers(int limit) {
return stockMap.values().stream()
.filter(stock -> stock.getLatestPriceChange().compareTo(BigDecimal.ZERO) > 0)
.sorted(Comparator.comparing(Stock::getLatestPriceChange).reversed())
.limit(limit)
.toList();
}

/**
* Method for obtaining losers
*
* <p>
* Returns the stocks that have done it the worst
* in value in the latest week.
* </p>
*
* @param limit - Amount of stocks to be returned.
* @return A list of stocks sorted in inclining order.
*/
public List<Stock> getLosers(int limit) {
return stockMap.values().stream()
.filter(stock -> stock.getLatestPriceChange().compareTo(BigDecimal.ZERO) < 0)
.sorted(Comparator.comparing(Stock::getLatestPriceChange))
.limit(limit)
.toList();
}

/**
* Method to allow a player to buy a stock.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/ntnu/idi/idatt/marked/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public BigDecimal getLatestPriceChange() {
return BigDecimal.ZERO;
}
int size = prices.size();
return prices.get(size - 1).min(prices.get(size - 2));
return prices.get(size - 1).subtract(prices.get(size - 2));
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt/ExchangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ void PTAdvance() {

}

/**
* Test for obtaining the gainers and loosers.
*/
@Test
void PTgetGainers_Losers() {
// Simulate price change
stocks.get(0).addNewSalesPrice(new BigDecimal("4333"));
stocks.get(1).addNewSalesPrice(new BigDecimal("50"));
stocks.get(2).addNewSalesPrice(new BigDecimal("3"));
stocks.get(3).addNewSalesPrice(new BigDecimal("800"));

assertEquals(2, exchange.getGainers(2).size());
assertEquals(2, exchange.getLosers(2).size());

assertEquals(List.of(stocks.get(0)), exchange.getGainers(1));
assertEquals(List.of(stocks.get(2)), exchange.getLosers(1));
}

/**
* Negative tests for stock-related methods.
*
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/edu/ntnu/idi/idatt/marked/StockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void PTgetPrices() {
assertEquals(prices, stock.getHistoricalPrices());
assertEquals(new BigDecimal("39.5"), stock.getLowestPrice());
assertEquals(new BigDecimal("51.2"), stock.getHighestPrice());
assertEquals(new BigDecimal("43.4").min(new BigDecimal("51.2")), stock.getLatestPriceChange());
assertEquals(new BigDecimal("43.4").subtract(new BigDecimal("51.2")), stock.getLatestPriceChange());
}

@Test
Expand Down