Skip to content

Commit

Permalink
Merge pull request #30 from IDATT2003-Gruppe-53/hotfix/22-fix-getgain…
Browse files Browse the repository at this point in the history
…ers-and-getlosers-in-exchange-and-add-junit-test

Fix gainers/losers logic in exchange (#22)
  • Loading branch information
roaraf authored Apr 15, 2026
2 parents af87542 + 082a39e commit 1a98d8a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
37 changes: 19 additions & 18 deletions millions/src/main/java/no/ntnu/gruppe53/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,34 @@ public void advance() {
}

/**
* Lists the given amount of stocks sorted by highest increase in sales price since last week.
* @param entries amount of stocks to be shown in list
* @return a descending list of stocks with the highest price increase since last week
* @throws IllegalArgumentException if number of entries in list is negative
* Sorts the stocks in an ascending order and returns a list of stocks.
*
* @param entries amount of entries in the list to display
* @param comparator the comparator used to compare the stocks
* @return a list of stocks
* @throws IllegalArgumentException if the amount of entries is negative
*/
public List<Stock> getGainers(int entries) {
private List<Stock> getTopStocks(int entries, Comparator<Stock> comparator) {
if (entries < 0) {
throw new IllegalArgumentException("Amount of entries in list cannot be negative.");
}

return stockMap.values().stream()
.sorted(Comparator.comparing(Stock::getLatestPriceChange).reversed())
.filter(stock -> stock.getLatestPriceChange().compareTo(BigDecimal.ZERO) > 0)
.sorted(comparator)
.limit(entries)

.collect(Collectors.toList());
}

/**
* Lists the given amount of stocks sorted by highest increase in sales price since last week.
* @param entries amount of stocks to be shown in list
* @return a descending list of stocks with the highest price increase since last week
* @throws IllegalArgumentException if number of entries in list is negative
*/
public List<Stock> getGainers(int entries) {
return getTopStocks(entries, Comparator.comparing(Stock::getLatestPriceChange).reversed());
}

/**
* Lists the given amount of stocks sorted by the stocks with the lowest price increase (or
* highest price decrease)
Expand All @@ -174,16 +184,7 @@ public List<Stock> getGainers(int entries) {
* @throws IllegalArgumentException if number of entries in list is negative
*/
public List<Stock> getLosers(int entries) {
if (entries < 0) {
throw new IllegalArgumentException("Amount of entries in list cannot be negative.");
}

return stockMap.values().stream()
.sorted(Comparator.comparing(Stock::getLatestPriceChange))
.filter(stock -> stock.getLatestPriceChange().compareTo(BigDecimal.ZERO) < 0)
.limit(entries)

.collect(Collectors.toList());
return getTopStocks(entries, Comparator.comparing(Stock::getLatestPriceChange));
}


Expand Down
43 changes: 43 additions & 0 deletions millions/src/test/java/no/ntnu/gruppe53/ExchangeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,47 @@ void advanceWeekTest() {

}

@Test
void getGainersShouldSortCorrectly() {
appleStock.addNewSalesPrice(new BigDecimal("3000"));
microStock.addNewSalesPrice(new BigDecimal("100"));
ntnuStock.addNewSalesPrice(new BigDecimal("1500"));

var gainList = exchange.getGainers(3);

assertEquals(appleStock.getSymbol(), gainList.getFirst().getSymbol(),
"Biggest gainer should be 'AAPL'.");
assertEquals(ntnuStock.getSymbol(), gainList.get(1).getSymbol(),
"Second biggest gainer should be 'NTNU'.");
assertEquals(microStock.getSymbol(), gainList.getLast().getSymbol(),
"Third biggest gainer should be 'MSFT'.");
}

@Test
void getLosersShouldSortCorrectly() {
appleStock.addNewSalesPrice(new BigDecimal("3000"));
microStock.addNewSalesPrice(new BigDecimal("100"));
ntnuStock.addNewSalesPrice(new BigDecimal("1500"));

var gainList = exchange.getLosers(3);

System.out.println(gainList.size());

assertEquals(microStock.getSymbol(), gainList.get(0).getSymbol(),
"Biggest loser should me 'MSFT'");
assertEquals(ntnuStock.getSymbol(), gainList.get(1).getSymbol(),
"Second biggest loser should be 'NTNU'");
assertEquals(appleStock.getSymbol(), gainList.get(2).getSymbol(),
"Least biggest loser should be 'AAPL'");
}

@Test
void getTopStocksShouldThrowIllegalArgumentExceptionsIfEntriesIsNegative() {
assertThrows(IllegalArgumentException.class, () -> exchange.getLosers(-1),
"It should not be possible to list a negative amount of entries.");
assertThrows(IllegalArgumentException.class, () -> exchange.getGainers(-2),
"It should not be possible to list a negative amount of entries.");

}

}

0 comments on commit 1a98d8a

Please sign in to comment.