Skip to content

add unit tests for Player and improved tests for Exchange and Portfolio #47

Merged
merged 3 commits into from
Feb 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,33 @@
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;
import java.util.Random;

/**
* Represents a stock exchange where players can buy and sell shares.
* <p>
* The {@code Exchange} keeps track of available stocks, the current week in the simulation,
* and allows players to perform transactions such as buying or selling shares.
* </p>
*/
public class Exchange {
private String name;
private int week;
private Map<String, Stock> stockMap;
private Random random;
private List<Stock> stocks;

/**
* Constructs a new {@code Exchange} with a name and a list of stocks.
*
* @param name the name of the exchange (e.g., "NYSE")
* @param stocks the initial list of available stocks on the exchange
*/
public Exchange(String name, List<Stock> stocks) {
this.name = name;
this.week = 0;
Expand All @@ -35,14 +47,32 @@ public int getWeek() {
return week;
}

/**
* Checks if the exchange has a stock with the given symbol.
*
* @param symbol the stock symbol to check
* @return {@code true} if the stock exists on the exchange, {@code false} otherwise
*/
public boolean hasStock(String symbol) {
return stockMap.containsKey(symbol);
}

/**
* Returns the stock associated with the given symbol.
*
* @param symbol the symbol of the stock to retrieve
* @return the {@code Stock} object, or {@code null} if not found
*/
public Stock getStock(String symbol) {
return stockMap.get(symbol);
}

/**
* Finds stocks by company name.
*
* @param searchTerm the company name to search for
* @return a list of {@code Stock} objects whose company name matches the search term
*/
public List<Stock> findStocks(String searchTerm) {
List<Stock> foundStocks = new ArrayList<>();

Expand All @@ -54,6 +84,17 @@ public List<Stock> findStocks(String searchTerm) {
return foundStocks;
}

/**
* Allows a player to buy a stock from the exchange.
* <p>
* The player’s money is withdrawn by the quantity amount, and a {@code Purchase} transaction is returned.
* </p>
*
* @param symbol the symbol of the stock to buy
* @param quantity the amount of stock to buy
* @param player the player performing the purchase
* @return a {@code Purchase} transaction representing the purchase
*/
public Transaction buy(String symbol, BigDecimal quantity, Player player) {

player.withdrawMoney(quantity);
Expand All @@ -64,12 +105,25 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) {
return new Purchase(share, week);
}

/**
* Allows a player to sell a share on the exchange.
* <p>
* The player receives money based on the sale calculation, and a {@code Sale} transaction is returned.
* </p>
*
* @param share the share to sell
* @param player the player performing the sale
* @return a {@code Sale} transaction representing the sale
*/
public Transaction sell(Share share, Player player) {
SaleCalculator saleCalculator = new SaleCalculator(share);
player.addMoney(saleCalculator.calculateTotal());
return new Sale(share, week);
}

/**
* Advances the simulation by 1 week.
*/
public void advance() {
week += 1;
}
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import java.math.BigDecimal;

/**
* Player represents the user, the money they own, and their portfolio.
* The class includes methods for adding and withdrawing money from the players portfolio.
* Represents a player in the stock market simulation.
* <p>
* A {@code Player} has a name, starting money, current money balance,
* a {@link Portfolio} of shares, and a {@link TransactionArchive} of past transactions.
* The player can add or withdraw money, and manage their portfolio through purchases and sales.
* </p>
*/
public class Player {
private final String name;
Expand All @@ -15,9 +19,10 @@ public class Player {
private TransactionArchive transactionArchive;

/**
* Constructs a new {@code Player} with a name and starting money.
*
* @param name
* @param startingMoney
* @param name the name of the player
* @param startingMoney the initial amount of money the player has
*/
public Player(String name, BigDecimal startingMoney) {
this.name = name;
Expand Down Expand Up @@ -45,16 +50,18 @@ public BigDecimal getMoney() {
}

/**
* Adds the specified amount of money to the player's balance.
*
* @param amount is the amount that will be added to players account.
* @param amount the amount to add to the player's account
*/
public void addMoney(BigDecimal amount) {
money = money.add(amount);
}

/**
* Withdraws the specified amount of money from the player's balance.
*
* @param amount is the amount that will be withdrawn from players account.
* @param amount the amount to withdraw from the player's account
*/
public void withdrawMoney(BigDecimal amount) {
money = money.subtract(amount);
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Portfolio.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
import java.util.List;

/**
* Portfolio represents a collection of {@link Share} objects owned by a {@link Player}.
* The class includes methods for adding, removing and getting shares from the portfolio.
* Represents a collection of {@link Share} objects owned by a {@link Player}.
* <p>
* A {@code Portfolio} allows adding, removing, and querying shares.
* It serves as the player's collection of owned shares.
* </p>
*/
public class Portfolio {
private List<Share> shares;

/**
* The constructor of the {@link Portfolio} class.
* Constructs an empty {@code Portfolio}.
*/
public Portfolio() {
this.shares = new ArrayList<>();
}

/**
* Method for adding a share to a portfolio.
* @param share represents the share to be added to the portfolio.
* @return true or false based on if the operation was successful or not.
* Adds a share to the portfolio.
*
* @param share the {@code Share} to add
* @return {@code true} if the share was successfully added, {@code false} if the share was {@code null}
*/
public boolean addShare(Share share) {
if (share == null) {
Expand All @@ -31,9 +35,11 @@ public boolean addShare(Share share) {
}

/**
* Method for removing a share to a portfolio.
* @param share represents the share to be removed from the portfolio.
* @return true or false based on if the operation was successful or not.
* Removes a share from the portfolio.
*
* @param share the {@code Share} to remove
* @return {@code true} if the share was successfully removed,
* {@code false} if the share was {@code null} or not present in the portfolio
*/
public boolean removeShare(Share share) {
if (share == null || !shares.contains(share)) {
Expand All @@ -48,9 +54,9 @@ public List<Share> getShares() {
}

/**
* Method for checking if a portfolio contains a given share.
* @param share represents the share to be checked.
* @return true or false based on if the share was found or not.
* Checks whether the portfolio contains a given share.
* @param share the {@code Share} to check for
* @return {@code true} if the share exists in the portfolio, {@code false} if {@code null} or not found
*/
public boolean contains(Share share) {
return share != null && shares.contains(share);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@

import java.math.BigDecimal;

/**
* Represents a share of a particular stock owned by a player.
* <p>
* A {@code Share} keeps track of the stock it represents, the quantity owned,
* and the purchase price at which it was bought.
* </p>
*/
public class Share {
private Stock stock;
private BigDecimal quantity;
private BigDecimal purchasePrice;

/**
* Constructs a new {@code Share} given the given stock, quantity, and purchase price.
* @param stock stock the stock that this share represents
* @param quantity quantity the number of shares owned
* @param purchasePrice purchasePrice the price per share at the time of purchase
*/
public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) {
this.stock = stock;
this.quantity = quantity;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Stock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@
import java.util.ArrayList;
import java.util.List;

/**
* Represents a stock of a company in the {@link Exchange}.
* <p>
* Each {@code Stock} has a unique symbol, a company name, and a history of sales prices.
* You can retrieve the current price or add new prices as the market changes.
* </p>
*/
public class Stock {
private final String symbol;
private final String company;
private List<BigDecimal> prices = new ArrayList<>();

/**
* Constructs a new {@code Stock} with the given symbol, company name, and initial sales price.
* @param symbol symbol the unique stock symbol (e.g., "AAPL")
* @param company company the company name (e.g., "Apple Inc.")
* @param salesPrice salesPrice the initial sales price of the stock
*/
public Stock(String symbol, String company, BigDecimal salesPrice) {
this.symbol = symbol;
this.company = company;
Expand All @@ -27,6 +40,11 @@ public BigDecimal getSalesPrice() {
return prices.get(prices.size() - 1);
}

/**
* Adds a new sales price to the stock's price history.
*
* @param salesPrice the new sales price to add
*/
public void addNewSalesPrices(BigDecimal salesPrice) {
prices.add(salesPrice);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import org.junit.jupiter.api.Test;

public class ExchangeTest {

private Exchange exchange;

@BeforeEach
public void setUp() {
void setUp() {
Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100"));
exchange = new Exchange("NYSE", List.of(stock));
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.ntnu.idi.idatt2003.gruppe42;

import static org.junit.jupiter.api.Assertions.assertEquals;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Player;
import java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PlayerTest {
private Player player;

@BeforeEach
void setUp() {
player = new Player("John Doe", new BigDecimal("10000"));
}

@Test
void addMoneyTest() {
player.addMoney(new BigDecimal("5000"));
assertEquals(new BigDecimal("15000"), player.getMoney());
}

@Test
void withdrawMoneyTest() {
player.withdrawMoney(new BigDecimal("5000"));
assertEquals(new BigDecimal("5000"), player.getMoney());
}
}
32 changes: 17 additions & 15 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
package edu.ntnu.idi.idatt2003.gruppe42;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Portfolio;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Share;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class PortfolioTest {
private Portfolio portfolio;
private Stock stock;
private Share share;

@BeforeEach
void setUp() {
portfolio = new Portfolio();
stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("10000"));
share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000"));
}

@Test
public void addShareTest() {
Portfolio portfolio = new Portfolio();
Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000"));
Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000"));

void addShareTest() {
boolean result = portfolio.addShare(share);
assertTrue(portfolio.contains(share));
}

@Test
public void removeShareTest() {
Portfolio portfolio = new Portfolio();
Stock stock = new Stock("AAPL", "Apple", new BigDecimal("10000"));
Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("15000"));

void removeShareTest() {
portfolio.addShare(share);
boolean result = portfolio.removeShare(share);
assertFalse(portfolio.contains(share));
Expand Down