Answer to Question #200222 in Java | JSP | JSF for sana

Question #200222

Suppose that you are hired by a High-End “Gola Ganda” shop, this shop offers Gola Gandas of all shapes and sizes. They have basic gola gandas of three sizes: Small, Medium and Large. All have different prices  

A customer can add as many toppings on it as they want, the toppings could be more “Sauce”, Condensed Milk, Nutella, etc (more can be added). Every topping has a different cost, and the cost of each topping will be added on top of the cost of the size of gola ganda.  

For example, if a small gola ganda cost, 50 rupees, and a topping of condensed milk cost 10 rupees, and a topping of Nutella cost 20 rupees, and if a customer has ordered a small gola ganda with condensed milk and Nutella then the total cost will be 80 rupees (50 for small gola ganda, 10 for condensed milk, and 20 for Nutella).  

 


1
Expert's answer
2021-05-29T01:34:32-0400
import java.util.Scanner;
public class Q200222 {
	public static void main(String[] args) {
		// Scanner object
		Scanner in = new Scanner(System.in);
		int totalPrice = 0;
		// size prices
		final int SMALL_PRICE = 50;
		final int MEDIUM_PRICE = 60;
		final int LARGE_PRICE = 70;
		// toppings prices
		final int SAUCE_PRICE = 5;
		final int MILK_PRICE = 10;
		final int NUTELLA_PRICE = 20;
		int size = 0;
		// get size
		while (size < 1 || size > 3) {
			System.out.println("1. Small - 50 rupees");
			System.out.println("2. Medium - 60 rupees");
			System.out.println("3. Large - 70 rupees");
			System.out.print("Select gola gandas size: ");
			size = in.nextInt();
		}
		if (size == 1) {
			totalPrice += SMALL_PRICE;
		}
		if (size == 2) {
			totalPrice += MEDIUM_PRICE;
		}
		if (size == 3) {
			totalPrice += LARGE_PRICE;
		}
		// get toppings
		String answer = "y";
		while (answer.compareToIgnoreCase("y") == 0) {
			in.nextLine();
			answer = "";
			while (answer.compareToIgnoreCase("y") != 0 && answer.compareToIgnoreCase("n") != 0) {
				System.out.print("Do you want to add toppings? y/n: ");
				answer = in.nextLine();
			}
			if (answer.compareToIgnoreCase("y") == 0) {
				int toppings = 0;
				while (toppings < 1 || toppings > 3) {
					System.out.println("1. Sauce - 5 rupees");
					System.out.println("2. Condensed Milk - 10 rupees");
					System.out.println("3. Nutella - 20 rupees");
					System.out.print("Select topping: ");
					toppings = in.nextInt();
				}
				if (toppings == 1) {
					totalPrice += SAUCE_PRICE;
				}
				if (toppings == 2) {
					totalPrice += MILK_PRICE;
				}
				if (toppings == 3) {
					totalPrice += NUTELLA_PRICE;
				}
			}
		}
		// Display the total price
		System.out.println("The total price: " + totalPrice + " rupees");
		// close Scanner
		in.close();
	}
}




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