In: Computer Science
Write a JAVA program that prompts a famer's market associate for the price of each item purchased by the customer and calculate their total. Use a do while loop to allow for the associate to enter as many items as necessary. Use 0 as a sentinel. If the customer spends at least $50, they receive a 15% discount. If the customer qualifies for the discount, output a statement informing them of this as well as their new total with the discount applied. If the customer does not qualify, only output their total. Be sure to format your output.
Java code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
//initializing item and
total
float item,total=0;
do{
//asking for price
System.out.print("Enter price of item: ");
//accepting it
item=input.nextFloat();
//adding it to total
total+=item;
//looping till 0 is entered
}while(item!=0);
//printing Total
System.out.println("Total:
$"+Math.round(total*100)/100.0);
//checking if total is
atleast 50
if(total>=50)
//printing total with
discount
System.out.println("Total with discount:
$"+(total-Math.round(total*100*15.0/100.0)/100.0));
}
}
Screenshot:
Input and Output: