diff --git a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java index 0f729a5..31fc20f 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java +++ b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java @@ -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 getGainers(int entries) { + private List getTopStocks(int entries, Comparator 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 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) @@ -174,16 +184,7 @@ public List getGainers(int entries) { * @throws IllegalArgumentException if number of entries in list is negative */ public List 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)); } diff --git a/millions/src/test/java/no/ntnu/gruppe53/ExchangeTest.java b/millions/src/test/java/no/ntnu/gruppe53/ExchangeTest.java index d01ca79..7167bd1 100644 --- a/millions/src/test/java/no/ntnu/gruppe53/ExchangeTest.java +++ b/millions/src/test/java/no/ntnu/gruppe53/ExchangeTest.java @@ -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."); + + } + }