Skip to content

Commit

Permalink
Add TransactionArchive
Browse files Browse the repository at this point in the history
  • Loading branch information
Solveig Natvig committed Mar 22, 2026
1 parent 32b5cb7 commit 5ec6c53
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/main/java/TransactionArchive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.List;

public class TransactionArchive {
private List<Transaction> transactions;

public TransactionArchive() {
this.transactions = new ArrayList<>();
}

public boolean add(Transaction transaction) {
if (transaction == null) {
throw new IllegalArgumentException("Should not be null"); // Eller NullPointerExeption?
}

return transactions.add(transaction);

}

public boolean isEmpty() {
return transactions.isEmpty();
}

public List<Transaction> getTransactions(int week) {
return transactions.stream().filter(transaction -> transaction.getWeek() == week).collect(Collectors.toList());
}

public List<Purchase> getPurchase(int week) {
return transactions.stream().filter(transaction -> transaction.getWeek() == week).filter(purchase -> transactions instanceof Purchase).map(transaction -> (Purchase) transaction).collect(Collectors.toList());
}

public List<Sale> getSale(int week) {
return transactions.stream().filter(transaction -> transaction.getWeek() == week).filter(sale -> transactions instanceof Sale).map(transaction -> (Sale) transaction).collect(Collectors.toList());
}

public int countDistinctWeeks() {
return (int) transactions.stream().map(Transaction::getWeek).distinct().count();
}
}

0 comments on commit 5ec6c53

Please sign in to comment.