/** Travel.java is the answer to Q5 on Sample Exam 1 */ import java.util.Scanner; public class Travel { /** read in info about trips and calculate prices */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numTrips=0, noDiscount=0; // Part 1 and 5. Loop and read until end of input using // Scanner method hasNext System.out.println("Enter ID number, ^D to exit: "); while(sc.hasNext()) { int idnum = sc.nextInt(); double miles = sc.nextDouble(); double pricePerMile = sc.nextDouble(); // Part 2: Compute and print base price double basePrice = miles*pricePerMile; // Part 3 and 4: calculate discount final double DISCOUNT = .075; double finalPrice = basePrice; if (miles >= 20) { System.out.println("The trip gets a discount of " + DISCOUNT*basePrice + "."); finalPrice = basePrice - DISCOUNT*basePrice; } else { System.out.println("The trip does not get a discount."); noDiscount++; // counter for Part 6 } numTrips++; // counter for Part 6 System.out.println("id: " + idnum + " final price: " + finalPrice); } // end while // Part 6: Print counters System.out.println(numTrips + " trips, " + noDiscount + " trips without a discount."); } }