UNB/ CS/ David Bremner/ teaching/ old/ cs1083/ java/ DecimalAccount.java
import java.math.BigDecimal;
public class DecimalAccount
{
    private BigDecimal balance;

    public DecimalAccount() {
        balance = new BigDecimal(0);
    }

    public DecimalAccount(BigDecimal initialBalance) {
        balance = initialBalance;
    }

    public DecimalAccount(long initialDollars,int initialCents)   {
        balance = new BigDecimal(initialDollars).
            add(new BigDecimal(initialCents).scaleByPowerOfTen(-2));
    }

    public void deposit(BigDecimal amount) {
        balance = balance.add(amount);
    }

    public void deposit(long amount) {
        this.deposit(new BigDecimal(amount));
    }

    public void withdraw(BigDecimal amount) {
        balance=balance.subtract(amount);
    }

    public void withdraw(long amount) {
        this.withdraw(new BigDecimal(amount));
    }

    public void transfer(DecimalAccount other, long amount)
    {
        withdraw(amount);
        other.deposit(amount);
    }

    public void transfer(DecimalAccount other, BigDecimal amount)
    {
        withdraw(amount);
        other.deposit(amount);
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public static void main(String[] args)
    {
        DecimalAccount account = new DecimalAccount(100,00);

        final BigDecimal INTEREST_RATE = new BigDecimal("0.05");

        BigDecimal interest;

        // compute and add interest for one period

        interest = account.getBalance().multiply(INTEREST_RATE);
        account.deposit(interest);

        System.out.println("Balance after year 1 is $"
                           + account.getBalance());

        // add interest again

        interest = account.getBalance().multiply(INTEREST_RATE);
        account.deposit(interest);

        System.out.println("Balance after year 2 is $"
                           + account.getBalance());
   }
}