1  // A class with data for a savings account.
  2  // This first example illustrates how to define a class with non-public
  3  // instance variables and some public methods.
  4  //       Nancy McCracken  3/14/97
  5  // API of this class:
  6  //
  7  // public class Account
  8  // {
  9  //    public Account (String n, double b) 
 10  //    public String getname()
 11  //    public double getbalance()
 12  //    public int getbalancedollars()
 13  //    public int getbalancecents()
 14  //    public void withdraw (double amount)
 15  //    public void deposit (double amount)
 16  //    public double monthactivity ()
 17  // }
 18  
 19  public class Account
 20  { // instance variables
 21    String name;
 22    double balance;
 23    static double interestrate = .04;
 24  
 25    // constructor method is used to initialize instance variables
 26    public Account (String n, double b)
 27    { name = n;
 28      balance = b;
 29    }
 30  
 31    // accessor methods
 32    public String getname()
 33    { return name; }
 34  
 35    public double getbalance()
 36    { return balance; }
 37  
 38    public int getbalancedollars()
 39    { return (int)Math.floor(balance); }
 40  
 41    public int getbalancecents()
 42    { return (int)(balance - Math.floor(balance))*100 ; }
 43  
 44    // other methods
 45    public void withdraw (double amount)
 46    { 
 47      if ((balance - amount) > 0)
 48      { balance -= amount;
 49      }
 50      else { } // later will return exception "insufficient funds"
 51    }
 52  
 53    public void deposit (double amount)
 54    {  balance += amount; }
 55  
 56    // returns monthly interest and updates balance
 57    public double monthactivity()
 58    { double interest = (balance * interestrate)/12;
 59      balance += interest;
 60      return interest;
 61    }
 62  }