diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Purchase.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Purchase.java index 717c73c..f042cb0 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Purchase.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Purchase.java @@ -6,16 +6,19 @@ *
Extends {@link Transaction}
* * */ -public class Purchase extends Transaction { +public final class Purchase extends Transaction { /** * Constructor. * * @param share the {@link Share} object to purchase. * @param week the week to purchase during. - * @param calculator the {@link TransactionCalculator} object to calculate this purchase. + * @param calculator the {@link TransactionCalculator} + * object to calculate this purchase. * */ - public Purchase(final Share share, final int week, final TransactionCalculator calculator) { + public Purchase(final Share share, + final int week, + final TransactionCalculator calculator) { super(share, week, calculator); } @@ -26,6 +29,6 @@ public Purchase(final Share share, final int week, final TransactionCalculator c * */ @Override public void commit(final Player player) { - commited = true; + setCommitted(true); } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculator.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculator.java index 6cffe1c..8e3cf16 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculator.java @@ -2,66 +2,75 @@ import java.math.BigDecimal; - /** * Calculator for purchase transactions. - *- * Uses the share's purchase price and quantity to calculate: - * gross value, commission, tax and total cost. - *
+ * + *Uses the share's purchase price and quantity to calculate: + * gross value, commission, tax and total cost.
*/ -public class PurchaseCalculator implements TransactionCalculator { +public final class PurchaseCalculator implements TransactionCalculator { + + /** + * The constant commission rate for purchases. + * */ + private static final BigDecimal COMMISSION_RATE + = new BigDecimal("0.005"); // 0.5% - private static final BigDecimal COMMISSION_RATE = new BigDecimal("0.005"); // 0.5% + /** + * The price of the purchase. + * */ + private final BigDecimal purchasePrice; - private final BigDecimal purchasePrice; - private final BigDecimal quantity; + /** + * The quantity bought. + * */ + 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(); - } + /** + * Creates a new {@code PurchaseCalculator} based on a share. + * + * @param share the share to base calculations on + */ + public PurchaseCalculator(final Share share) { + this.purchasePrice = share.getPurchasePrice(); + this.quantity = share.getQuantity(); + } - /** - * {@inheritDoc} - * Gross value = purchasePrice * quantity. - */ - @Override - public BigDecimal calculateGross() { - return purchasePrice.multiply(quantity); - } + /** + * {@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} + * 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} + * 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()); - } + /** + * {@inheritDoc} + * Total cost = gross + commission + tax. + */ + @Override + public BigDecimal calculateTotal() { + return calculateGross() + .add(calculateCommission()) + .add(calculateTax()); + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Sale.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Sale.java index 8fcf55b..50cbb24 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Sale.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Sale.java @@ -6,16 +6,19 @@ *Extends {@link Transaction}
* * */ -public class Sale extends Transaction { +public final class Sale extends Transaction { /** * Constructor. * * @param share the {@link Share} object to purchase. * @param week the week to purchase during. - * @param calculator the {@link TransactionCalculator} object to calculate this purchase. + * @param calculator the {@link TransactionCalculator} + * object to calculate this purchase. * */ - public Sale(final Share share, final int week, final TransactionCalculator calculator) { + public Sale(final Share share, + final int week, + final TransactionCalculator calculator) { super(share, week, calculator); } @@ -26,6 +29,6 @@ public Sale(final Share share, final int week, final TransactionCalculator calcu * */ @Override public void commit(final Player player) { - commited = true; + setCommitted(true); } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculator.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculator.java index ecc4242..8fb6227 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculator.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculator.java @@ -4,75 +4,94 @@ /** * Calculator for sale transactions. - *- * Calculates gross value, commission, tax and total sale value - * based on the share's purchase price, current sales price and quantity. - *
+ * + *Calculates gross value, commission, tax and total sale value + * based on the share's purchase price, current sales price and quantity.
*/ -public class SaleCalculator implements TransactionCalculator { +public final 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% + /** + * The constant commission rate for sales. + * */ + private static final BigDecimal COMMISSION_RATE = + new BigDecimal("0.01"); // 1% - private final BigDecimal purchasePrice; - private final BigDecimal salesPrice; - private final BigDecimal quantity; + /** + * The constant tax rates for sales. + * */ + private static final BigDecimal TAX_RATE = + new BigDecimal("0.30"); // 30% - /** - * 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(); - } + /** + * The purchase price of the share. + * */ + private final BigDecimal purchasePrice; - /** - * {@inheritDoc} - * Gross value = salesPrice * quantity. - */ - @Override - public BigDecimal calculateGross() { - return salesPrice.multiply(quantity); - } + /** + * The sale price of the share. + * */ + private final BigDecimal salesPrice; - /** - * {@inheritDoc} - * Commission = 1% of gross value. - */ - @Override - public BigDecimal calculateCommission() { - return calculateGross().multiply(COMMISSION_RATE); - } + /** + * The quantity the share represents. + * */ + private final BigDecimal quantity; - /** - * {@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); + /** + * Creates a new {@code SaleCalculator} based on a share. + * + * @param share the share to base calculations on + */ + public SaleCalculator(final Share share) { + this.purchasePrice = share.getPurchasePrice(); + this.salesPrice = share.getStock().getSalesPrice(); + this.quantity = share.getQuantity(); + } - return profit.signum() > 0 - ? profit.multiply(TAX_RATE) - : BigDecimal.ZERO; - } + /** + * {@inheritDoc} + * Gross value = salesPrice * quantity. + */ + @Override + public BigDecimal calculateGross() { + return salesPrice.multiply(quantity); + } - /** - * {@inheritDoc} - * Total value = gross - commission - tax. - */ - @Override - public BigDecimal calculateTotal() { - return calculateGross() - .subtract(calculateCommission()) - .subtract(calculateTax()); - } -} \ No newline at end of file + /** + * {@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()); + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Share.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Share.java index d27478a..0373bd4 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Share.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Share.java @@ -4,53 +4,66 @@ /** * Represents a share owned by a player. - *- * A share contains information about which stock was purchased, - * the quantity purchased, and the purchase price. - *
+ * + *A share contains information about which stock was purchased, + * the quantity purchased, and the purchase price.
*/ -public class Share { - private final Stock stock; - private final BigDecimal quantity; - private final BigDecimal purchasePrice; - - /** - * Creates a new {@code Share}. - * - * @param stock the stock that was purchased - * @param quantity the quantity purchased - * @param purchasePrice the price per unit at purchase time - */ - public Share(Stock stock, BigDecimal quantity, BigDecimal purchasePrice) { - this.stock = stock; - this.quantity = quantity; - this.purchasePrice = purchasePrice; - } - - /** - * Returns the stock associated with this share. - * - * @return the stock - */ - public Stock getStock() { - return stock; - } - - /** - * Returns the quantity of the stock owned. - * - * @return the quantity - */ - public BigDecimal getQuantity() { - return quantity; - } - - /** - * Returns the purchase price per unit. - * - * @return the purchase price - */ - public BigDecimal getPurchasePrice() { - return purchasePrice; - } +public final class Share { + + /** + * The stock this share is associated with. + * */ + private final Stock stock; + + /** + * The quantity of this share. + * */ + private final BigDecimal quantity; + + /** + * The purchase price of this share. + * */ + private final BigDecimal purchasePrice; + + /** + * Creates a new {@code Share}. + * + * @param stock the stock that was purchased + * @param quantity the quantity purchased + * @param purchasePrice the price per unit at purchase time + */ + public Share(final Stock stock, + final BigDecimal quantity, + final BigDecimal purchasePrice) { + this.stock = stock; + this.quantity = quantity; + this.purchasePrice = purchasePrice; + } + + /** + * Returns the stock associated with this share. + * + * @return the stock + */ + public Stock getStock() { + return stock; + } + + /** + * Returns the quantity of the stock owned. + * + * @return the quantity + */ + public BigDecimal getQuantity() { + return quantity; + } + + /** + * Returns the purchase price per unit. + * + * @return the purchase price + */ + public BigDecimal getPurchasePrice() { + return purchasePrice; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Transaction.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Transaction.java index 78156de..8da798a 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Transaction.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/Transaction.java @@ -6,19 +6,32 @@ *Used for transactions
*/ public abstract class Transaction { + /** + * Flag used to determine whether this transaction has been committed or not. + * */ + private boolean committed = false; + + /** + * The share this transaction is about. + * */ private final Share share; + /** + * The week this transaction takes place. + * */ private final int week; + /** + * The calculator this transaction uses. + * */ private final TransactionCalculator calculator; - protected boolean commited = false; - /** * Creates a new {@code Transaction} with a share, week and calculator. * * @param share the share to perform the transaction on * @param week the current week to perform the transaction under + * @param calculator the {@link TransactionCalculator} to use. */ protected Transaction(final Share share, final int week, @@ -58,9 +71,22 @@ public TransactionCalculator getCalculator() { * * @return commited*/ public boolean isCommited() { - return commited; + return committed; } - /** Commits the transaction. */ + /** + * Commits the transaction. + * + * @param player The player that performed the transaction. + * */ public abstract void commit(Player player); + + /** + * Sets the committed flag. + * + * @param value the value to set the committed flag to. + * */ + protected void setCommitted(final boolean value) { + committed = value; + } } diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchive.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchive.java index c09cec6..83bb7cc 100644 --- a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchive.java +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchive.java @@ -6,96 +6,104 @@ /** * Stores completed transactions. */ -public class TransactionArchive { +public final class TransactionArchive { - private final ListHas methods for calculating gross, commission fee, tax and total
*/ public interface TransactionCalculator { - /** Method for calculating gross transaction amount. */ + /** + * Method for calculating gross transaction amount. + * + * @return the gross money before applying tax or commission. + * */ BigDecimal calculateGross(); - /** Method for calculating commission fee for transaction amount. */ + /** + * Method for calculating commission fee for transaction amount. + * + * @return the amount of money "lost" from commission fee. + * */ BigDecimal calculateCommission(); - /** Method for calculating tax fee for transaction amount. */ + /** + * Method for calculating tax fee for transaction amount. + * + * @return the amount of money "lost" to tax. + * */ BigDecimal calculateTax(); - /** Method for calculating total amount for transaction. */ + /** + * Method for calculating total amount for transaction. + * + * @return the total money, after tax and commission fees. + * */ BigDecimal calculateTotal(); } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculatorTest.java index 4c73305..543688a 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculatorTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseCalculatorTest.java @@ -1,45 +1,45 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; -import java.math.BigDecimal; - import static org.junit.jupiter.api.Assertions.assertEquals; -class PurchaseCalculatorTest { +import java.math.BigDecimal; +import org.junit.jupiter.api.Test; + +final class PurchaseCalculatorTest { - @Test - void calculateGrossReturnsPurchasePriceTimesQuantity() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - PurchaseCalculator calculator = new PurchaseCalculator(share); + @Test + void calculateGrossReturnsPurchasePriceTimesQuantity() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + PurchaseCalculator calculator = new PurchaseCalculator(share); - assertEquals(new BigDecimal("200"), calculator.calculateGross()); - } + assertEquals(new BigDecimal("200"), calculator.calculateGross()); + } - @Test - void calculateCommissionReturnsHalfPercentOfGross() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - PurchaseCalculator calculator = new PurchaseCalculator(share); + @Test + void calculateCommissionReturnsHalfPercentOfGross() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + PurchaseCalculator calculator = new PurchaseCalculator(share); - assertEquals(new BigDecimal("1.000"), calculator.calculateCommission()); - } + assertEquals(new BigDecimal("1.000"), calculator.calculateCommission()); + } - @Test - void calculateTaxReturnsZero() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - PurchaseCalculator calculator = new PurchaseCalculator(share); + @Test + void calculateTaxReturnsZero() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + PurchaseCalculator calculator = new PurchaseCalculator(share); - assertEquals(BigDecimal.ZERO, calculator.calculateTax()); - } + assertEquals(BigDecimal.ZERO, calculator.calculateTax()); + } - @Test - void calculateTotalReturnsGrossPlusCommission() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - PurchaseCalculator calculator = new PurchaseCalculator(share); + @Test + void calculateTotalReturnsGrossPlusCommission() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + PurchaseCalculator calculator = new PurchaseCalculator(share); - assertEquals(new BigDecimal("201.000"), calculator.calculateTotal()); - } + assertEquals(new BigDecimal("201.000"), calculator.calculateTotal()); + } } diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseTest.java index 33174da..e66c84b 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/PurchaseTest.java @@ -9,15 +9,34 @@ /** * Test class for {@link Purchase}. * */ -class PurchaseTest { - - Stock testStock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); - Share testShare = new Share(testStock, new BigDecimal("10"), new BigDecimal("10")); - PurchaseCalculator testPurchaseCalculator = new PurchaseCalculator(testShare); - Player testPlayer = new Player("TestName", new BigDecimal("1000.00")); +final class PurchaseTest { + + /** + * Valid test stock. + * */ + private final Stock testStock = + new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + + /** + * Valid test share. + * */ + private final Share testShare = + new Share(testStock, new BigDecimal("10"), new BigDecimal("10")); + + /** + * Valid test purchase calculator. + * */ + private final PurchaseCalculator testPurchaseCalculator = + new PurchaseCalculator(testShare); + + /** + * Valid test player. + * */ + private final Player testPlayer = + new Player("TestName", new BigDecimal("1000.00")); @Test - void constructor_sets_values() { + void constructorSetsValues() { Purchase purchase = new Purchase(testShare, 1, testPurchaseCalculator); assertEquals(testShare, purchase.getShare()); @@ -26,7 +45,7 @@ void constructor_sets_values() { } @Test - void commit_method_sets_commit_to_true() { + void commitMethodSetsCommitToTrue() { Purchase purchase = new Purchase(testShare, 1, testPurchaseCalculator); purchase.commit(testPlayer); diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculatorTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculatorTest.java index 3b7a0c6..e8a4640 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculatorTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleCalculatorTest.java @@ -1,55 +1,54 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.math.BigDecimal; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class SaleCalculatorTest { +final class SaleCalculatorTest { - @Test - void calculateGrossReturnsSalesPriceTimesQuantity() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - SaleCalculator calculator = new SaleCalculator(share); + @Test + void calculateGrossReturnsSalesPriceTimesQuantity() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + SaleCalculator calculator = new SaleCalculator(share); - assertEquals(new BigDecimal("300"), calculator.calculateGross()); - } + assertEquals(new BigDecimal("300"), calculator.calculateGross()); + } - @Test - void calculateCommissionReturnsOnePercentOfGross() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - SaleCalculator calculator = new SaleCalculator(share); + @Test + void calculateCommissionReturnsOnePercentOfGross() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + SaleCalculator calculator = new SaleCalculator(share); - assertEquals(new BigDecimal("3.00"), calculator.calculateCommission()); - } + assertEquals(new BigDecimal("3.00"), calculator.calculateCommission()); + } - @Test - void calculateTaxReturnsThirtyPercentOfProfitWhenProfitIsPositive() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - SaleCalculator calculator = new SaleCalculator(share); + @Test + void calculateTaxReturnsThirtyPercentOfProfitWhenProfitIsPositive() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + SaleCalculator calculator = new SaleCalculator(share); - assertEquals(new BigDecimal("29.1000"), calculator.calculateTax()); - } + assertEquals(new BigDecimal("29.1000"), calculator.calculateTax()); + } - @Test - void calculateTaxReturnsZeroWhenProfitIsZeroOrNegative() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("90")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - SaleCalculator calculator = new SaleCalculator(share); + @Test + void calculateTaxReturnsZeroWhenProfitIsZeroOrNegative() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("90")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + SaleCalculator calculator = new SaleCalculator(share); - assertEquals(BigDecimal.ZERO, calculator.calculateTax()); - } + assertEquals(BigDecimal.ZERO, calculator.calculateTax()); + } - @Test - void calculateTotalReturnsGrossMinusCommissionMinusTax() { - Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); - Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); - SaleCalculator calculator = new SaleCalculator(share); + @Test + void calculateTotalReturnsGrossMinusCommissionMinusTax() { + Stock stock = new Stock("AAPL", "Apple", new BigDecimal("150")); + Share share = new Share(stock, new BigDecimal("2"), new BigDecimal("100")); + SaleCalculator calculator = new SaleCalculator(share); - assertEquals(new BigDecimal("267.9000"), calculator.calculateTotal()); - } + assertEquals(new BigDecimal("267.9000"), calculator.calculateTotal()); + } } \ No newline at end of file diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleTest.java index 12f14f7..996437a 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/SaleTest.java @@ -9,15 +9,34 @@ /** * Test class for {@link Sale}. * */ -class SaleTest { - - Stock testStock = new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); - Share testShare = new Share(testStock, new BigDecimal("10"), new BigDecimal("10")); - SaleCalculator testSaleCalculator = new SaleCalculator(testShare); - Player testPlayer = new Player("TestName", new BigDecimal("1000.00")); +final class SaleTest { + + /** + * Valid test stock. + * */ + private final Stock testStock = + new Stock("AAPL", "Apple Inc.", new BigDecimal("100.00")); + + /** + * Valid test share. + * */ + private final Share testShare = + new Share(testStock, new BigDecimal("10"), new BigDecimal("10")); + + /** + * Valid test purchase calculator. + * */ + private final PurchaseCalculator testSaleCalculator = + new PurchaseCalculator(testShare); + + /** + * Valid test player. + * */ + private final Player testPlayer = + new Player("TestName", new BigDecimal("1000.00")); @Test - void constructor_sets_values() { + void constructorSetsValues() { Sale sale = new Sale(testShare, 1, testSaleCalculator); assertEquals(testShare, sale.getShare()); @@ -26,7 +45,7 @@ void constructor_sets_values() { } @Test - void commit_method_sets_commit_to_true() { + void commitMethodSetsCommitToTrue() { Sale sale = new Sale(testShare, 1, testSaleCalculator); sale.commit(testPlayer); diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ShareTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ShareTest.java index 0a5deb1..de9d2c1 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ShareTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/ShareTest.java @@ -1,48 +1,51 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; -class ShareTest { - - @Test - void constructorStoresAllValuesCorrectly() { - Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("150.00")); - BigDecimal quantity = new BigDecimal("10"); - BigDecimal purchasePrice = new BigDecimal("145.50"); - - Share share = new Share(stock, quantity, purchasePrice); - - assertSame(stock, share.getStock()); - assertEquals(quantity, share.getQuantity()); - assertEquals(purchasePrice, share.getPurchasePrice()); - } - - @Test - void shareSupportsDecimalValues() { - Stock stock = new Stock("TSLA", "Tesla Inc.", new BigDecimal("200.00")); - - Share share = new Share( - stock, - new BigDecimal("2.5"), - new BigDecimal("198.75") - ); - - assertEquals(new BigDecimal("2.5"), share.getQuantity()); - assertEquals(new BigDecimal("198.75"), share.getPurchasePrice()); - } - - @Test - void getStockReturnsCorrectStockObject() { - Stock stock = new Stock("NVDA", "NVIDIA Corporation", new BigDecimal("875.40")); - Share share = new Share(stock, new BigDecimal("4"), new BigDecimal("850.00")); - - assertSame(stock, share.getStock()); - assertEquals("NVDA", share.getStock().getSymbol()); - assertEquals("NVIDIA Corporation", share.getStock().getCompany()); - } -} \ No newline at end of file +import java.math.BigDecimal; +import org.junit.jupiter.api.Test; + +final class ShareTest { + + @Test + void constructorStoresAllValuesCorrectly() { + Stock stock = new Stock("AAPL", "Apple Inc.", new BigDecimal("150.00")); + BigDecimal quantity = new BigDecimal("10"); + BigDecimal purchasePrice = new BigDecimal("145.50"); + + Share share = new Share(stock, quantity, purchasePrice); + + assertSame(stock, share.getStock()); + assertEquals(quantity, share.getQuantity()); + assertEquals(purchasePrice, share.getPurchasePrice()); + } + + @Test + void shareSupportsDecimalValues() { + Stock stock = new Stock("TSLA", "Tesla Inc.", new BigDecimal("200.00")); + + Share share = new Share( + stock, + new BigDecimal("2.5"), + new BigDecimal("198.75") + ); + + assertEquals(new BigDecimal("2.5"), share.getQuantity()); + assertEquals(new BigDecimal("198.75"), share.getPurchasePrice()); + } + + @Test + void getStockReturnsCorrectStockObject() { + Stock stock = new Stock("NVDA", + "NVIDIA Corporation", + new BigDecimal("875.40")); + Share share = new Share(stock, + new BigDecimal("4"), + new BigDecimal("850.00")); + + assertSame(stock, share.getStock()); + assertEquals("NVDA", share.getStock().getSymbol()); + assertEquals("NVIDIA Corporation", share.getStock().getCompany()); + } +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchiveTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchiveTest.java index 17726ad..f447b8b 100644 --- a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchiveTest.java +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/TransactionArchiveTest.java @@ -1,130 +1,138 @@ package edu.ntnu.idi.idatt2003.g40.mappe; -import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; import java.util.List; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; - -class TransactionArchiveTest { - - private final TransactionCalculator calculator = new TransactionCalculator() { - @Override - public BigDecimal calculateGross() { - return BigDecimal.ZERO; - } - - @Override - public BigDecimal calculateCommission() { - return BigDecimal.ZERO; - } - - @Override - public BigDecimal calculateTax() { - return BigDecimal.ZERO; - } - - @Override - public BigDecimal calculateTotal() { - return BigDecimal.ZERO; - } - }; - - @Test - void newArchiveIsEmpty() { - TransactionArchive archive = new TransactionArchive(); - - assertTrue(archive.isEmpty()); - } - - @Test - void addMakesArchiveNonEmpty() { - TransactionArchive archive = new TransactionArchive(); - Transaction transaction = createPurchase("AAPL", "Apple", 1); - - boolean result = archive.add(transaction); - - assertTrue(result); - assertFalse(archive.isEmpty()); - } - - @Test - void getTransactionsReturnsOnlyTransactionsFromGivenWeek() { - TransactionArchive archive = new TransactionArchive(); - - Transaction transaction1 = createPurchase("AAPL", "Apple", 1); - Transaction transaction2 = createSale("TSLA", "Tesla", 2); - Transaction transaction3 = createPurchase("NVDA", "Nvidia", 1); - - archive.add(transaction1); - archive.add(transaction2); - archive.add(transaction3); - - List