Skip to content

Commit

Permalink
Merge pull request #31 from einaskoi/per/exchange
Browse files Browse the repository at this point in the history
impliment logic in exchange class
  • Loading branch information
einaskoi authored Feb 23, 2026
2 parents 8067564 + aa53277 commit 19899ac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Purchase;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Sale;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.Transaction;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -13,12 +16,14 @@ public class Exchange {
private int week;
private Map<String, Stock> stockMap;
private Random random;
private List<Stock> stocks;

public Exchange(String name, List<Stock> stocks) {
this.name = name;
this.week = 0;
this.stockMap = new HashMap<String, Stock>();
this.random = new Random();
this.stocks = stocks;
}

public String getName() {
Expand All @@ -30,23 +35,41 @@ public int getWeek() {
}

public boolean hasStock(String symbol) {
return true;
return stockMap.containsKey(symbol);
}

public Stock getStock(String symbol) {
return null;
return stockMap.get(symbol);
}

public List<Stock> findStocks(String searchTerm) {
return null;
List<Stock> foundStocks = new ArrayList<>();

for (Stock stock : stocks) {
if (stock.getCompany().equals(searchTerm)) {
foundStocks.add(stock);
}
}
return foundStocks;
}

public Transaction buy(String symbol, BigDecimal quantity, Player player) {
return null;

player.withdrawMoney(quantity);
Stock stock = getStock(symbol);
BigDecimal purchasePrice = stock.getSalesPrice();

Share share = new Share(stock, quantity, purchasePrice);
return new Purchase(share, week);
}

public Transaction sell(Share share, Player player) {
return null;
BigDecimal salesPrice = share.getStock().getSalesPrice();
BigDecimal quantity = share.getQuantity();

player.addMoney(salesPrice.multiply(quantity));

return new Sale(share, week);
}

public void advance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Share {
private BigDecimal quantity;
private BigDecimal purchasePrice;

public void Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
this.stock = stock;
this.quantity = quantity;
this.purchasePrice = purchasePrice;
Expand Down

0 comments on commit 19899ac

Please sign in to comment.