/** * A simple cash register program * Program will repeatedly prompt (ask) if user has any more items to purchase * if yes, user is prompted for unitPrice, quantity * and a subtotal is output * if no, then loop terminates and the total is printed out * * This program will demonstrate various forms of input AND output!! */ import java.util.*; public class CashRegister { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int quantity; double unitPrice; String keepGoing; double total=0.0; keepGoing="Y"; // need an initial value for keepGoing // so that boolean expression inside while // makes sense. This assumes there is at least 1 item while(keepGoing.toUpperCase().equals("Y")) { System.out.print("\nEnter unit price: "); //Prompt for unit price and input unitPrice=sc.nextDouble(); System.out.print("Enter quantity :"); //Prompt for qunatity and input quantity=sc.nextInt(); sc.nextLine(); // why is this here ??? Read book or come to class for answer! System.out.printf("\nSubtotal : %8.2f\n",unitPrice*quantity); total=unitPrice*quantity; //update total System.out.print("Do you want to continue [Y/N]: " ); keepGoing=sc.nextLine(); } System.out.printf("\nTotal Due : %9.2f",total); //print out total due } }