From 8e0d87c79a5965a8e4c9dbd1618238e42712c79b Mon Sep 17 00:00:00 2001 From: Roar Date: Fri, 27 Mar 2026 13:37:43 +0100 Subject: [PATCH 1/2] Update Exchange Added methods for getGainers and getLosers. --- .../main/java/no/ntnu/gruppe53/Exchange.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java index abbff58..2650963 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java +++ b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java @@ -143,13 +143,33 @@ public void advance() { } stock.addNewSalesPrice(changedPrice); + } + } + public List getGainers(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).reversed()) + .filter(stock -> stock.getLatestPriceChange().compareTo(BigDecimal.ZERO) > 0) + .limit(entries) + .collect(Collectors.toList()); + } + 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()); } From da2fde96ebd810884ffb828e8c47d01f2e30d442 Mon Sep 17 00:00:00 2001 From: Roar Date: Fri, 27 Mar 2026 13:42:53 +0100 Subject: [PATCH 2/2] Update Exchange Added JavaDoc. --- .../src/main/java/no/ntnu/gruppe53/Exchange.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java index 2650963..0f729a5 100644 --- a/millions/src/main/java/no/ntnu/gruppe53/Exchange.java +++ b/millions/src/main/java/no/ntnu/gruppe53/Exchange.java @@ -146,6 +146,12 @@ 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 + */ public List getGainers(int entries) { if (entries < 0) { throw new IllegalArgumentException("Amount of entries in list cannot be negative."); @@ -159,6 +165,14 @@ public List getGainers(int entries) { .collect(Collectors.toList()); } + /** + * Lists the given amount of stocks sorted by the stocks with the lowest price increase (or + * highest price decrease) + * @param entries amount of stocks to be shown in the list + * @return a descending list of stocks with the highest price decrease or + * lowest price increase + * @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.");