Skip to content

Commit

Permalink
Feat: Refactored transactionarchive + testing
Browse files Browse the repository at this point in the history
Replaced for loops with streams, centralized concrete getters via get transactions, and added parameter guarding. Also updated test class.
  • Loading branch information
tommyah committed May 25, 2026
1 parent 3ee9296 commit dccb4f8
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
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.Transaction;
import edu.ntnu.idi.idatt2003.g40.mappe.utils.Validator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Stores completed transactions.
* Stores completed transactions in an {@link ArrayList} object.
*
* @version 1.1.0
*/
public final class TransactionArchive {

Expand All @@ -20,6 +24,7 @@ public final class TransactionArchive {
* Creates an empty transaction archive.
*/
public TransactionArchive() {
// Empty constructor.
}

/**
Expand All @@ -28,8 +33,13 @@ public TransactionArchive() {
* @param transaction the transaction to add
*
* @return true if the transaction was added
*
* @throws IllegalArgumentException if transaction is null
*/
public boolean add(final Transaction transaction) {
if (transaction == null) {
throw new IllegalArgumentException("Transaction cannot be null!");
}
return transactions.add(transaction);
}

Expand All @@ -48,24 +58,27 @@ public boolean isEmpty() {
* @param week the week number
*
* @return list of transactions from the given week
*
* @throws IllegalArgumentException if week is less than 1.
*/
public List<Transaction> getTransactions(final int week) {
List<Transaction> result = new ArrayList<>();
for (Transaction transaction : transactions) {
if (transaction.getWeek() == week) {
result.add(transaction);
}
if (!Validator.VALID_WEEK.isValid(Integer.toString(week))) {
throw new IllegalArgumentException(
Validator.VALID_WEEK.getErrorMessage()
);
}
return result;
return transactions.stream()
.filter(transaction -> transaction.getWeek() == week)
.toList();
}

/**
* Returns all transactions.
* Returns an un-mutable reference to the transactions list.
*
* @return list of transactions from the given week
* @return unmodifiable version of list.
*/
public List<Transaction> getTransactions() {
return transactions;
return Collections.unmodifiableList(transactions);
}

/**
Expand All @@ -74,16 +87,15 @@ public List<Transaction> getTransactions() {
* @param week the week number
*
* @return list of purchases from the given week
*
* @throws IllegalArgumentException if week is less than 1.
*/
public List<Purchase> getPurchases(final int week) {
List<Purchase> result = new ArrayList<>();
for (Transaction transaction : transactions) {
if (transaction instanceof Purchase purchase
&& transaction.getWeek() == week) {
result.add(purchase);
}
}
return result;
public List<Purchase> getPurchases(final int week)
throws IllegalArgumentException {
return getTransactions(week).stream()
.filter(Purchase.class::isInstance)
.map(Purchase.class::cast)
.toList();
}

/**
Expand All @@ -92,15 +104,15 @@ public List<Purchase> getPurchases(final int week) {
* @param week the week number
*
* @return list of sales from the given week
*
* @throws IllegalArgumentException if week is less than 1.
*/
public List<Sale> getSales(final int week) {
List<Sale> result = new ArrayList<>();
for (Transaction transaction : transactions) {
if (transaction instanceof Sale sale && transaction.getWeek() == week) {
result.add(sale);
}
}
return result;
public List<Sale> getSales(final int week)
throws IllegalArgumentException {
return getTransactions(week).stream()
.filter(Sale.class::isInstance)
.map(Sale.class::cast)
.toList();
}

/**
Expand All @@ -109,13 +121,9 @@ public List<Sale> getSales(final int week) {
* @return number of distinct weeks
*/
public int countDistinctWeeks() {
List<Integer> weeks = new ArrayList<>();
for (Transaction transaction : transactions) {
int week = transaction.getWeek();
if (!weeks.contains(week)) {
weeks.add(week);
}
}
return weeks.size();
return (int) transactions.stream()
.map(Transaction::getWeek)
.distinct()
.count();
}
}
Loading

0 comments on commit dccb4f8

Please sign in to comment.