Skip to content

Commit

Permalink
Merge branch 'main' into 50-file-managment-system
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed Mar 18, 2026
2 parents cfedec2 + cb771f0 commit 67917ba
Show file tree
Hide file tree
Showing 9 changed files with 499 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Exchange.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public Transaction buy(String symbol, BigDecimal quantity, Player player) {
Stock stock = stockMap.get(symbol);
Share share = new Share(stock, quantity, stock.getSalesPrice());
TransactionCalculator calculator = new PurchaseCalculator(share);
player.withdrawMoney(calculator.calculateTotal());
return new Purchase(share, week, calculator);
}

public Transaction sell(Share share, Player player) {
TransactionCalculator calculator = new SaleCalculator(share);
player.addMoney(calculator.calculateTotal());
return new Sale(share, week, calculator);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Player.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.ntnu.idi.idatt2003.g40.mappe;

import java.math.BigDecimal;
import java.util.Objects;

public class Player {

Expand Down
95 changes: 92 additions & 3 deletions src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ExchangeTest.java
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 src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PlayerTest.java
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 src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PortfolioTest.java
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));
}
}
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());
}
}
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());
}
}
Loading

0 comments on commit 67917ba

Please sign in to comment.