In: Computer Science
jgrasp environment, java
write a complete program that calculates a restaurant bill. Prompt the user for the check amount, then ask the user what type of tipp they would like to leave: the choices are 1 for good tip (20%), 2 for an average tip (15%), or 3 for poor tip (10%). Use their input and an if-else to calculate the tip amount. Then calculate and output the final bill:
check+tip
print out the bill, exactly as shown (print the dollar sign as a separate character before the dollar amounts.)
sample output:
total check: $20.0
tax: $1.4
tip (good): $4.0
final bill
both tax and tip are calculated using the initial check amount (dont tax the tip, and dont use ta when calculating the tip). without special formatting, the dollar amounts will not have two decimal places of precision, that is fine for this project.
Thank you.
import java.util.*;
class Bill
{
public static void main (String[] args)
{
Scanner input = new
Scanner(System.in);
String type = "";
double tax,tip;
tip = 0.0;
System.out.println("Enter the check
amount : ");
double check =
input.nextDouble();
tax = check*0.07;
System.out.println("Enter the type
of tip <1--- good tip(20%) , 2--- average tip(15%), 3--- poor
tip(10%) : ");
int tipType =
input.nextInt();
if(tipType == 1)
{
type =
"good";
tip =
check*0.2;
}
else if(tipType == 2)
{
type =
"average";
tip =
check*0.15;
}
else if(tipType == 3)
{
type =
"poor";
tip =
check*0.1;
}
System.out.println("total check:
$"+check);
System.out.printf("tax:
$%.2f",tax);
System.out.println("\ntip
("+type+"): $"+tip);
System.out.println("Final bill :
$"+(check+tip));
}
}
Output:
Enter the check amount : 20.0 Enter the type of tip <1--- good tip(20%) , 2--- average tip(15%), 3--- poor tip(10%) : 1 total check: $20.0 tax: $1.40 tip (good): $4.0 Final bill : $24.0
Do ask if any doubt. Please upvote.