tags:

views:

15

answers:

2

Here is the code I have so far for the Account class

public class Account {
    Transcation transcation[];
    private int account;



public Account(){
    transcation = new Transcation[5];


}
private void setAccount(int account){
    this.account = account;

}
public int getAccount(){
    return account;
}

public performTranscation(){
    return transcation;
}

}

A: 
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());
    }
}
Michael Barker
A: 

Define transactions as EnumSet:

EnumSet<Transcation> transactions;
卢声远 Shengyuan Lu