// This file contains excerpts from the textbook Concrete // Abstractions: An Introduction to Computer Science Using Scheme, by // Max Hailperin, Barbara Kaiser, and Karl Knight, Copyright (c) 1998 // by the authors. Full text is available for free at // http://www.gustavus.edu/+max/concrete-abstractions.html 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 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 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 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(); } } } private 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(); } } private static java.io.BufferedReader reader = new java.io.BufferedReader (new java.io.InputStreamReader(System.in)); 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 = null; try{ inputAsString = reader.readLine(); } catch(java.io.IOException e){ 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 program termination } int inputAsInt; 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]; } }