diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exceptions/InsufficientFunds.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exceptions/InsufficientFunds.java new file mode 100644 index 0000000..8a2f1be --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Exceptions/InsufficientFunds.java @@ -0,0 +1,7 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Model.Exceptions; + +public class InsufficientFunds extends RuntimeException { + public InsufficientFunds(String message) { + super(message); + } +} 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 9fb9000..3cf9f1c 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 @@ -37,6 +37,9 @@ public Exchange(String name, List stocks) { this.stockMap = new HashMap(); this.random = new Random(); this.stocks = stocks; + for (Stock stock : stocks) { + stockMap.put(stock.getSymbol(), stock); + } } public String getName() { diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java index d505c60..14beaf7 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Player.java @@ -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; @@ -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); } - - } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Purchase.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Purchase.java index 56b06cc..c442fec 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Purchase.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Purchase.java @@ -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); } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Sale.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Sale.java index 3ad873d..6367cf9 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Sale.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Sale.java @@ -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); } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Transaction.java b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Transaction.java index 67edcaf..0052939 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Transaction.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/Transaction.java @@ -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; @@ -34,4 +34,8 @@ public boolean isCommitted() { } public void commit(Player player) {} + + public void setCommitted(boolean committed) { + this.committed = committed; + } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/MillionsTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/MillionsTest.java index b96a92c..00f144d 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/MillionsTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/MillionsTest.java @@ -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[]{})); + } +} \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/PurchaseCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/PurchaseCalculatorTest.java index d9c62d6..6870d4c 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/PurchaseCalculatorTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/PurchaseCalculatorTest.java @@ -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; @@ -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())); } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/SaleCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/SaleCalculatorTest.java index aa2c8c7..4519596 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/SaleCalculatorTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Calculator/SaleCalculatorTest.java @@ -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())); } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ExchangeTest.java similarity index 76% rename from src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java rename to src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ExchangeTest.java index c79e9c1..c8a9d4f 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/ExchangeTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ExchangeTest.java @@ -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; @@ -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 result = exchange.findStocks("Apple Inc."); diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java new file mode 100644 index 0000000..3e2da0d --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PlayerTest.java @@ -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()); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PortfolioTest.java similarity index 74% rename from src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java rename to src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PortfolioTest.java index ca1b060..6089bed 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PortfolioTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/PortfolioTest.java @@ -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; @@ -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 diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ShareTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ShareTest.java new file mode 100644 index 0000000..68ecfec --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/ShareTest.java @@ -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())); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockTest.java new file mode 100644 index 0000000..db17efb --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/StockTest.java @@ -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())); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/TransactionArchiveTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/TransactionArchiveTest.java new file mode 100644 index 0000000..557bbec --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/Model/Transaction/TransactionArchiveTest.java @@ -0,0 +1,64 @@ +package edu.ntnu.idi.idatt2003.gruppe42.Model.Transaction; + +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.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; + +public class TransactionArchiveTest { + private TransactionArchive archive; + private Purchase purchase; + private Sale sale; + + @BeforeEach + void setUp() { + archive = new TransactionArchive(); + Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100")); + Share share = new Share(stock, new BigDecimal("10"), new BigDecimal("90")); + purchase = new Purchase(share, 1); + sale = new Sale(share, 2); + } + + @Test + void addTest() { + assertTrue(archive.isEmpty()); + assertTrue(archive.add(purchase)); + assertFalse(archive.isEmpty()); + assertFalse(archive.add(null)); + } + + @Test + void getTransactionsByWeekTest() { + archive.add(purchase); + archive.add(sale); + + assertEquals(1, archive.getTransactions(1).size()); + assertEquals(1, archive.getTransactions(2).size()); + assertEquals(0, archive.getTransactions(3).size()); + } + + @Test + void getPurchasesAndSalesTest() { + archive.add(purchase); + archive.add(sale); + + assertEquals(1, archive.getPurchases(1).size()); + assertEquals(0, archive.getPurchases(2).size()); + assertEquals(0, archive.getSales(1).size()); + assertEquals(1, archive.getSales(2).size()); + } + + @Test + void countDistinctWeeksTest() { + archive.add(purchase); // Week 1 + archive.add(sale); // Week 2 + archive.add(new Purchase(purchase.getShare(), 1)); // Week 1 + + assertEquals(2, archive.countDistinctWeeks()); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java b/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java deleted file mode 100644 index 9ea3a0b..0000000 --- a/src/test/java/edu/ntnu/idi/idatt2003/gruppe42/PlayerTest.java +++ /dev/null @@ -1,29 +0,0 @@ -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()); - } -}