/* * Account.java * * Created on March 6, 2002, 12:45 PM */ /** * * @author Mike Redmond * @version 1 * Simple Bank Account */ public class Account { protected String acctNum; protected String PIN; protected double balance = 0; /** Creates new Account */ public Account() { } // deposit a given amount into the calling account public boolean deposit(double amt) { // reject a negative deposit // only return false because we don't know where the amount came // from. it might not have come from a user, so we don't know if // we should reprompt (let caller handle the problem) if (amt <= 0) { System.out.println("PROGRAM ERROR - cannot deposit negative amount"); return false; } else { balance = balance + amt; return true; } } // withdraw a given amount from the calling account public boolean withdraw(double amt) { // reject a negative wothdrawal // only return false because we don't know where the amount came // from. it might not have come from a user, so we don't know if // we should reprompt (let caller handle the problem) if (amt <= 0) { System.out.println("PROGRAM ERROR - cannot withraw a negative amount"); return false; } else if (amt > balance) { System.out.println("ERROR - cannot withraw an amount more than in account"); return false; } else { // can withdraw balance = balance - amt; return true; } } // return the current balance - basically an account inquiry public double getBal() { return balance; } // return the account number public String getAcctNum() { return acctNum; } // report the PIN public String getPin() { return PIN; } // set the account balance - should probably only be used publicly // at the creation of the account. All other accesss to changing // the balance should be handled through deposit or withdraw public boolean setBal(double amt) { // reject negative balance if (amt < 0) { System.out.println("PROGRAM ERROR - cannot set a negative account balance"); return false; } else { balance = amt; } return true; } // set account number - should probably only be used publically // at the creation of the account. public boolean setAcctNum(String acctNumber) { acctNum = acctNumber; // could check acct number to see if it is valid (check digit // - or already in DB) but for now just say that it is ok return true; } // set PIN - should be carefully controlled public boolean setPIN(String pin) { PIN = pin; // could check PIN based on some PIN rules - but for now just say // that it is ok return true; } // determine if a passed string is the correct account number for the // calling object public boolean matchAcctNum(String toCompare) { if (acctNum.equals(toCompare)) { return true; } else { return false; } } // determine if a passed account has the same account number as the // calling object public boolean matchAcctNum(Account toCompare) { if (acctNum.equals(toCompare.getAcctNum())) { return true; } else { return false; } } // determine if a passed account has the same contents as the // calling object (account number, PIN, AND balance) public boolean equals(Account toCompare) { if ( acctNum.equals(toCompare.getAcctNum()) && PIN.equals(toCompare.getPin()) && balance == toCompare.getBal() ) { return true; } else { return false; } } // produce a string for an account - generally used for display public String toString() { String res = "Acct: " + acctNum + " PIN: " + PIN + " Bal: " + balance; return res; } }