-
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.
- Loading branch information
Showing
1 changed file
with
26 additions
and
22 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,35 @@ | ||
| package Model; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * Sale class for whenever the player makes a sale. | ||
| */ | ||
| public class Sale extends Transaction { | ||
| public Sale(Share share, int week) { | ||
| super(share, week, new SaleCalculator(share)); | ||
| } | ||
| public Sale(Share share, int week) { | ||
| super(share, week, new SaleCalculator(share)); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(Player player) { | ||
| 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() | ||
| ); | ||
| } | ||
| @Override | ||
| public void commit(Player player) { | ||
| 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(); | ||
| BigDecimal price = getCalculator().calculateTotal(); | ||
|
|
||
| player.addMoney(price); | ||
| player.getPortfolio().removeShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
| player.addMoney(price); | ||
| player.getPortfolio().removeShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
|
|
||
| this.committed = true; | ||
| } | ||
| this.committed = true; | ||
| } | ||
| } |