-
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,32 +1,36 @@ | ||
| package Model; | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
| /** | ||
| * Purchase class whenever the player makes a purchase. | ||
| */ | ||
| public class Purchase extends Transaction { | ||
| public Purchase(Share share, int week) { | ||
| super(share, week, new PurchaseCalculator(share)); | ||
| } | ||
| public Purchase(Share share, int week) { | ||
| super(share, week, new PurchaseCalculator(share)); | ||
| } | ||
|
|
||
| @Override | ||
| public void commit(Player player) { | ||
| if (player == null) { | ||
| throw new IllegalArgumentException("Player cannot be null"); | ||
| } | ||
| if (isCommitted()) { | ||
| throw new IllegalStateException("Purchase has already been committed"); | ||
| } | ||
| @Override | ||
| public void commit(Player player) { | ||
| if (player == null) { | ||
| throw new IllegalArgumentException("Player cannot be null"); | ||
| } | ||
| if (isCommitted()) { | ||
| throw new IllegalStateException("Purchase has already been committed"); | ||
| } | ||
|
|
||
| BigDecimal price = this.getCalculator().calculateTotal(); | ||
| BigDecimal price = this.getCalculator().calculateTotal(); | ||
|
|
||
| if (player.getMoney().compareTo(price) < 0) { | ||
| throw new IllegalStateException( | ||
| "Insufficient funds: required " + price + ", available " + player.getMoney() | ||
| ); | ||
| } | ||
| if (player.getMoney().compareTo(price) < 0) { | ||
| throw new IllegalStateException( | ||
| "Insufficient funds: required " + price + ", available " + player.getMoney() | ||
| ); | ||
| } | ||
|
|
||
| player.withdrawMoney(price); | ||
| player.getPortfolio().addShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
| player.withdrawMoney(price); | ||
| player.getPortfolio().addShare(this.getShare()); | ||
| player.getTransactionArchive().add(this); | ||
|
|
||
| this.committed = true; | ||
| } | ||
| this.committed = true; | ||
| } | ||
| } |