/* * EzPassBridge.java * * Created on April 27, 2003, 7:52 PM */ package ezpassassign; import java.util.*; /** * * @author mike */ public class EzPassBridge extends EzPassLocation { /** * price for a particular bridge */ private double price = 2.70; /** Creates a new instance of EzPassBridge - with defaults for everything*/ public EzPassBridge() { //System.out.println("In bridge constructor #1"); } /** Creates a new instance of EzPassBridge - with name and defaults for everything else */ public EzPassBridge(String nam) { super(nam); //System.out.println("In bridge constructor #2"); } /** Creates a new instance of EzPassBridge - with name and price given, defaults for everything else */ public EzPassBridge(String nam, double prc) { super(nam); //System.out.println("In bridge constructor #3"); price = prc; } /** Creates a new instance of EzPassBridge - with name, city and state and price given, defaults for everything else */ public EzPassBridge(String nam, String cty, String stat, double prc) { super(nam,cty,stat); //System.out.println("In bridge constructor #4"); price = prc; } /** Creates a new instance of EzPassBridge - with all info given */ public EzPassBridge(String nam, String cty, String stat, String hwy, int num, char dir, double prc) { super(nam,cty,stat,hwy,num,dir); //System.out.println("In bridge constructor #5"); price = prc; } /** * report ez pass bridge price */ public double getPrice() { return price; } /** * report ez pass bridge price - alias since this is really core functionality * much more important than just an inspector - provide a very logical name for * the functionality * */ public double tollAmount() { return price; } /** * change ez pass bridge price. * If price is outside normal range (0.25 -> 10.00), reject and return false. * (based top of range on Chesapeake Bay Bridge Tunnel) * Existing price will remain. */ public boolean setPrice(double prc) { if ((prc < 0.25) || (prc > 10.00)) { // anything under a penny is impossible return false; } else { price = prc; return true; } } /** * debit a customer the price for this bridge. * Reduces the customer balance by the price for this bridge. * If the customer method throws an exception, we must handle it (and return false) * */ public boolean debit(EzPassCustomer cust) { System.out.println("In bridge debit"); try { // don't need return because that is only false if negative amount and // we have not made it possible for price to be negative cust.decreaseBalance(price); // System.out.println("Cust: " + cust); return true; } catch (InsufficientFundsNonCreditException ex) { System.out.println("In catch block with: " + ex.getCustTakingFrom()); cust.sendViolation(); return false; } } /** * generate a string represnetation for a bridge */ public String toString() { String res = super.toString(); res += " Price: " + getPrice(); return res; } public boolean equals(Object obj) { if (!(obj instanceof EzPassBridge)) { // can't be equal if not the same typ object return false; } else { EzPassLocation toCompare = (EzPassLocation) obj; // if all data equal then objects equal if (super.equals(toCompare) && price == ((EzPassBridge) toCompare).getPrice() ) { return true; } else { return false; } } } /** * @param args the command line arguments */ public static void main(String[] args) { EzPassCustomer joe = new EzPassCustomer("Joe", 15.00, true, 2.00); System.out.println("Joe: " + joe); EzPassCustomer jason = new EzPassCustomer("Jason", 5.00, false, 2.00); System.out.println("Jason: " + jason); EzPassBridge betsy = new EzPassBridge("Betsy Ross", "Pennsauken", "NJ", "NJ 90", 4, 'w', 3.00); betsy.debit(joe); betsy.debit(jason); // save time betsy.debit(joe); } } /* * just trying out HashMap before trying to use it for interchanges EzPassBridge ben = new EzPassBridge("Ben Franklin", "Camden", "NJ", "NJ 38", 2, 'w', 3.00); EzPassBridge walt = new EzPassBridge("Walt Whitman", "Philadelphia", "PA", "NJ 42", 3, 'w', 3.00); EzPassBridge tac = new EzPassBridge("Tacony Palmyra", "Pennsauken", "NJ", "NJ 73", 2, 'w', 2.00); HashMap lookup = new HashMap(); lookup.put(betsy,new Integer(30)); lookup.put(ben,new Integer(20)); lookup.put(walt,new Integer(10)); lookup.put(tac,new Integer(0)); int res = ((Integer)lookup.get(ben)).intValue(); System.out.println("Ben: " + ben + " is key with value: " + res); **/