import java.util.Scanner; /** * This class calculates cost of several trips using while looop. */ public class SampleExam1Question5 { /** * main does all the work, no methods necessary. * put into a while loop: * 1. read in data * 2. compute base price * 3. use if to check for discount * 4. compute final price * Outside of loop: print total # of trips and nodiscount */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int idnum, miles, noDiscount=0, numTrips=0; double pricePerMile; idnum=sc.nextInt(); while(idnum != -1) { numTrips++; // 1. read in data miles = sc.nextInt(); pricePerMile = sc.nextDouble(); // 2. compute base price double basePrice = miles*pricePerMile; System.out.printf("Base price for trip: %.2f.\n", basePrice); // 3. check for discount double discount=0; if (miles >= 20) { discount = .075*basePrice; System.out.printf("you received a discount of %.2f.\n",discount); } else { System.out.println("You did not receive the discount."); noDiscount++; } // 4. compute final price double finalPrice = basePrice - discount; // 5. Repeat: use structure read loop System.out.print("Enter ID number of trip, -1 to exit: "); idnum = sc.nextInt(); }// end while // 6. Print totals System.out.println("Total number of trips is: " + numTrips + " number of trips without a discount is: " + noDiscount); } }