/* * EzPassCustomer.java * * Created on March 3, 2005, 8:47 AM * Originally Created on April 27, 2003, 7:40 PM */ package ezpassassign; //import IO.*; /** * * @author Mike */ public class EzPassCustomer { // constant - penalty public static final double PENALTY = 10.00; /** * customer ID - to keep simple for now, just make it an integer */ private int custID = -1; /** * customer name. Other basic info like address is left off for the test */ private String name = ""; /** * current balance */ private double balance = 0; /** * amount to replenish the account with each time it is to be replenished */ private double replenishmentAmount = 50.00; /** * whether the customer is a credit card customer (true) or not */ private boolean creditCust = true; /** * Creates a new instance of EzPassCustomer - with all defaults */ public EzPassCustomer() { System.out.println("In cust constructor #1"); } /** * Creates a new instance of EzPassCustomer - with name, and rest defaults */ public EzPassCustomer(String nam) { System.out.println("In cust constructor #2"); name = nam; } /** * Creates a new instance of EzPassCustomer - with name, replenishment amount and rest defaults */ public EzPassCustomer(String nam, double amt) { System.out.println("In cust constructor #3"); name = nam; setReplenishmentAmount(amt); } /** * Creates a new instance of EzPassCustomer - with name, replenishment amount, credit card status and rest defaults */ public EzPassCustomer(String nam, double amt, boolean cred) { System.out.println("In cust constructor #4"); name = nam; setReplenishmentAmount(amt); setCreditCust(cred); } /** * Creates a new instance of EzPassCustomer - with all but ID passed */ public EzPassCustomer(String nam, double amt, boolean cred, double bal) { System.out.println("In cust constructor #5"); name = nam; setReplenishmentAmount(amt); setCreditCust(cred); setBalance(bal); } /** * Creates a new instance of EzPassCustomer - with all info passed */ public EzPassCustomer(String nam, double amt, boolean cred, double bal, int ID) { System.out.println("In cust constructor #6"); custID = ID; name = nam; setReplenishmentAmount(amt); setCreditCust(cred); setBalance(bal); } /** * report customer id */ public int getCustID() { return custID; } /** * report customer name */ public Object getName() { return name; } /** * report current balance for customer */ public double getBalance() { return balance; } /** * report replenishment amount */ public double getReplenishmentAmount() { return replenishmentAmount; } /** * report whether customer is a credit card customer or not */ public boolean getCreditCust() { return creditCust; } /** * change customer ID - for now, reject negative, all else ok */ public boolean setCustID(int ID) { if (ID >= 0) { custID = ID; return true; } else { return false; } } /** * change customer name */ public void setName(String nam) { name = nam; } /** * change replenishment amount. Amount is rejected if not positive */ public boolean setReplenishmentAmount(double amt) { if (amt < 0) { return false; } else { replenishmentAmount = amt; return true; } } /** * change whether customer is a credit card customer or not. Right now, I think this always succeeds. */ public boolean setCreditCust(boolean cred) { creditCust = cred; return true; } /** * change customer balance. Private - only used inside the class. * If negative amount return false. */ private boolean setBalance(double amt) { if (amt < 0) { return false; } else { balance = amt; return true; } } /** * decrease customer's outstanding balance. If the amount passed is negative, return false. * If the account has insufficient funds but is a credit card account, replenish the account, * decrease the balance and post to Credit card company. * (NOTE: I don't think this is exactly how EZPass actually works) * If the account has insufficient funds and is not a credit card account - throw an exception. */ public boolean decreaseBalance(double amt) throws InsufficientFundsNonCreditException { System.out.println("In customer decrease balance"); if (amt < 0) { return false; } // check if enough money if (balance < amt) { // not enough // check if credit card customer if (creditCust) { // credit card customer. // replenish account, reduce balance, post transaction to credit card company boolean ok = replenishAccount(); balance -= amt; postCreditCardDebit(replenishmentAmount); return true; } else { // not a credit card customer. gotta reduce balance so we don't lose // track of that they went through. Send a bill, and Throw exception balance -= amt + PENALTY; // hold off on sending the bill, let caller decide - just throw exception // sendBill(replenishmentAmount + PENALTY); // bill for replenishment amount plus // $10 fee for overrdraft throw new InsufficientFundsNonCreditException("Customer Out of Money", this, amt); } } else { // sufficient funds // debit balance -= amt; return true; } //return true; // should never get here } /** * replenish account balance, increasing it by the replenishment amount. * This could be for a credit card customer or non credit, so this method does * not handle posting credit card transaction. * For now, I don't see a way that this should fail, so we are always returning true. */ public boolean replenishAccount() { System.out.println("In customer replenish account"); balance += replenishmentAmount; return true; } /** * post a transaction to the customers credit card */ public void postCreditCardDebit(double amt) { System.out.println("Debit of " + amt + " to credit card account for " + this); } /** * send a customer a bill */ public void sendBill(double amt) { System.out.println("You owe " + amt + " cust: " + this); } /** * send a violation to a customer */ public void sendViolation() { System.out.println("VIOLATION " + this + " send replenishment amount + $10 fee immediately"); } /** * accept customer payment - increase balance * only thing that can go wrong is a negative amount */ public boolean acceptPayment(double amt) { if (amt > 0) { this.balance += amt; return true; } else { return false; } } /** * test if two customers have the same contents * Policy Decision: All instance variables must match */ public boolean equals(Object obj) { if (!(obj instanceof EzPassCustomer)) { // can't be equal if not the same typ object return false; } else { EzPassCustomer toCompare = (EzPassCustomer) obj; // if all data equal then objects equal if ((name.equals(toCompare.getName())) && (custID == toCompare.getCustID()) && (balance == toCompare.getBalance()) && (creditCust == toCompare.getCreditCust()) && (replenishmentAmount == toCompare.getReplenishmentAmount()) ) { return true; } else { return false; } } } /** * get a string representation for the customer */ public String toString() { String res = ""; res += "ID: " + getCustID(); res += " Name: " + getName(); res += " Balance: " + getBalance(); /// rest eliminated to save students time writing during the test res += " Cred: " + getCreditCust(); res += " Replenish: " + getReplenishmentAmount(); return res; } }