Skip to content

Commit

Permalink
Added Checkstyle to Purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
elisab3 committed May 24, 2026
1 parent 2774464 commit 6803d26
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/main/java/Model/Purchase.java
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;
}
}

0 comments on commit 6803d26

Please sign in to comment.