Skip to content

Commit

Permalink
Feat: Updated exchange and test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyah committed May 26, 2026
1 parent f7ef95b commit 12bb7bc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,19 @@ public Transaction sell(BigDecimal amount,
throw new IllegalArgumentException("Player does not own any shares of this stock!");
}

Share ownedPosition = matchingShares.getFirst();
BigDecimal totalOwned = ownedPosition.getQuantity();
BigDecimal ownedPurchasePrice = ownedPosition.getPurchasePrice();
BigDecimal totalOwned = matchingShares.stream()
.map(Share::getQuantity)
.reduce(BigDecimal.ZERO, BigDecimal::add);

if (amount.compareTo(totalOwned) > 0) {
amount = totalOwned;
}

Stock stock = ownedPosition.getStock();
Share shareToSell = new Share(stock, amount, ownedPurchasePrice);
Stock stock = matchingShares.getFirst().getStock();

BigDecimal oldestPurchasePrice = matchingShares.getFirst().getPurchasePrice();

Share shareToSell = new Share(stock, amount, oldestPurchasePrice);

Transaction sale = TransactionFactory.createTransaction(
TransactionType.SALE, shareToSell, getWeek()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,20 @@ void buyThrowsExceptionOnIllegalArguments() {

@Test
void sellReturnsSaleAndAddsMoneyToPlayer() {
Share share =
new Share(appleStock, new BigDecimal("2"),
appleStock.getSalesPrice());

Transaction transaction = testExchange.sell(share, testPlayer);
BigDecimal sellAmount = new BigDecimal("2");
Share share = new Share(appleStock, sellAmount, appleStock.getSalesPrice());
testPlayer.getPortfolio().addShare(share);

Transaction transaction = testExchange.sell(sellAmount, appleStock.getSymbol(), testPlayer);
TransactionCalculator calculator = new SaleCalculator(share);
BigDecimal expectedPlayerMoney =
testPlayer.getStartingMoney().add(calculator.calculateTotal());


assertInstanceOf(Sale.class, transaction);
assertEquals(1, transaction.getWeek());
assertSame(share, transaction.getShare());
assertEquals(share.getQuantity(), transaction.getShare().getQuantity());
assertEquals(share.getPurchasePrice(), transaction.getShare().getPurchasePrice());
assertEquals(expectedPlayerMoney, testPlayer.getMoney());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,16 @@ void getNetWorthCalculatesCorrectlyForSales() {
Stock stock = new Stock("AAPL", "Apple inc.,", new BigDecimal("100.00"));
Share share = new Share(stock, new BigDecimal("1"), new BigDecimal("100.00"));

BigDecimal expectedNetWorth = testPlayer.getNetWorth();
Transaction transaction = TransactionFactory.createTransaction(TransactionType.SALE, share, 1);
testPlayer.getPortfolio().addShare(share);
BigDecimal baselineNetWorth = testPlayer.getNetWorth();

Transaction transaction = TransactionFactory.createTransaction(TransactionType.SALE, share, 1);
testPlayer.handleTransaction(transaction);

BigDecimal actualNetWorth = testPlayer.getNetWorth();
expectedNetWorth = expectedNetWorth.add(transaction.getCalculator().calculateTotal());

BigDecimal commissionFee = transaction.getCalculator().calculateCommission();
BigDecimal expectedNetWorth = baselineNetWorth.subtract(commissionFee);

assertEquals(expectedNetWorth, actualNetWorth);
assertEquals(actualNetWorth.floatValue(),
Expand Down

0 comments on commit 12bb7bc

Please sign in to comment.