Skip to content

Commit

Permalink
Updated missing methods in Exchange class
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed Mar 24, 2026
1 parent 33cebc4 commit 35ca87a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
46 changes: 37 additions & 9 deletions src/main/java/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,53 @@ public Stock getStock(String symbol) {
}

public List<Stock> findStocks(String searchTerm) {
List<Stock> result = new ArrayList<>();
List<Stock> result = new ArrayList<>();
String lowerSearch = searchTerm.toLowerCase();

for (Stock stock : stockMap.values()) {
if (stock.getSymbol().contains(searchTerm)
|| stock.getCompany().contains(searchTerm)) {
if (stock.getSymbol().toLowerCase().contains(lowerSearch)
|| stock.getCompany().toLowerCase().contains(lowerSearch)) {
result.add(stock);
}
}

return result;
}

// public Transaction buy(String symbol, BigDecimal quantity, Player player) {
// return;
// }
public Transaction buy(String symbol, BigDecimal quantity, Player player) {
Stock stock = getStock(symbol);

// unngå nullpointerexception
if (stock == null) {
return null;
}

// lager en ny "andel" basert på nåværende salgspris
Share shareToBuy = new Share(stock, quantity, stock.getSalesPrice());

// oppretter kjøpstransaksjonen for den uka
Purchase purchase = new Purchase(shareToBuy, this.week);

// committer til player
purchase.commit(player);

return purchase;
}

public Transaction sell(Share share, Player player) {
// unngå nullpointerexception
if (share == null) {
return null;
}

// public Transaction sell(Share share, Player player) {
// return;
// }
// oppretter salgstransaksjonen for den uka
Sale sale = new Sale(share, this.week);

// commiter til player
sale.commit(player);

return sale;
}

public void advance() {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public BigDecimal getMoney() {
}

public void addMoney(BigDecimal amount) {
this.money.add(amount);
this.money = this.money.add(amount);
}

public void withdrawMoney(BigDecimal amount) {
this.money.subtract(amount);
this.money = this.money.subtract(amount);
}

public Portfolio getPortfolio() {
Expand Down
Binary file modified target/classes/Exchange.class
Binary file not shown.

0 comments on commit 35ca87a

Please sign in to comment.