/* * EzPassInterchangeArrays.java * * Stores prices in parallel arrays - starting interchanges and corresponding prices for the current interchange * (which would be the end interchange) * * Created on March 3, 2005, 8:53 AM * Originally Created on February 10, 2004, 4:46 PM */ package ezpassassign; import java.util.*; /** * * @author Mike */ public class EzPassInterchangeArrays extends EzPassLocation { private EzPassInterchangeArrays[] startingXs; // starting interchanges private double[] prices; // prices // if this object is Valley Forge interchange, startingXs array could hold Phila, Willow Grove, NE Extension // prices could hold 1.50, 1.20, 6.50 etc' /** * Creates a new instance of EzPassInterchange * - initially with no prices, and default expected number of prices (16 ) */ public EzPassInterchangeArrays() { System.out.println("In EZPIA constructor #1"); prices = new double[16]; startingXs = new EzPassInterchangeArrays[16]; } /** * Creates a new instance of EzPassInterchange * - initially with no prices, with expected number of prices as given */ public EzPassInterchangeArrays(int size) { System.out.println("In EZPIA constructor #2"); prices = new double[size]; startingXs = new EzPassInterchangeArrays[size]; } /** * Creates a new instance of EzPassInterchangeArrays - with all info given except prices * (default space for price data structure) */ public EzPassInterchangeArrays(String nam, String cty, String stat, String hwy, int num, char dir) { super(nam,cty,stat,hwy,num,dir); System.out.println("In EZPIA constructor #3"); prices = new double[16]; startingXs = new EzPassInterchangeArrays[16]; } /** * Creates a new instance of EzPassInterchangeArrays - with all info given except prices */ public EzPassInterchangeArrays(String nam, String cty, String stat, String hwy, int num, char dir, int size) { super(nam,cty,stat,hwy,num,dir); System.out.println("In EZPIA constructor #4"); prices = new double[size]; startingXs = new EzPassInterchangeArrays[size]; } /** * Lookup toll from a given Interchange to the current (invoking) EzPassInterchangeArrays * */ public double tollAmount(EzPassInterchangeArrays start) { double result = -1.0; // would indicate not found if stays that way - really would like to use exception handling to handle // look up toll for (int x = 0; x < startingXs.length; x++) { if (startingXs[x] != null) { if (startingXs[x].equals(start)) { result = prices[x]; } } } return result; } /** * add a toll for an exchange that hasn't been priced yet */ public boolean addPrice(EzPassInterchangeArrays start, double price) { // check that the price is valid if ((price < 0.10) || (price > 50.00)) { // anything under a dime is not worth collecting // and I don't believe anything over $50 return false; } else { // check if the interchange already has a price double toll = this.tollAmount(start); // protect against already there if (toll > 0) { return false; } else { // price not found - let's add it // find first empty entry int loc = 0; while ((loc < startingXs.length) && (startingXs[loc] != null)) { loc++; } // loc now marks the place to add entry // (protect against out of space) if (loc == startingXs.length) { return false; } else { startingXs[loc] = start; prices[loc] = price; return true; } } } } /** * remove a price for a given starting interchange. * this operation is not currently supported by the parallel arrays version */ public boolean deletePrice(EzPassInterchangeArrays start) { System.out.println("this operation is not currently supported by the parallel arrays version"); return true; } /** * change the price for a given existing interchange */ public boolean changePrice(EzPassInterchangeArrays start, double newPrice) { // look up toll // find correct entry int loc = 0; int found = -1; // initially indicate not found // loop until reach end of array, end of contents of array while ((loc < startingXs.length) && (startingXs[loc] != null)) { // find entry? if (startingXs[loc].equals(start)) { found = loc; } loc++; } // found now marks the place to change entry // (protect against not found) if (found < 0) { return false; } else { // price found - change the value - replace with new value prices[found] = newPrice; return true; } } /** * increase the price by a given pct for a given existing interchange */ public boolean increasePrice(EzPassInterchangeArrays start, double pctIncr) { // look up toll double oldPrice = this.tollAmount(start); double newPrice = oldPrice + (pctIncr/100) * oldPrice; // protect against not found if (oldPrice < 0) { return false; } else { // price found - change the value - replace with new value boolean res = this.changePrice(start, newPrice); return res; } } /** * would like a method to increase all prices by a given pct, but haven't done it yet */ /** * generate a string represnetation for an interchange */ public String toString() { // introduce decimal format //DecimalFormat dollarFormat = new DecimalFormat("0.##"); String res = super.toString(); res += " Ending here - Price List for starting at: \n "; for (int x = 0; x < startingXs.length; x++) { if (startingXs[x] != null) { //res += startingXs[x].getName() + ": " + dollarFormat.format(prices[x]) + " "; res += startingXs[x].getName() + ": " + prices[x] + " "; } } return res; } /** * check if invoking interchange has the same contents as the passed interchange */ public boolean equals(Object obj) { if (!(obj instanceof EzPassInterchangeArrays)) { // 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)) { // && // comparing all prices will be very challenging - and may not be what we want while building the price list //price == ((EzPassBridge) toCompare).getPrice() ) { return true; } else { return false; } } } /** * @param args the command line arguments */ public static void main(String[] args) { // simulate NJ Turnpike EzPassInterchangeArrays [] all = new EzPassInterchangeArrays[10]; String namestem = "NJTurnEx"; String citystem = "City"; String state = "NJ"; String hwy = "NJ Turnpike"; char dir = 'N'; for (int i = 0; i < 10; i++) { all[i] = new EzPassInterchangeArrays(namestem + i, citystem + i, state, hwy, 2, dir); } for (int x1 = 0; x1 < 10; x1++) { for (int x2 = 0; x2 < 10; x2++ ) { if (x1 != x2) { if (x1 > x2) { all[x1].addPrice(all[x2], (x1 - x2) * 0.30); } else { all[x1].setDirection('S'); all[x1].addPrice(all[x2], (x2 - x1) * 0.30); } } } } // show interchanges for (int j = 0; j < 10; j++) { System.out.println(all[j]); } // show all prices for (int ex1 = 0; ex1 < 10; ex1++) { for (int ex2 = 0; ex2 < 10; ex2++ ) { System.out.println(((EzPassLocation)all[ex2]).getName() + " to " + ((EzPassLocation)all[ex1]).getName() + " Toll: " + all[ex1].tollAmount(all[ex2])); } } // change prices for (int x1 = 0; x1 < 10; x1++) { for (int x2 = 0; x2 < 10; x2++ ) { if (x1 != x2) { all[x1].increasePrice(all[x2],10.0); } } } // show all prices for (int ex1 = 0; ex1 < 10; ex1++) { for (int ex2 = 0; ex2 < 10; ex2++ ) { System.out.println(((EzPassLocation)all[ex2]).getName() + " to " + ((EzPassLocation)all[ex1]).getName() + " Toll: " + all[ex1].tollAmount(all[ex2])); } } } }