Advertisement
jdalbey

Account class for JUnit practice lab

Jan 12th, 2015
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. import java.text.NumberFormat;
  2.  
  3. /**
  4.  * Account is a bank account with basic services for deposit,
  5.  * withdrawal, and interest.
  6.  */
  7. public class Account
  8. {
  9.     private NumberFormat fmt = NumberFormat.getCurrencyInstance();
  10.  
  11.     private final float kInterestRate = 0.045f;  // interest rate of 4.5%
  12.  
  13.     private long acctNumber;
  14.     private float balance;
  15.     public final String name;
  16.  
  17.     /**
  18.      * Sets up the account by defining its owner, account number,
  19.      * and initial balance.
  20.      * @param owner name of account holder
  21.      * @param account the account number, an identifier for the account
  22.      * @param initial the initial amount of money in the account.
  23.      */
  24.     public Account(String owner, long account, float initial)
  25.     {
  26.         name = owner;
  27.         acctNumber = account;
  28.         balance = initial;
  29.     }
  30.  
  31.     /**
  32.      *  Deposit the specified amount into the account.
  33.      *  @param amount value to be added to the balance
  34.      *  @return true if amount is non-negative, false if amount
  35.      *  is negative; indicates balance was not changed.
  36.      */
  37.     public boolean deposit(float amount)
  38.     {
  39.         boolean result = true;
  40.         // is amount invalid?
  41.         if (amount < 0)  
  42.         {
  43.             result = false;
  44.         }
  45.         else
  46.         {
  47.             balance = balance + amount;
  48.         }
  49.  
  50.         return result;
  51.     }
  52.  
  53.     /**
  54.      *  Withdraw the specified amount from the account,
  55.      *  unless amount is negative, fee is negative, or
  56.      *  amount exceeds current balance.
  57.      *  @param amount value to be deducted from the balance
  58.      *  @param fee the transaction fee debited from the account
  59.      *  @return true if transaction was successful, false otherwise;
  60.      */
  61.     public boolean withdraw(float amount, float fee)
  62.     {
  63.         // validate parameters
  64.         if (isValidWithdrawl(amount, fee))
  65.         {
  66.             amount += fee;
  67.             balance = balance - amount;
  68.         }
  69.         return isValidWithdrawl(amount, fee);
  70.     }
  71.  
  72.     /* Determine if withdrawal parameters are valid */
  73.     private boolean isValidWithdrawl(float amount, float fee)
  74.     {
  75.         return amount >= 0 && fee >= 0 && amount <= balance;
  76.     }
  77.  
  78.     /**
  79.      *  Adds interest to the account.
  80.      */
  81.     public void addInterest()
  82.     {
  83.         balance += (balance * kInterestRate);
  84.     }
  85.  
  86.     /**
  87.      * Accessor to the current balance of the account.
  88.      * @return the current balance of the account.
  89.      */
  90.     public float getBalance()
  91.     {
  92.         return balance;
  93.     }
  94.  
  95.     /**
  96.      *  Accessor to the account number.
  97.      * @return the account number.
  98.      */
  99.     public long getAccountNumber()
  100.     {
  101.         return acctNumber;
  102.     }
  103.  
  104.     /**
  105.      *  Returns a one-line description of the account as a string.
  106.      *  @return formatted account information
  107.      */
  108.     public String toString()
  109.     {
  110.         return (acctNumber + "\t" + name + "\t" + fmt.format(balance));
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement