A store owner asks you to write a program for use in the che
A store owner asks you to write a program for use in the checkout process. The program should:
• Prompt the user to enter the cost of the rst item.
• Continue to prompt for additional items, until the user enters 0. • Display the total.
• Prompt for the dollar amount the customer submits as payment. • Display the change due.
Solution
Bill.java
import java.util.Scanner;
public class Bill {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int total = 0;
while(true){
System.out.print(\"Enter the cost of the rst item(0 to stop): \");
int n = scan.nextInt();
if(n==0){
break;
}else{
total = total + n;
}
}
System.out.println(\"Total: \"+total);
System.out.print(\"Enter the dollar amount as payment: \");
int payment = scan.nextInt();
System.out.println(\"The change due is \"+(payment-total));
}
}
Output:
Enter the cost of the rst item(0 to stop): 50
Enter the cost of the rst item(0 to stop): 60
Enter the cost of the rst item(0 to stop): 70
Enter the cost of the rst item(0 to stop): 80
Enter the cost of the rst item(0 to stop): 30
Enter the cost of the rst item(0 to stop): 0
Total: 290
Enter the dollar amount as payment: 300
The change due is 10
