Source of SavingsAccount.java


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