-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 50-file-managment-system
- Loading branch information
Showing
9 changed files
with
499 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 92 additions & 3 deletions
95
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,92 @@ | ||
| public class ExchangeTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class ExchangeTest { | ||
|
|
||
| @Test | ||
| void constructorSetsNameWeekAndStocksCorrectly() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
|
|
||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
|
|
||
| assertEquals("NASDAQ", exchange.getName()); | ||
| assertEquals(1, exchange.getWeek()); | ||
| assertTrue(exchange.hasStock("AAPL")); | ||
| assertTrue(exchange.hasStock("TSLA")); | ||
| } | ||
|
|
||
| @Test | ||
| void getStockReturnsCorrectStock() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
|
|
||
| Stock result = exchange.getStock("AAPL"); | ||
|
|
||
| assertSame(apple, result); | ||
| } | ||
|
|
||
| @Test | ||
| void findStocksReturnsMatchingStocksBySymbolOrCompany() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple, tesla)); | ||
|
|
||
| List<Stock> resultBySymbol = exchange.findStocks("AAP"); | ||
| List<Stock> resultByCompany = exchange.findStocks("tes"); | ||
|
|
||
| assertEquals(1, resultBySymbol.size()); | ||
| assertTrue(resultBySymbol.contains(apple)); | ||
|
|
||
| assertEquals(1, resultByCompany.size()); | ||
| assertTrue(resultByCompany.contains(tesla)); | ||
| } | ||
|
|
||
| @Test | ||
| void buyReturnsPurchaseAndWithdrawsMoneyFromPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
|
|
||
| Transaction transaction = exchange.buy("AAPL", new BigDecimal("2"), player); | ||
|
|
||
| assertInstanceOf(Purchase.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertEquals(new BigDecimal("2"), transaction.getShare().getQuantity()); | ||
| assertEquals(new BigDecimal("100"), transaction.getShare().getPurchasePrice()); | ||
| assertEquals(new BigDecimal("799.000"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void sellReturnsSaleAndAddsMoneyToPlayer() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
| Share share = new Share(apple, new BigDecimal("2"), new BigDecimal("100")); | ||
|
|
||
| Transaction transaction = exchange.sell(share, player); | ||
|
|
||
| assertInstanceOf(Sale.class, transaction); | ||
| assertEquals(1, transaction.getWeek()); | ||
| assertSame(share, transaction.getShare()); | ||
| assertEquals(new BigDecimal("1267.9000"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void advanceIncreasesWeekAndUpdatesStockPrice() { | ||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("100")); | ||
| Exchange exchange = new Exchange("NASDAQ", List.of(apple)); | ||
|
|
||
| BigDecimal oldPrice = apple.getSalesPrice(); | ||
| exchange.advance(); | ||
|
|
||
| assertEquals(2, exchange.getWeek()); | ||
| assertNotEquals(oldPrice, apple.getSalesPrice()); | ||
| } | ||
| } |
51 changes: 48 additions & 3 deletions
51
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,48 @@ | ||
| public class PlayerTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class PlayerTest { | ||
|
|
||
| @Test | ||
| void constructorSetsNameMoneyPortfolioAndArchive() { | ||
| Player player = new Player("Alice", new BigDecimal("1000")); | ||
|
|
||
| assertEquals("Alice", player.getName()); | ||
| assertEquals(new BigDecimal("1000"), player.getMoney()); | ||
| assertNotNull(player.getPortfolio()); | ||
| assertNotNull(player.getTransactionArchive()); | ||
| } | ||
|
|
||
| @Test | ||
| void addMoneyIncreasesBalance() { | ||
| Player player = new Player("Bob", new BigDecimal("500")); | ||
|
|
||
| player.addMoney(new BigDecimal("200")); | ||
|
|
||
| assertEquals(new BigDecimal("700"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void withdrawMoneyDecreasesBalance() { | ||
| Player player = new Player("Charlie", new BigDecimal("500")); | ||
|
|
||
| player.withdrawMoney(new BigDecimal("150")); | ||
|
|
||
| assertEquals(new BigDecimal("350"), player.getMoney()); | ||
| } | ||
|
|
||
| @Test | ||
| void addAndWithdrawMoneyUpdateBalanceCorrectly() { | ||
| Player player = new Player("Dana", new BigDecimal("1000")); | ||
|
|
||
| player.addMoney(new BigDecimal("250")); | ||
| player.withdrawMoney(new BigDecimal("300")); | ||
|
|
||
| assertEquals(new BigDecimal("950"), player.getMoney()); | ||
| } | ||
| } |
83 changes: 80 additions & 3 deletions
83
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,80 @@ | ||
| public class PortfolioTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class PortfolioTest { | ||
|
|
||
| @Test | ||
| void addShareAddsShareToPortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
|
|
||
| boolean result = portfolio.addShare(share); | ||
|
|
||
| assertTrue(result); | ||
| assertTrue(portfolio.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void removeShareRemovesShareFromPortfolio() { | ||
| Portfolio portfolio = new Portfolio(); | ||
| Stock stock = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
| Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("200")); | ||
|
|
||
| portfolio.addShare(share); | ||
| boolean result = portfolio.removeShare(share); | ||
|
|
||
| assertTrue(result); | ||
| assertFalse(portfolio.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void getSharesReturnsAllShares() { | ||
| Portfolio portfolio = new Portfolio(); | ||
|
|
||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("3"), new BigDecimal("150")); | ||
|
|
||
| portfolio.addShare(share); | ||
|
|
||
| List<Share> shares = portfolio.getShares(); | ||
|
|
||
| assertEquals(1, shares.size()); | ||
| assertTrue(shares.contains(share)); | ||
| } | ||
|
|
||
| @Test | ||
| void getSharesWithSymbolReturnsMatchingShares() { | ||
| Portfolio portfolio = new Portfolio(); | ||
|
|
||
| Stock apple = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Stock tesla = new Stock("TSLA", "Tesla", new BigDecimal("200")); | ||
|
|
||
| Share appleShare = new Share(apple, new BigDecimal("1"), new BigDecimal("150")); | ||
| Share teslaShare = new Share(tesla, new BigDecimal("1"), new BigDecimal("200")); | ||
|
|
||
| portfolio.addShare(appleShare); | ||
| portfolio.addShare(teslaShare); | ||
|
|
||
| List<Share> result = portfolio.getShares("AAPL"); | ||
|
|
||
| assertEquals(1, result.size()); | ||
| assertTrue(result.contains(appleShare)); | ||
| } | ||
|
|
||
| @Test | ||
| void containsReturnsFalseWhenShareNotPresent() { | ||
| Portfolio portfolio = new Portfolio(); | ||
|
|
||
| Stock stock = new Stock("NVDA", "Nvidia", new BigDecimal("800")); | ||
| Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("800")); | ||
|
|
||
| assertFalse(portfolio.contains(share)); | ||
| } | ||
| } |
46 changes: 44 additions & 2 deletions
46
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,45 @@ | ||
| public class PurchaseCalculatorTest { | ||
|
|
||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import java.math.BigDecimal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class PurchaseCalculatorTest { | ||
|
|
||
| @Test | ||
| void calculateGrossReturnsPurchasePriceTimesQuantity() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| PurchaseCalculator calculator = new PurchaseCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("200"), calculator.calculateGross()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateCommissionReturnsHalfPercentOfGross() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| PurchaseCalculator calculator = new PurchaseCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("1.000"), calculator.calculateCommission()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateTaxReturnsZero() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| PurchaseCalculator calculator = new PurchaseCalculator(share); | ||
|
|
||
| assertEquals(BigDecimal.ZERO, calculator.calculateTax()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateTotalReturnsGrossPlusCommission() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| PurchaseCalculator calculator = new PurchaseCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("201.000"), calculator.calculateTotal()); | ||
| } | ||
| } |
58 changes: 55 additions & 3 deletions
58
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,55 @@ | ||
| public class SaleCalculatorTest { | ||
|
|
||
| } | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class SaleCalculatorTest { | ||
|
|
||
| @Test | ||
| void calculateGrossReturnsSalesPriceTimesQuantity() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| SaleCalculator calculator = new SaleCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("300"), calculator.calculateGross()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateCommissionReturnsOnePercentOfGross() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| SaleCalculator calculator = new SaleCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("3.00"), calculator.calculateCommission()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateTaxReturnsThirtyPercentOfProfitWhenProfitIsPositive() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| SaleCalculator calculator = new SaleCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("29.1000"), calculator.calculateTax()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateTaxReturnsZeroWhenProfitIsZeroOrNegative() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("90")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| SaleCalculator calculator = new SaleCalculator(share); | ||
|
|
||
| assertEquals(BigDecimal.ZERO, calculator.calculateTax()); | ||
| } | ||
|
|
||
| @Test | ||
| void calculateTotalReturnsGrossMinusCommissionMinusTax() { | ||
| Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); | ||
| Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); | ||
| SaleCalculator calculator = new SaleCalculator(share); | ||
|
|
||
| assertEquals(new BigDecimal("267.9000"), calculator.calculateTotal()); | ||
| } | ||
| } |
Oops, something went wrong.