-
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 #89 from Team-40-IDATT2003/74-transaction-factory
74 transaction factory
- Loading branch information
Showing
4 changed files
with
160 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
51 changes: 51 additions & 0 deletions
51
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionFactory.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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package edu.ntnu.idi.idatt2003.g40.mappe.service; | ||
|
|
||
| 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.Transaction; | ||
| import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator; | ||
|
|
||
| /** | ||
| * Responsible for creating concrete implementations of {@link Transaction}. | ||
| * | ||
| * <p>Uses the factory design pattern.</p> | ||
| * */ | ||
| 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!"); | ||
| }; | ||
| } | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionType.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 |
|---|---|---|
| @@ -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; | ||
| } |
70 changes: 70 additions & 0 deletions
70
src/test/java/edu/ntnu/idi/idatt2003/g40/mappe/service/TransactionFactoryTest.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 |
|---|---|---|
| @@ -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()); | ||
| } | ||
| } |