TransactionArchive.java
package edu.ntnu.idi.idatt.model.transaction;
import java.util.ArrayList;
import java.util.List;
import edu.ntnu.idi.idatt.model.player.Player;
/**
* TransactionArchive class
*
* <p>
* Manages and handles transaction logic
* </p>
*
*/
public class TransactionArchive {
private final ArrayList<Transaction> transactions = new ArrayList<>();
/**
* Method for adding a new transaction to ArrayList transactions.
*
* @param transaction - The transaction instance
* @return - was the list modified?
*/
public boolean add(Transaction transaction) {
return transactions.add(transaction);
}
/**
* Method for checking if there has been any transactions previously.
*
* @return - was the transactions ArrayList empty?
*/
public boolean isEmpty() {
return transactions.isEmpty();
}
/**
* Getter for transactions done
*
* @param week - Transaction interval
* @return - List of Transaction done in a specified week.
*/
public List<Transaction> getTransactions(int week) {
return transactions.stream().filter(transaction -> transaction.getWeek() == week).toList();
}
/**
* Getter for all transactions done.
*
* @return List of transactions.
*/
public List<Transaction> getTransactions() {
return transactions;
}
/**
* Getter for purchases done
*
* @param week - Purchase interval
* @return - List of Purchase done in a specified week.
*/
public List<Purchase> getPurchases(int week) {
return getTransactions(week).stream().filter(t -> t instanceof Purchase)
.map(t -> (Purchase) t)
.toList();
}
/**
* Getter for all purchases.
*
* @return List of Purchase.
*/
public List<Purchase> getPurchases() {
return getTransactions().stream().filter(t -> t instanceof Purchase)
.map(t -> (Purchase) t)
.toList();
}
/**
* Getter for sales done
*
* @param week - Sale interval
* @return - List of Sale done in a specified week.
*/
public List<Sale> getSales(int week) {
return getTransactions(week).stream().filter(t -> t instanceof Sale)
.map(t -> (Sale) t)
.toList();
}
/**
* Getter for all sales.
*
* @return List of Sale.
*/
public List<Sale> getSales() {
return getTransactions().stream().filter(t -> t instanceof Sale)
.map(t -> (Sale) t)
.toList();
}
/**
* Method for counting amount of distinct weeks.
* <p>
* Calculates how many weeks atleast one Transaction has been done.
* Used to calculate player statuses.
* </p>
*
* {@link Player}
*
* @return int amount of distinct weeks.
*/
public int countDistinctWeeks() {
return (int) transactions.stream()
.map(Transaction::getWeek)
.distinct()
.count();
}
}