CS302 Fall 2012 Exam 2 Solution // Part I: Write a complete program import java.util.Scanner; public class DairyDelivery { public static void main(String[] args) { final double MILK_PRICE = 2.75; final double BOTTLE_DEPOSIT = 1.50; final int MIN_ORDER = 3; final double DISC1 = 10.0; // DISCOUNT LEVELS final double DISC2 = 20.0; final double DELIVERY = 2.5; // $ 0- 9.99 is $2.50 final double DELIVERY1 = 1.5; // $10-19.99 is $1.50 final double DELIVERY2 = 0; // $20+ is $0.00 Scanner stdin = new Scanner(System.in); int halfgals = -1, bottles = -1; // Get number of bottles ordered while ( halfgals < MIN_ORDER ) { System.out.print("Number of half gallons? "); halfgals = stdin.nextInt(); if (halfgals < MIN_ORDER) { System.out.println( "Error! Please enter an int >= "+MIN_ORDER); } } // Get number of empty bottles returned while ( bottles < 0 ) { System.out.print("Number of bottles returned? "); bottles = stdin.nextInt(); if ( bottles < 0 ) { System.out.println( "Error! Please enter an int >= 0"); } } // Compute invoice items double milk = halfgals * MILK_PRICE; double deposit = halfgals * BOTTLE_DEPOSIT; double refund = bottles * BOTTLE_DEPOSIT; double delivery = 2.50; // delivery chg is $2.50 if ( milk >= DISC2 ) delivery = DELIVERY2; // if >= 20, chg is FREE else if ( milk >= DISC1 ) delivery = DELIVERY1; // else if >= 20, chg is $1.50 double ppg = (milk + delivery) / (halfgals / 2.0); // price per gallon System.out.println( "Milk Subtotal: $" + milk + "\n" + "Bottle Deposit Charge: + " + deposit + "\n" + "Bottle Return Refund: - " + refund + "\n" + "Delivery Charge: + " + delivery + "\n" + "Total Bill: = $" + (milk+deposit-refund+delivery) + "\n" + "Cost Per Gallon: = $" + ppg + "/gal" ); } // end main } // end class