-
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.
Merge pull request #45 from Team-40-IDATT2003/enhancement/31-salecalc…
…ulator-class Enhancement/31 salecalculator class
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| 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"); // 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); | ||
| BigDecimal profit = calculateGross() | ||
| .subtract(calculateCommission()) | ||
| .subtract(purchaseCost); | ||
|
|
||
| return profit.signum() > 0 | ||
| ? profit.multiply(TAX_RATE) | ||
| : BigDecimal.ZERO; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * Total value = gross - commission - tax. | ||
| */ | ||
| @Override | ||
| public BigDecimal calculateTotal() { | ||
| return calculateGross() | ||
| .subtract(calculateCommission()) | ||
| .subtract(calculateTax()); | ||
| } | ||
| } |