Answer to Question #262378 in Java | JSP | JSF for TayTayyy

Question #262378

Provide a method calculatePrice() that calculates and returns the price of this sandwich according to the rates given in the above table. E.g. the price of a sandwich with 3 bread slices, 2 cheese slices, 2 tomato slices, 3 patties, mustard, no ketchup, no ice berg with grilling will be calculated as 3 * 20 + 2 * 30 + 2 * 5 + 3 * 70 + 5+ 0 + 0 + 10 . Provide a method applyDiscount(double disc) that calculates the price, applies the discount % given in the argument and returns the discounted price of the sandwich.E.g. if the method is called on a sandwich whose real price returned by the calculatePrice function is 550 and applyDiscount(10) is called, then a ten percent discount will be calculated as 550 * 10/100 = 55. Now this function will return the discounted price as 550 – 55 = 495. (Please note that this is just an example, do not hard code these values in your function).  


1
Expert's answer
2021-11-07T10:12:10-0500


import java.util.Scanner;


public class App {
	static Scanner keyBoard = new Scanner(System.in);


	public static void main(String[] args) {


		double discount = -1;
		while (discount < 0 || discount > 1000000) {
			System.out.print("Enter discount: ");
			discount = keyBoard.nextDouble();
		}


		System.out.println("The discounted price: " + applyDiscount(discount));
		keyBoard.close();
	}


	static double calculatePrice() {
		int numberBreads = -1;
		while (numberBreads < 0 || numberBreads > 1000000) {
			System.out.print("Enter number of bread slices: ");
			numberBreads = keyBoard.nextInt();
		}
		int numberCheeses = -1;
		while (numberCheeses < 0 || numberCheeses > 1000000) {
			System.out.print("Enter number of cheese slices: ");
			numberCheeses = keyBoard.nextInt();
		}
		int numberTomatos = -1;
		while (numberTomatos < 0 || numberTomatos > 1000000) {
			System.out.print("Enter number of tomato slices: ");
			numberTomatos = keyBoard.nextInt();
		}
		int numberPatties = -1;
		while (numberPatties < 0 || numberPatties > 1000000) {
			System.out.print("Enter number of patties: ");
			numberPatties = keyBoard.nextInt();
		}
		int mustard = -1;
		while (mustard < 0 || mustard > 1) {
			System.out.print("Do you want to add mustard? 1-yes, 0-no: ");
			mustard = keyBoard.nextInt();
		}
		mustard *= 5;
		int ketchup = -1;
		while (ketchup < 0 || ketchup > 1) {
			System.out.print("Do you want to add ketchup? 1-yes, 0-no: ");
			ketchup = keyBoard.nextInt();
		}
		ketchup *= 5;


		int iceberg = -1;
		while (iceberg < 0 || iceberg > 1) {
			System.out.print("Do you want to add ice berg? 1-yes, 0-no: ");
			iceberg = keyBoard.nextInt();
		}
		iceberg *= 5;
		int grillin = -1;
		while (grillin < 0 || grillin > 1) {
			System.out.print("Is the sandwich with grillin? 1-yes, 0-no: ");
			grillin = keyBoard.nextInt();
		}
		grillin *= 10;


		// the price of a sandwich with 3 bread slices, 2 cheese slices, 2 tomato
		// slices, 3 patties, mustard, no ketchup, no ice berg with grillin
		double priceSandwich = numberBreads * 20 + numberCheeses * 30 + numberTomatos * 5 + numberPatties * 70 + mustard
				+ ketchup + iceberg + grillin;


		System.out.println("The price of a sandwich: " + priceSandwich);
		return priceSandwich;
	}


	static double applyDiscount(double disc) {
		double price = calculatePrice();
		return price -(price * disc) / 100.0;
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS