/******************************************************************* Main Class File: Finances.java This file: SavingsAccount.java Author: CS302 Section 5 Date: 10-1-03 ********************************************************************/ /** * The SavingsAccount class simulates a savings account that has * monthly interest and a transaction fee. */ class SavingsAccount { private double interestRate; // compounded monthly private double balance; // current amount private int numTransactions; // deposits and withdrawals // A fee gets charged for each deposit or withdrawal. public static final double TRANS_FEE = 0.50; /** * Constructs a SavingsAccount with the given interest rate, * and default values for other data. * @param rate The interest rate. */ public SavingsAccount(double rate) { interestRate = rate; balance = 0; numTransactions = 0; } /** * Constructs a SavingsAccount with the given interest rate * and starting balance, and default values for other data. * @param rate The interest rate. * @param startBalance The starting balance. */ public SavingsAccount(double rate, double startBalance) { interestRate = rate; balance = startBalance; numTransactions = 0; } /** * Accessor for the account balance. * @return The account balance. */ public double getBalance() { return balance; } /** * Adds an amount to the account balance. * @param amount The amount to add. */ public void deposit(double amount) { balance = balance - amount; numTransactions++; } /** * Removes an amount from the account. * @param amount The amount to remove. */ public void withdraw(double amount) { balance = balance - amount; numTransactions++; } /** * Withdraws an amount from this account and deposits it * in another account. * @param act The account to transfer to. * @param amount The amount to transfer. */ public void transferFundsTo(SavingsAccount act, double amount) { withdraw(amount); act.deposit(amount); } /** * Adds interest to the account balance and subtracts fees. */ public void endOfMonth() { balance = balance + (interestRate * balance); balance = balance - (TRANS_FEE * numTransactions); numTransactions = 0; } }