Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Apr 20, 2026
1 parent c9c91b0 commit c3a9c17
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
19 changes: 19 additions & 0 deletions src/main/java/millions/model/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

public class Exchange {
private String name;
Expand Down Expand Up @@ -67,6 +70,22 @@ public List<Stock> findStocks(String searchTerm) {
.toList();
}

public List<Stock> getGainers(int limit) {
Collection<Stock> stocksCollection = stocks.values();
return stocksCollection.stream()
.sorted(Comparator.comparing(Stock::getLatestPriceChange).reversed())
.limit(limit)
.collect(Collectors.toList());
}

public List<Stock> getLosers(int limit) {
Collection<Stock> stocksCollection = stocks.values();
return stocksCollection.stream()
.sorted(Comparator.comparing(Stock::getLatestPriceChange))
.limit(limit)
.collect(Collectors.toList());
}

public void advance() {
this.weekNumber++;
for (Stock stock : this.stocks.values()) {
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/millions/model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Player {
private BigDecimal money;
private Portfolio portfolio;
private TransactionArchive transactionArchive;
//temporary attribute until a better solution is found
// temporary attribute until a better solution is found
public int weeksTraded;

public Player(String name, BigDecimal startingMoney) {
Expand Down Expand Up @@ -42,12 +42,6 @@ public void withdrawMoney(BigDecimal amount) {
this.money = this.money.subtract(amount);
}

public BigDecimal getNetWorth() {
BigDecimal netWorth = this.money;
netWorth = netWorth.add(this.portfolio.getNetWorth());
return netWorth;
}

public String getStatus() {
String status = "Novice";
BigDecimal netWorth = getNetWorth();
Expand All @@ -73,6 +67,10 @@ public Portfolio getPortfolio() {
return this.portfolio;
}

public BigDecimal getNetWorth() {
return this.money.add(this.portfolio.getNetWorth());
}

public TransactionArchive getTransactionArchive() {
return this.transactionArchive;
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/millions/model/Portfolio.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package millions.model;

import millions.model.calculators.SaleCalculator;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import millions.model.calculators.SaleCalculator;

public class Portfolio {
List<Share> shares;
Expand Down Expand Up @@ -32,11 +31,12 @@ public List<Share> getShares(String symbol) {
}

public BigDecimal getNetWorth() {
BigDecimal netWorth = new BigDecimal(0);
BigDecimal total = BigDecimal.ZERO;
for (Share share : shares) {
netWorth = netWorth.add(new SaleCalculator(share).calculateTotal());
BigDecimal value = new SaleCalculator(share).calculateTotal();
total = total.add(value);
}
return netWorth;
return total;
}

public boolean contains(Share share) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/millions/model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public BigDecimal getLowestPrice() {
}

public BigDecimal getLatestPriceChange() {
if (this.prices.size() < 2) {
return BigDecimal.ZERO;
}

BigDecimal currentPrice = this.prices.getLast();
BigDecimal lastPrice = this.prices.get(this.prices.size() - 2);

Expand Down

0 comments on commit c3a9c17

Please sign in to comment.