/** ups1.java (adds loop to handle multiple packages) * * CS7 - practicing with java functions * * This program asks the user for package details and produces a * classification according to the ups rules. To find out more * about these guidelines, visit: * * http://www.ups.com/content/us/en/resources/prepare/weight_size.html * * Issues: * - DOUBLES SHOULD PROBABLY BE USED FOR EXTRA PRECISION */ // Javadoc comments are also used. See p.131 in JBD. import tio.*; class ups1 { /** main() handles the top level control. It calls various * methods to determine the proper classification for the * user's packages. */ public static void main (String[] args) { char answer='y'; // user's choice. int weight, height, width, length; while (answer=='y' || answer=='Y') { // get the package specifications System.out.println("Please enter the package specifications."); weight = getPosInt(" weight (lbs): "); height = getPosInt(" height (in): "); width = getPosInt(" width (in): "); length = getPosInt(" length (in): "); System.out.println(); // for testing System.out.println("Got: h=" + height + " wt=" + weight + " wd=" + width + " ln=" + length); System.out.println(); System.out.print("Do you have another package to classify? (y/n) "); Console.in.readLine(); // flush out the CR answer = (char) Console.in.readChar(); } } // end main() /** getPosInt() reads an integer from the user and rereads while the * number is not positive. * * @param prompt The string to present to the user prior to input. * @return The positive integer. */ public static int getPosInt(String prompt) { System.out.print(prompt); int n = Console.in.readInt(); while (n <= 0) { System.out.print("Please enter a postive value: "); n = Console.in.readInt(); } return n; } // end getPosInt() } // end ups class /* SAMPLE RUN: * $ java ups1 Please enter the package specifications. weight (lbs): 18 height (in): 0 Please enter a postive value: 23 width (in): 29 length (in): 44 Got: h=23 wt=18 wd=29 ln=44 Do you have another package to classify? (y/n) y Please enter the package specifications. weight (lbs): 1 height (in): 2 width (in): 3 length (in): 4 Got: h=2 wt=1 wd=3 ln=4 Do you have another package to classify? (y/n) n $ */