diff --git a/pom.xml b/pom.xml
index a9f5520..f5081aa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -65,6 +65,27 @@
Uses the factory design pattern.
+ * */ +public final class TransactionFactory { + + private TransactionFactory() { + + } + + /** + * Creates a transaction based on transactionType, share, week and calculator. + * + * @param transactionType the type of transaction to create. + * @param share the share this transaction is about. + * @param week the week this transaction takes place in. + * @param calculator the calculator to use when calculating the transaction. + * + * @return an implementation of {@link Transaction}. + * */ + public static Transaction createTransaction(final TransactionType + transactionType, + final Share share, + final int week, + final TransactionCalculator + calculator) + throws IllegalArgumentException { + if (transactionType == null + || share == null + || !Validator.VALID_POSITIVE_INT.isValid(Integer.toString(week)) + || calculator == null) { + throw new IllegalArgumentException("Null or empty parameters for factory!"); + } else { + return switch (transactionType) { + case SALE -> new Sale(share, week, calculator); + case PURCHASE -> new Purchase(share, week, calculator); + default -> throw new + IllegalArgumentException("Invalid transaction type!"); + }; + } + } +} diff --git a/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionType.java b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionType.java new file mode 100644 index 0000000..bb0ed32 --- /dev/null +++ b/src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionType.java @@ -0,0 +1,18 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.service; + +/** + * Enum used by the {@link TransactionFactory} to separate between + * the different transaction implementations. + * */ +public enum TransactionType { + + /** + * {@link edu.ntnu.idi.idatt2003.g40.mappe.model.Sale}. + * */ + SALE, + + /** + * {@link edu.ntnu.idi.idatt2003.g40.mappe.model.Purchase}. + * */ + PURCHASE; +} diff --git a/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionFactoryTest.java b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionFactoryTest.java new file mode 100644 index 0000000..6b25c35 --- /dev/null +++ b/src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionFactoryTest.java @@ -0,0 +1,70 @@ +package edu.ntnu.idi.idatt2003.g40.mappe.service; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import edu.ntnu.idi.idatt2003.g40.mappe.model.Purchase; +import edu.ntnu.idi.idatt2003.g40.mappe.model.Sale; +import edu.ntnu.idi.idatt2003.g40.mappe.model.Share; +import edu.ntnu.idi.idatt2003.g40.mappe.model.Stock; +import edu.ntnu.idi.idatt2003.g40.mappe.model.Transaction; +import java.math.BigDecimal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class TransactionFactoryTest { + private Stock testStock; + private Share testShare; + private Sale testSale; + private Purchase testPurchase; + private SaleCalculator testSaleCalculator; + private PurchaseCalculator testPurchaseCalculator; + + @BeforeEach + void setUp() { + testStock = new Stock("AAPL", "APPLE INC.", new BigDecimal("100.00")); + testShare = new Share(testStock, new BigDecimal("10.0"), testStock.getSalesPrice()); + testSaleCalculator = new SaleCalculator(testShare); + testSale = new Sale(testShare, 1, testSaleCalculator); + testPurchaseCalculator = new PurchaseCalculator(testShare); + testPurchase = new Purchase(testShare, 1, testPurchaseCalculator); + } + + @Test + void factoryReturnsCorrectSale() { + Transaction sale2 = TransactionFactory.createTransaction(TransactionType.SALE, testShare, 1, testSaleCalculator); + assertTrue(equalTransactions(testSale, sale2)); + } + + @Test + void factoryReturnsCorrectPurchase() { + Transaction purchase2 = TransactionFactory.createTransaction(TransactionType.PURCHASE, testShare, 1, testPurchaseCalculator); + assertTrue(equalTransactions(testPurchase, purchase2)); + } + + @Test + void factoryThrowsErrors() { + assertThrows(IllegalArgumentException.class, () -> { + TransactionFactory.createTransaction(TransactionType.PURCHASE, null, 1, testPurchaseCalculator); + }); + + assertThrows(IllegalArgumentException.class, () -> { + TransactionFactory.createTransaction(TransactionType.PURCHASE, testShare, -1, testPurchaseCalculator); + }); + + assertThrows(IllegalArgumentException.class, () -> { + TransactionFactory.createTransaction(TransactionType.PURCHASE, testShare, 1, null); + }); + + assertThrows(IllegalArgumentException.class, () -> { + TransactionFactory.createTransaction(null, testShare, 1, testPurchaseCalculator); + }); + } + + private boolean equalTransactions(final Transaction transaction1, final Transaction transaction2) { + return (transaction1.getWeek() == transaction2.getWeek() + && transaction1.getShare() == transaction2.getShare() + && transaction1.getCalculator() == transaction2.getCalculator() + && transaction1.isCommited() == transaction2.isCommited()); + } +} \ No newline at end of file