From aa53277c7d7451c4d2794093035976c5bf1456c6 Mon Sep 17 00:00:00 2001 From: Per Eric Trapnes Date: Mon, 23 Feb 2026 11:30:03 +0100 Subject: [PATCH] impliment logic in exchange class --- .../idatt2003/gruppe42/Model/Exchange.java | 33 ++++++++++++++++--- .../idi/idatt2003/gruppe42/Model/Share.java | 2 +- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java index 7e10040..9aac7df 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exchange.java @@ -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; @@ -13,12 +16,14 @@ public class Exchange { private int week; private Map stockMap; private Random random; + private List stocks; public Exchange(String name, List stocks) { this.name = name; this.week = 0; this.stockMap = new HashMap(); this.random = new Random(); + this.stocks = stocks; } public String getName() { @@ -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 findStocks(String searchTerm) { - return null; + List 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() { diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Share.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Share.java index 7701571..41c7ed1 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Share.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Share.java @@ -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;