Skip to content

Commit

Permalink
Finnished Sale and Purchase, then added the last tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peretr committed Feb 27, 2026
1 parent a89a527 commit 2341911
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model.Exceptions;

public class InsufficientFunds extends RuntimeException {
public InsufficientFunds(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public Exchange(String name, List<Stock> stocks) {
this.stockMap = new HashMap<String, Stock>();
this.random = new Random();
this.stocks = stocks;
for (Stock stock : stocks) {
stockMap.put(stock.getSymbol(), stock);
}
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

import edu.ntnu.idi.idatt2003.gruppe42.Model.Exceptions.InsufficientFunds;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction.TransactionArchive;
import java.math.BigDecimal;

Expand Down Expand Up @@ -64,8 +65,9 @@ public void addMoney(BigDecimal amount) {
* @param amount the amount to withdraw from the player's account
*/
public void withdrawMoney(BigDecimal amount) {
if (money.compareTo(amount) < 0) {
throw new InsufficientFunds("Not enough money to withdraw " + amount);
}
money = money.subtract(amount);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public Purchase(Share share, int week) {

@Override
public void commit(Player player) {
setCommitted(true);
player.withdrawMoney(getCalculator().calculateTotal());
player.getTransactionArchive().add(this);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public Sale(Share share, int week) {

@Override
public void commit(Player player) {

setCommitted(true);
player.withdrawMoney(getCalculator().calculateTotal());
player.getTransactionArchive().add(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class Transaction {
private Share share;
private int week;
private TransactionCalculator calculator;
private boolean committed;
private boolean committed = false;

protected Transaction(Share share, int week, TransactionCalculator calculator) {
this.share = share;
Expand All @@ -34,4 +34,8 @@ public boolean isCommitted() {
}

public void commit(Player player) {}

public void setCommitted(boolean committed) {
this.committed = committed;
}
}
10 changes: 10 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/MillionsTest.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
package edu.ntnu.idi.idatt2003.gruppe42;

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

import org.junit.jupiter.api.Test;

public class MillionsTest {
@Test
void mainTest() {
assertDoesNotThrow(() -> Millions.main(new String[]{}));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model.Calculator;

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

import edu.ntnu.idi.idatt2003.gruppe42.Model.Share;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
Expand All @@ -26,44 +27,25 @@ void setUp() {

@Test
void testCalculateGross() {
/**
* Gross = Sales Price * Quantity
* 1000 = 100 * 10
*/
BigDecimal expectedGross = new BigDecimal("1000");
assertEquals(0, expectedGross.compareTo(calculator.calculateGross()),
"Gross should be purchasePrice * quantity");
assertEquals(0, expectedGross.compareTo(calculator.calculateGross()));
assertNotEquals(0, new BigDecimal("100").compareTo(calculator.calculateGross()));
}

@Test
void testCalculateCommission() {
/**
* Commission = Gross * Commission Rate (1%)
* Commission = 1000 * 0.01 = 10
*/
BigDecimal expectedCommission = new BigDecimal("10");
assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()),
"Commission should be 1% of Gross");
assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()));
}

@Test
void testCalculateTax() {
/**
* Purchase tax is 0
*/
BigDecimal expectedTax = BigDecimal.ZERO;
assertEquals(0, expectedTax.compareTo(calculator.calculateTax()),
"Tax for purchase should be zero");
assertEquals(0, BigDecimal.ZERO.compareTo(calculator.calculateTax()));
}

@Test
void testCalculateTotal() {
/**
* Total = Gross + Commission
* Total = 1000 + 10 = 1010
*/
BigDecimal expectedTotal = new BigDecimal("1010");
assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()),
"Total should be Gross + Commission + Tax");
assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,25 @@ void setUp() {

@Test
void testCalculateGross() {
/**
* Gross = Sales Price * Quantity
* 1500 = 150 * 10
*/
BigDecimal expectedGross = new BigDecimal("1500");
assertEquals(0, expectedGross.compareTo(calculator.calculateGross()),
"Gross should be salesPrice * quantity");
assertEquals(0, expectedGross.compareTo(calculator.calculateGross()));
}

@Test
void testCalculateCommission() {
/**
* Commission = Gross * Commission Rate (1%)
* Commission = 1500 * 0.01 = 15
*/
BigDecimal expectedCommission = new BigDecimal("15");
assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()),
"Commission should be 1% of Gross");
assertEquals(0, expectedCommission.compareTo(calculator.calculateCommission()));
}

@Test
void testCalculateTax() {
/**
* Tax = (Sales Price - Purchase Price) * Quantity * Tax Rate (22%)
* (150 - 100) * 10 * 0.22 = 110
*/
BigDecimal expectedTax = new BigDecimal("110");
assertEquals(0, expectedTax.compareTo(calculator.calculateTax()),
"Tax should be 22% of profit");
assertEquals(0, expectedTax.compareTo(calculator.calculateTax()));
}

@Test
void testCalculateTotal() {
/**
* Total = Gross - Commission - Tax
* Total = 1500 - 15 - 110 = 1375
*/
BigDecimal expectedTotal = new BigDecimal("1375");
assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()),
"Total should be Gross - Commission - Tax");
assertEquals(0, expectedTotal.compareTo(calculator.calculateTotal()));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package edu.ntnu.idi.idatt2003.gruppe42;
package edu.ntnu.idi.idatt2003.gruppe42.Model;

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

import edu.ntnu.idi.idatt2003.gruppe42.Model.Exchange;
import edu.ntnu.idi.idatt2003.gruppe42.Model.Stock;
import java.math.BigDecimal;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -19,6 +18,13 @@ void setUp() {
exchange = new Exchange("NYSE", List.of(stock));
}

@Test
void stockManagementTest() {
assertTrue(exchange.hasStock("AAPL"));
assertFalse(exchange.hasStock("GOOG"));
assertEquals("AAPL", exchange.getStock("AAPL").getSymbol());
}

@Test
void findStocksTest() {
List<Stock> result = exchange.findStocks("Apple Inc.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

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

import edu.ntnu.idi.idatt2003.gruppe42.Model.Exceptions.InsufficientFunds;
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());
}

@Test
void withdrawInsufficientFundsTest() {
assertThrows(InsufficientFunds.class, () -> player.withdrawMoney(new BigDecimal("15000")));
}

@Test
void getNameTest() {
assertEquals("John Doe", player.getName());
}

@Test
void getPortfolioTest() {
assertNotNull(player.getPortfolio());
}

@Test
void getTransactionArchiveTest() {
assertNotNull(player.getTransactionArchive());
}

@Test
void getMoneyTest() {
assertEquals(new BigDecimal("10000"), player.getMoney());
}
@Test
void withdrawExactAmountTest() {
player.withdrawMoney(new BigDecimal("10000"));
assertEquals(BigDecimal.ZERO, player.getMoney());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package edu.ntnu.idi.idatt2003.gruppe42;
package edu.ntnu.idi.idatt2003.gruppe42.Model;

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 java.math.BigDecimal;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -24,15 +21,18 @@ void setUp() {

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

@Test
void removeShareTest() {
portfolio.addShare(share);
boolean result = portfolio.removeShare(share);
assertTrue(portfolio.removeShare(share));
assertFalse(portfolio.contains(share));
assertFalse(portfolio.removeShare(null));
assertFalse(portfolio.removeShare(share));
}

@Test
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ShareTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

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

import java.math.BigDecimal;
import org.junit.jupiter.api.Test;

public class ShareTest {
@Test
void sharePropertiesTest() {
Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100"));
BigDecimal quantity = new BigDecimal("10");
BigDecimal purchasePrice = new BigDecimal("90");
Share share = new Share(stock, quantity, purchasePrice);

assertEquals(stock, share.getStock());
assertEquals(0, quantity.compareTo(share.getQuantity()));
assertEquals(0, purchasePrice.compareTo(share.getPurchasePrice()));
}
}
27 changes: 27 additions & 0 deletions src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package edu.ntnu.idi.idatt2003.gruppe42.Model;

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

import java.math.BigDecimal;
import org.junit.jupiter.api.Test;

public class StockTest {
@Test
void stockPropertiesTest() {
BigDecimal price = new BigDecimal("100");
Stock stock = new Stock("AAPL", "Apple Inc.", price);

assertEquals("AAPL", stock.getSymbol());
assertEquals("Apple Inc.", stock.getCompany());
assertEquals(0, price.compareTo(stock.getSalesPrice()));
}

@Test
void addNewPriceTest() {
Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100"));
BigDecimal newPrice = new BigDecimal("110");
stock.addNewSalesPrices(newPrice);

assertEquals(0, newPrice.compareTo(stock.getSalesPrice()));
}
}
Loading

0 comments on commit 2341911

Please sign in to comment.