Source of SavingsAccount.java


  1: 
  2: import java.util.Scanner;
  3: /**
  4:  Class of savings accounts.
  5: */
  6: public class SavingsAccount
  7: {
  8:         private double balance;
  9:     private static double interestRate = 0;
 10:     private static int numberOfAccounts = 0;
 11: 
 12:     public SavingsAccount()
 13:     {
 14:         balance = 0;
 15:                 numberOfAccounts++;
 16:     }
 17: 
 18:         /** Sets the interest rate for all accounts. */
 19:     public static void setInterestRate(double newRate)
 20:     {
 21:         interestRate = newRate;
 22:     }
 23: 
 24:     public static double getInterestRate()
 25:     {
 26:         return interestRate;
 27:     }
 28: 
 29:     public static int getNumberOfAccounts()
 30:     {
 31:         return numberOfAccounts;
 32:     }
 33: 
 34:     public void deposit(double amount)
 35:     {
 36:         balance = balance + amount;
 37:     }
 38: 
 39:     public double withdraw(double amount)
 40:     {
 41:                 if (balance >= amount)
 42:                         balance = balance - amount;
 43:                 else
 44:                         amount = 0;
 45:         return amount;
 46:     }
 47: 
 48:     public void addInterest()
 49:     {
 50:             double interest = balance * interestRate; //balance * getInterestRate()        
 51:             balance = balance + interest;
 52:     }
 53:         
 54:     public double getBalance()
 55:     {
 56:         return balance;
 57:     }
 58: 
 59:     public static void showBalance(SavingsAccount account)
 60:         {
 61:                 System.out.print(account.getBalance());         
 62:         }
 63: }