CS 1063, Fall 2005
SavingsAccount class

The lectures for Week 6 had you work on a SavingsAccount class, modeled after the BankAccount class of the lecture in Week 5. Here is one version of that class. Many additions just change "Bank" to "Savings". More important additions are in red.

// SavingsAccount: handle balance and operations
public class SavingsAccount {
  // private data (instance variables) here
  private double myCurrentBalance;
  private double myInterest;
 
  // Constructors
  // Constructs a savings account with a zero balance./
  public SavingsAccount() {
    // initialize instance variables to 0
    this(0.0, 0.0);
  }
  
  // Constructs a savings account with a given balance
  //    @param initialBalance the initial balance
  public SavingsAccount(double initialBalance, 
                        double initialInterest) {
    // initialize 
    myCurrentBalance = initialBalance;
    myInterest = initialInterest;
  }
 
  // methods
  // add interest into the savings account.
  public void addInterest(){
    //  Add interest to balance
    double interestAmount = myCurrentBalance*myInterest;
    myCurrentBalance = myCurrentBalance + interestAmount;
  }

  // Deposits money into the savings account.
  //    @param amount the amount to deposit
  public void deposit(double amount){
    //  Add amount to balance
    myCurrentBalance = myCurrentBalance + amount;
  }
 
  // Withdraws money from the savings account
  //    @param amount the amount to withdraw
  public void withdraw(double amount){
    //  Subtract amount from balance
    myCurrentBalance = myCurrentBalance - amount;
  }
 
  // Gets the current balance of the savings account.
  //    @return the current balance
  public double getBalance(){
    //  Return balance
    return myCurrentBalance;  
  }
  
  // Gets the interest rate of the savings account.
  //    @return the interest
  public double getInterest(){
    //  Return interest rate
    return myInterest;  
  }
 
  // toString method allows printing class instance
  public String toString() {
    return "Current Balance: " + myCurrentBalance +
       ", Interest Rate: " + myInterest;
  }
}
public class SavingsAccountTest {

   public static void main(String[] args) {
      SavingsAccount savings1 = 
         new SavingsAccount(1000, 0.10);
      System.out.println("Savings 1 balance: " +
         savings1.getBalance());
      savings1.addInterest();
      savings1.addInterest();
      savings1.addInterest();
      System.out.println("Savings 1 balance: " +
         savings1.getBalance());
      savings1.addInterest();
      savings1.addInterest();
      savings1.addInterest();
      System.out.println("Savings 1 balance: " +
         savings1.getBalance());
      System.out.println("Savings 1 interest: " +
         savings1.getInterest());
      // call toString method implicitly
      System.out.println(savings1);
      
      SavingsAccount savings2 = 
         new SavingsAccount();
      System.out.println(savings2);
   }
}

Output:
Savings 1 balance: 1000.0
Savings 1 balance: 1331.0    (After 3 years)
Savings 1 balance: 1771.561  (After 3 more years)
Savings 1 interest: 0.1
(Final balance for savings1 account)
Current Balance: 1771.561, Interest Rate: 0.1
(Balance and interest for savings2 account)
Current Balance: 0.0, Interest Rate: 0.0