aznishboy

CheckingAccount

Feb 9th, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.43 KB | None | 0 0
  1. /**
  2.    A bank account has a balance that can be changed by
  3.    deposits and withdrawals.
  4. */
  5. public class CheckingAccount
  6. {
  7.   private final double CHECK_CHARGE = 0.15;
  8.   private double myBalance;
  9.  
  10.   /**
  11.     Constructs a bank account with a zero balance
  12.   */
  13.   public CheckingAccount()
  14.   {
  15.     myBalance = 0;
  16.   }
  17.  
  18.   /**
  19.     Constructs a bank account with a given balance
  20.     @param initialBalance the initial balance
  21.   */
  22.   public CheckingAccount(double initialBalance)
  23.   {
  24.     if (initialBalance >= 0)
  25.         myBalance = initialBalance;
  26.     else
  27.         throw new IllegalArgumentException("Error CheckingAccount: negative initial balance");
  28.   }
  29.  
  30.   /**
  31.     Deposits money into the bank account.
  32.     @param amount the amount to deposit
  33.   */
  34.   public void deposit(double amount)
  35.   {
  36.     if (amount >= 0)
  37.         myBalance = myBalance + amount;
  38.     else
  39.         throw new IllegalArgumentException("Error deposit: negative amount");
  40.   }
  41.  
  42.   /**
  43.     Withdraws money from the bank account.
  44.     @param amount the amount to withdraw
  45.   */
  46.   public void withdraw(double amount)
  47.   {
  48.     if (amount >= 0 && amount <= (myBalance - CHECK_CHARGE))
  49.         myBalance = myBalance - amount - CHECK_CHARGE;
  50.     else
  51.         throw new IllegalArgumentException("Error withdraw: illegal amount");
  52.   }
  53.  
  54.   /**
  55.     Gets the current balance of the bank account.
  56.     @return the current balance
  57.   */
  58.   public double getBalance()
  59.   {
  60.     return myBalance;
  61.   }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment