Problem S(1 point. Instructor\'s Initials A grocery store owner has asked you to
ID: 3701995 • Letter: P
Question
Problem S(1 point. Instructor's Initials A grocery store owner has asked you to develop a program for use in the checkout process. The program should a) Prompt the cashier to enter the cost of the first item b) Continue to prompt for additional items until the cashier enters 0 c) Display the total d) Prompt for the amount the customer submits as payment e Display the change due All money displays should have dollar signs and 2 digits after the decimal (i.e. $27.32, not 27.3200000). If the cashier accidently enters a negative value, the program should not accept the value as part of the total, nor should it stop or crash. Instead it should remind the cashier that there are no negative prices in the store, and keep working (hint: use the continue instruction). Demonstrate operation to the instructor, and print a copy of the program to attach to the lab.Explanation / Answer
Bill.java
import java.util.Scanner;
public class Bill {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double total = 0;
System.out.print("Enter the cost of the item(0 to stop): ");
double n = scan.nextDouble();
while(n!=0){
total = total + n;
System.out.print("Enter the cost of the item(0 to stop): ");
n = scan.nextDouble();
}
System.out.println("Total: "+total);
System.out.print("Enter the payment: ");
double payment = scan.nextInt();
while(payment<=0) {
System.out.print("Invalid input. Please enter the payment: ");
payment = scan.nextInt();
}
System.out.printf("The change due is %.2f ",(payment-total));
}
}
Output:
Enter the cost of the item(0 to stop): 11.1
Enter the cost of the item(0 to stop): 22.2
Enter the cost of the item(0 to stop): 33.3
Enter the cost of the item(0 to stop): 4.4
Enter the cost of the item(0 to stop): 1
Enter the cost of the item(0 to stop): 2
Enter the cost of the item(0 to stop): 3
Enter the cost of the item(0 to stop): 4
Enter the cost of the item(0 to stop): 5
Enter the cost of the item(0 to stop): 0
Total: 86.0
Enter the payment: 100
The change due is 14.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.