import java.io.*; public class CompuDuds { public static void displayPrice(int totalCents){ int dollars = totalCents / 100; int remainingCents = totalCents % 100; System.out.print("$"); System.out.print(dollars); if(remainingCents < 10) System.out.print(".0"); else System.out.print("."); System.out.print(remainingCents); } public static int inputIntegerInRange(int min, int max){ System.out.print("(enter "); System.out.print(min); System.out.print("-"); System.out.print(max); System.out.println(")"); String inputAsString; int inputAsInt; try{ inputAsString = new DataInputStream(System.in).readLine(); }catch(IOException e){ inputAsString = null; // the dumb java compiler needs this System.err.print("Problem reading input: "); System.err.println(e); System.exit(1); } if(inputAsString == null){ // this means end of file on input System.exit(0); // handle as a normal peaceful program termination } try{ inputAsInt = Integer.parseInt(inputAsString); }catch(NumberFormatException e){ System.err.println("input must be an integer and wasn't"); return inputIntegerInRange(min, max); } if(inputAsInt < min || inputAsInt > max){ System.err.println("input out of range"); return inputIntegerInRange(min, max); }else{ return inputAsInt; } } public static String inputSelection(String[] choices){ for(int number = 1; number <= choices.length; number = number + 1){ System.out.print(" "); System.out.print(number); System.out.print(") "); System.out.println(choices[number - 1]); } return choices[inputIntegerInRange(1, choices.length) - 1]; } public static void main(String[] commandLineArgs){ ItemList itemList = new ItemList(); while(true){ // infinite loop terminated by program exiting System.out.println(); System.out.println("What would you like to do?"); System.out.println(" 1) Exit this program."); System.out.println(" 2) Add an item to your selections."); System.out.println(" 3) List the items you have selected."); System.out.println(" 4) See the total price of the items you have selected."); int option; if(itemList.empty()){ option = inputIntegerInRange(1, 4); }else{ System.out.println(" 5) Delete one of your selections."); System.out.println(" 6) Revise the specifics of a selected item."); option = inputIntegerInRange(1, 6); } if(option == 1){ System.exit(0); }else if(option == 2){ Item item = inputItem(); itemList.add(item); item.inputSpecifics(); }else if(option == 3){ itemList.display(); }else if(option == 4){ displayPrice(itemList.totalPrice()); System.out.println(); }else if(option == 5){ itemList.delete(itemList.choose()); }else if(option == 6){ itemList.choose().reviseSpecifics(); } } } static Item inputItem(){ System.out.println("What would you like?"); System.out.println(" 1) Chinos"); System.out.println(" 2) Oxford shirt"); if(inputIntegerInRange(1, 2) == 1) return new Chinos(); else return new OxfordShirt(); } }