Skip to content

impliment logic in exchange class #31

Merged
merged 1 commit into from
Feb 23, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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