Skip to content

Commit

Permalink
Merge pull request #20 from IDATT2003-Gruppe-53/Statistics-Exchange-C…
Browse files Browse the repository at this point in the history
…lass

Merge Statistics exchange class branch with main
  • Loading branch information
roaraf authored Mar 27, 2026
2 parents 3984b06 + da2fde9 commit af87542
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions millions/src/main/java/no/ntnu/gruppe53/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,47 @@ public void advance() {
}

stock.addNewSalesPrice(changedPrice);
}
}

/**
* 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) {
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());
}

/**
* 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<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());
}


Expand Down

0 comments on commit af87542

Please sign in to comment.