Skip to content

Commit

Permalink
Update Sale class with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 428fde3 commit 588cd2d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/Model/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ public Sale(Share share, int week) {
super(share, week, new SaleCalculator(share));
}

@Override
public void commit(Player player) {
BigDecimal price = getCalculator().calculateTotal();

if (isCommitted() || !player.getPortfolio().contains(this.getShare())) {
return;
if (player == null) {
throw new IllegalArgumentException("Player cannot be null");
}
if (isCommitted()) {
throw new IllegalStateException("Sale has already been committed");
}
if (!player.getPortfolio().contains(this.getShare())) {
throw new IllegalStateException(
"Share not found in player's portfolio: " + this.getShare().getStock().getSymbol()
);
}

BigDecimal price = getCalculator().calculateTotal();

player.addMoney(price);
player.getPortfolio().removeShare(this.getShare());
Expand Down

0 comments on commit 588cd2d

Please sign in to comment.