Skip to content

Commit

Permalink
SaleCalculator documented
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenTinius committed Feb 19, 2026
1 parent b8a404e commit 0ee47c6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,57 @@

import java.math.BigDecimal;

/**
* Calculator for sale transactions.
* <p>
* Calculates gross value, commission, tax and total sale value
* based on the share's purchase price, current sales price and quantity.
* </p>
*/
public class SaleCalculator implements TransactionCalculator {

private static final BigDecimal COMMISSION_RATE = new BigDecimal("0.01");
private static final BigDecimal TAX_RATE = new BigDecimal("0.30");
private static final BigDecimal COMMISSION_RATE = new BigDecimal("0.01"); // 1%
private static final BigDecimal TAX_RATE = new BigDecimal("0.30"); // 30%

private final BigDecimal purchasePrice;
private final BigDecimal salesPrice;
private final BigDecimal quantity;

/**
* Creates a new {@code SaleCalculator} based on a share.
*
* @param share the share to base calculations on
*/
public SaleCalculator(Share share) {
this.purchasePrice = share.getPurchasePrice();
this.salesPrice = share.getStock().getSalesPrice();
this.quantity = share.getQuantity();
}

/**
* {@inheritDoc}
* Gross value = salesPrice * quantity.
*/
@Override
public BigDecimal calculateGross() {
return salesPrice.multiply(quantity);
}

/**
* {@inheritDoc}
* Commission = 1% of gross value.
*/
@Override
public BigDecimal calculateCommission() {
return calculateGross().multiply(COMMISSION_RATE);
}

/**
* {@inheritDoc}
* Tax = 30% of profit.
* Profit = gross - commission - (purchasePrice * quantity).
* If profit is negative or zero, tax is zero.
*/
@Override
public BigDecimal calculateTax() {
BigDecimal purchaseCost = purchasePrice.multiply(quantity);
Expand All @@ -39,6 +65,10 @@ public BigDecimal calculateTax() {
: BigDecimal.ZERO;
}

/**
* {@inheritDoc}
* Total value = gross - commission - tax.
*/
@Override
public BigDecimal calculateTotal() {
return calculateGross()
Expand Down

0 comments on commit 0ee47c6

Please sign in to comment.