87.4k views
2 votes
The Gas-N-Clean Service Station sells gasoline and has a car wash. Fees for the car wash are $1.25 with a gasoline purchase of $10.00 or more and $3.00 otherwise. Three kinds of gasoline are available: regular at $2.89, plus at $3.09, and super at $3.39 per gallon. User Request:

Write a program that prints a statement for a customer. Analysis:
Input consists of number of gallons purchased (R, P, S, or N for no purchase), and car wash desired (Y or N). Gasoline price should be program defined constant. Sample output for these data is
Enter number of gallons and press 9.7
Enter gas type (R, P, S, or N) and press R
Enter Y or N for car wash and press Y
**************************************
* *
* *
* Gas-N-Clean Service Station *
* *
* March 2, 2004 *
* * ************************************** Amount Gasoline purchases 9.7 Gallons Price pre gallons $ 2.89 Total gasoline cost $ 28.03 Car wash cost $ 1.25 Total due $ 29.28 Thank you for stopping Pleas come again Remember to buckle up and drive safely

User RiaanDP
by
3.9k points

1 Answer

3 votes

Answer:

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

//print the header

System.out.println("**************************************");

System.out.println("* *");

System.out.println("* *");

System.out.println("* Gas-N-Clean Service Station *");

System.out.println("* *");

System.out.println("* March 2, 2004 *");

System.out.println("* *");

System.out.println("**************************************");

//set the constant values for gasoline

final double REGULAR_GASOLINE = 2.89;

final double PLUS_GASOLINE = 3.09;

final double SUPER_GASOLINE = 3.39;

// initialize the variables as 0

double gasolinePrice = 0, gasolineCost = 0, carWashCost = 0, totalCost = 0;

Scanner input = new Scanner(System.in);

//ask the user to enter the gallons, gas type and car wash choice

System.out.print("Enter number of gallons ");

double gasolinePurchase = input.nextDouble();

System.out.print("Enter gas type (R, P, S, or N) ");

char gasType = input.next().charAt(0);

System.out.print("Enter Y or N for car wash ");

char carWashChoice = input.next().charAt(0);

//check the gas type. Depending on the choice set the gasolinePrice from the corresponding constant value

if(gasType == 'R')

gasolinePrice = REGULAR_GASOLINE;

else if(gasType == 'P')

gasolinePrice = PLUS_GASOLINE;

else if(gasType == 'S')

gasolinePrice = SUPER_GASOLINE;

//calculate the gasolineCost

gasolineCost = gasolinePurchase * gasolinePrice;

//check the carWashChoice. If it is yes and gasolineCost is greater than 10, set the carWashCost as 1.25. Otherwise, set the carWashCost as 3.00

if(carWashChoice == 'Y'){

if(gasolineCost >= 10)

carWashCost = 1.25;

else

carWashCost = 3.00;

}

//calculate the total cost, add gasolineCost and carWashCost

totalCost = gasolineCost + carWashCost;

//print the values in required format

System.out.println("Amount Gasoline purchases " + gasolinePurchase);

System.out.println("Gallons Price per gallons $ " + gasolinePrice);

System.out.printf("Total gasoline cost $ %.2f\\", gasolineCost);

System.out.println("Car wash cost $ " + carWashCost);

System.out.printf("Total due $ %.2f\\", totalCost);

System.out.println("Thank you for stopping\\Please come again \\Remember to buckle up and drive safely");

}

}

Step-by-step explanation:

*The code is in Java.

Please see the comments in the code for explanation

User Itzik
by
4.0k points