-
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.
this is the whole purchase class
- Loading branch information
EspenTinius
committed
Feb 14, 2026
1 parent
c41e279
commit d993590
Showing
1 changed file
with
64 additions
and
2 deletions.
There are no files selected for viewing
66 changes: 64 additions & 2 deletions
66
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculator.java
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,5 +1,67 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe; | ||
|
|
||
| public class PurchaseCalculator { | ||
|
|
||
| import java.math.BigDecimal; | ||
|
|
||
|
|
||
| /** | ||
| * Calculator for purchase transactions. | ||
| * <p> | ||
| * Uses the share's purchase price and quantity to calculate: | ||
| * gross value, commission, tax and total cost. | ||
| * </p> | ||
| */ | ||
| public class PurchaseCalculator implements TransactionCalculator { | ||
|
|
||
| private static final BigDecimal COMMISSION_RATE = new BigDecimal("0.005"); // 0.5% | ||
|
|
||
| private final BigDecimal purchasePrice; | ||
| private final BigDecimal quantity; | ||
|
|
||
| /** | ||
| * Creates a new {@code PurchaseCalculator} based on a share. | ||
| * | ||
| * @param share the share to base calculations on | ||
| */ | ||
| public PurchaseCalculator(Share share) { | ||
| this.purchasePrice = share.getPurchasePrice(); | ||
| this.quantity = share.getQuantity(); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * Gross value = purchasePrice * quantity. | ||
| */ | ||
| @Override | ||
| public BigDecimal calculateGross() { | ||
| return purchasePrice.multiply(quantity); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * Commission = 0.5% of gross value. | ||
| */ | ||
| @Override | ||
| public BigDecimal calculateCommission() { | ||
| return calculateGross().multiply(COMMISSION_RATE); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * No tax on purchases. | ||
| */ | ||
| @Override | ||
| public BigDecimal calculateTax() { | ||
| return BigDecimal.ZERO; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * Total cost = gross + commission + tax. | ||
| */ | ||
| @Override | ||
| public BigDecimal calculateTotal() { | ||
| return calculateGross() | ||
| .add(calculateCommission()) | ||
| .add(calculateTax()); | ||
| } | ||
| } |