/* * Account.java * * Created on August 14, 2002, 1:18 PM * originally created on March 6, 2002, 12:45 PM */ package bank; //import IO.RedmondMsgIn; import IDs.Id; import SortSearchEtc.Sorts; import java.util.Random; import java.util.Arrays; // don't have to import exception class NegativeAmount because it is in the same // package /** * * @author Mike Redmond * @version 1 * Simple Bank Account */ public class Account implements Comparable, Cloneable { // Comparable is an interface - don't show until we talk about them // making account comparable enables sorting them protected Id acctNum; // use Id class so get check digits and stuff like that protected String PIN; protected double balance = 0; // protected double intRate = 0.03; /** * Creates a new instance of Account * takes defaults for balance (0) and interest rate, but no value for acct number */ public Account() { } /** * Creates a new instance of Account - with random acct number * and other given info about the account */ public Account(String pin, double bal) { String numb = Id.genRandomValidId(); // but what length? and do we want a check digit? // in Java, the Id, being an object, is not automatically created when the account // is being created (I believe in C++ it would exist starting when the constructor started // (after the base member initialization list has "run")) acctNum = new Id(numb); //acctNum.setId(numb); // add a check digit acctNum.addCheckDigit(); // don't need the returned check digit PIN = pin; balance = bal; } /** * Creates a new instance of Account - given info about the account */ public Account(String acct, String pin, double bal) { // in Java, the Id, being an object, is not automatically created when the account // is being created (I believe in C++ it would exist starting when the constructor started // (after the base member initialization list has "run")) acctNum = new Id(acct); //acctNum.setId(acct); PIN = pin; balance = bal; } /** * 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 { // Updates the account balance and returns true if successful balance = balance + amt; return true; } } /** * deposit a given amount into the calling Account * returns the new balance * Exception Handling version - do not show until exception handling covered */ public double depositEx (double amt) throws NegativeAmount { // reject a negative deposit // only throw exception 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) // or even really whether it is reasonable to put a message out - might be // a program error instead of a user error // using exception handling allows us to return the new balance when things // are ok. Before, I was forced to choose between having a boolean return or a balance if (amt <= 0) { throw new NegativeAmount("PROGRAM ERROR - cannot deposit negative amount",amt); } else { // Updates the account balance and returns true if successful balance = balance + amt; return balance; } } /** * 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; } // reject a withdrawal larger than account balance 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; } } /////////////////////////////////////////////////////////////////// // Inspectors /////////////////////////////////////////////////////////////////// /** * return the current balance - basically an account inquiry */ public double getBal() { return balance; } /** * return the account number as a string */ public String getAcctNum() { return acctNum.getId(); } /** * return the account number as an ID */ public Id getAcctID() { return acctNum; } /** * report the PIN */ public String getPin() { return PIN; } /////////////////////////////////////////////////////////////////// // Mutators /////////////////////////////////////////////////////////////////// /** * 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, given an id - should probably only be used publically * at the creation of the account. */ public boolean setAcctId(Id 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 account number, given a string - should probably only be used publically * at the creation of the account. */ public boolean setAcctNum(String acctNumber) { acctNum.setId(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.getId().equals(toCompare)) { return true; } else { return false; } } /** * determine if a passed account has the same account number as the * calling object * (does not worry about whether the id matches on required length or * whether there is a check digit - if the string matches, this matches */ public boolean matchAcctNum(Account toCompare) { if (acctNum.getId().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) * Overrides version in java.lang.Object */ public boolean equals(Account toCompare) { if ( acctNum.getId().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 * Overrides version in java.lang.Object */ public String toString() { String res = "Acct: " + acctNum.getId() + " PIN: " + PIN + " Bal: " + balance; return res; } /** * Compares two accounts - needed for sorting - required since implements Comparable interface * Don't show until we cover interfaces * returns negative if object comparing to is less, zero if object comparing to is equal, * positive if object comparing to is greater * MAR - I have made the call that accounts should be compared based on their acct number */ public int compareTo(Object obj) { // convert the passed object to an Account\ Account other = (Account) obj; // pull out the ids Id otherId = other.getAcctID(); Id firstId = getAcctID(); // make an object based on the otherId Object otherObj = (Object) otherId; // do the comparison using Id class's capabilities (compareTo) // and return the result return firstId.compareTo(otherObj); } /** * @param args the command line arguments * little test driver */ public static void main (String args[]) { System.exit(0); } }