Skip to content

Commit

Permalink
Finishing Exchange methods, and spelling
Browse files Browse the repository at this point in the history
  • Loading branch information
martin committed Mar 5, 2026
1 parent 570e37c commit 9964e4b
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/main/java/temppackage/Exchange.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package temppackage;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,10 +17,15 @@ public Exchange(String name, List<Stock> stockList) {
this.name = name;
this.stocks = new HashMap<>();
this.weekNumber = 1;

// Populate the stocks map to get ticker -> stock
for (Stock stock : stockList) {
this.stocks.put(stock.getSymbol(), stock);
}
}

public void buy(Player player, Stock stock, BigDecimal quantity) {
Share shareToBuy = new Share(stock, quantity, stock.getPrice());
Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice());
Purchase purchase = new Purchase(shareToBuy, this.weekNumber);
purchase.commit(player);
}
Expand All @@ -29,15 +35,34 @@ public void sell(Player player, Share share) {
sale.commit(player);
}

public void advance() {
this.weekNumber++;
}

public Map<String, Stock> getStocks() {
return this.stocks;
}

public Stock geStock(String symbol) {
// filter med symbol eller company name
public Stock getStock(String symbol) {
return this.stocks.get(symbol);
}

public boolean hasStock(String symbol) {
return this.stocks.containsKey(symbol);
}

public List<Stock> findStocks(String searchTerm) {
return this.stocks.values().stream()
.filter(s -> s.getSymbol().contains(searchTerm) || s.getCompany().contains(searchTerm))
.toList();
}

public void advance() {
this.weekNumber++;
for (Stock stock : this.stocks.values()) {
double change = 0.9 + random.nextDouble() * 0.2;
stock.addNewSalesPrice(
stock
.getSalesPrice()
.multiply(BigDecimal.valueOf(change))
.setScale(2, RoundingMode.HALF_UP));
// RoundingMode from AI suggestion
}
}
}

0 comments on commit 9964e4b

Please sign in to comment.