In: Computer Science
Write a Java application for a gas station to calculate the total payment for customer.
Your program should display a menu and get customer’s input: ****************************
Welcome to XYZ gas station! Select type of gas (enter R, P, S) Regular - R. plus - P. Super -S: ****************************
Enter the amount of gas in gallons:
In your code, define regular price: $2.45, plus: $2.65, super $2.95 as constant.
Use 7% as sale tax.
Calculate gas subtotal = sale price * gas amount Tax=subtotal * sale tax. Total payment = subtotal + Tax Output nicely to customer the total amount payment:
######################################################
Your payment for (Regular/plus/Super) is $###.
######################################################
Requirements:
Use switch statement for type of gas (case ‘R’, case ‘P’, case ‘S’ , etc)
Use while loop to display the menu. If user inputs anything other than R,P,S, display the menu again.
Use for loop statement to draw lines(**********… and ##############...)
import java.util.Scanner;
public class GasStation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// declarations
double reg = 2.45, plus = 2.65, sup = 2.95;
double saleTax = 0.07;
double gasSubtotal = 0.0;
double tax = 0.0;
double totalPayement = 0.0;
char ch;
do{ // menu until valid input is not given
for (int i = 0; i < 30; i++) {
System.out.print("*");
}
System.out.println("\nWelcome to XYZ gas station!");
for (int i = 0; i < 30; i++) {
System.out.print("*");
}
System.out.print("\nSelect type of gas (enter R, P, S) Regular - R.
plus - P. Super -S: ");
ch = sc.next().charAt(0);
if(ch == 'R'||ch =='S'|| ch =='P') break; // only break when input
is correct
}while(true);
System.out.print("Enter the amount of gas in gallons: ");
double gasAmount = sc.nextDouble(); // take amount
switch(ch){
case 'R': gasSubtotal = reg * gasAmount;
tax = gasSubtotal * saleTax;
totalPayement = gasSubtotal + tax;
break;
case 'P': gasSubtotal = plus * gasAmount;
tax = gasSubtotal * saleTax;
totalPayement = gasSubtotal + tax;
break;
case 'S': gasSubtotal = sup * gasAmount;
tax = gasSubtotal * saleTax;
totalPayement = gasSubtotal + tax;
break;
}
System.out.println("");
for (int i = 0; i < 50; i++) {
System.out.print("#");
}
System.out.println("\nYour payment for (Regular/plus/Super) is
$"+totalPayement);
for (int i = 0; i < 50; i++) {
System.out.print("#");
}
System.out.println("");
}
}
/* OUTPUT */
/* PLEASE UPVOTE */