public class Account {
private Transaction[] transactions = new Transaction[0];
private BigDecimal balance = BigDecimal.ZERO;
public performTransaction(Transaction transaction) {
transactions = transactions.copyOf(transactions, transactions.length + 1;
transactions[transactions.length - 1] = transaction;
balance = balance.plus(transaction.getDelta());
}
}
public class Transaction {
private final BigDecimal delta;
public Transaction(BigDecimal delta) {
this.delta = delta;
}
public BigDecimal getDelta() {
return delta;
}
}
However you should use an ArrayList not an array; it will be more efficient as you will not need to copy the array each time.
public class Account {
private final List<Transaction> transactions = new ArrayList<Transaction>();
private BigDecimal balance = BigDecimal.ZERO;
public performTransaction(Transaction transaction) {
transactions.add(transaction);
balance = balance.plus(transaction.getDelta());
}
}