Skip to content

74 transaction factory #89

Merged
merged 3 commits into from
May 12, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.12.0</version>
</plugin>

<!-- Test coverage report creation -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.14</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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!");
};
}
}
}
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;
}
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());
}
}