Question: Please help me with my case A and creating arrays using [] to store the user data. I need to be able to print out
Please help me with my case A and creating arrays using [] to store the user data. I need to be able to print out a statment such as this with the ham and pineapple having been scanner inputs
What topping would you like?
Your options are: Pepperoni, Mushroom, Chicken, Ham, Pineapple, Sausage, Basil, Olive.
Ham
Chosen topping Ham added.
What topping would you like?
Your options are: Pepperoni, Mushroom, Chicken, Ham, Pineapple, Sausage, Basil, Olive.
Pineapple
Chosen topping Pineapple added.
You have ordered Hawaiian with toppings:
Ham, Pineapple.
This pizza costs: $15.65.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Pizzeria {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// variables
String userName;
String pizzaName;
double diameter;
double area = 0;
char totalDisplay = 0;
char pepperoniChoice;
double total = 0.0;
int numPizzas = 0;
final double cheesePerInch = 0.0272;
final double saucePerInch = 0.0316;
final double doughPerInch = 0.0228;
final double toppingPerInch = 0.0284;
String[] pizzaNames = new String[50]; // Array to store pizza names
double[] pizzaPrices = new double[50]; // Array to store pizza prices
//array list of valid toppings
String[] validToppings = {"Pepperoni", "Mushroom", "Chicken", "Ham", "Pineapple", "Sausage", "Basil", "Olive"};
//user intro
System.out.println("Welcome to Adkins Pizzeria!");
System.out.println("Can I have your name please?");
userName = scnr.nextLine();
System.out.println("Ok, let's start your order, " + userName + ".");
//Menu
while (true) {
System.out.println("Here is what you can do next:");
System.out.println("MENU");
System.out.println("a - Add a pizza");
System.out.println("t - Print the total");
System.out.println("r - Read the order");
System.out.println("q - Quit");
System.out.println("Please make a selection:");
String selection = "";
//read users selection and store it into variable for switch statement
selection = scnr.nextLine();
switch (selection) {
case "a":
numPizzas++;
System.out.println(" What would you like to name pizza number " + numPizzas + "?");
pizzaName = scnr.nextLine();
System.out.println("What size would you like " + pizzaName + " to be?");
System.out.println("We can handle pizzas between 8 and 48 inches.");
diameter = scnr.nextDouble();
while (diameter < 8 || diameter > 48) {
System.out.println("Invalid diameter. Please enter a value between 8 and 48 inches.");
diameter = scnr.nextDouble();
}
scnr.nextLine(); // Consume the remaining newline character
System.out.println("How many toppings would you like on this pizza?");
System.out.println("You can have between 0-8 toppings.");
int numToppings = scnr.nextInt();
while (numToppings < 0 || numToppings > 8) {
System.out.println("Invalid number of toppings. Please enter a value between 0 and 8.");
numToppings = scnr.nextInt();
}
scnr.nextLine(); // Consume the remaining newline character
String[] = toppings = new String [numToppings];
for (int j = 0; j < numToppings; j++) {
System.out.println("What topping would you like?");
System.out.println("Your options are: Pepperoni, Mushroom, Chicken, Ham, Pineapple, Sausage, Basil, Olive.");
topping = scnr.nextLine();
toppings[j] = topping;
while (!Arrays.asList(validToppings).contains(topping)) {
System.out.println("Invalid topping. Your options are: Pepperoni, Mushroom, Chicken, Ham, Pineapple, Sausage, Basil, Olive.");
topping = scnr.nextLine();
}
toppings.add(topping);
System.out.println("Chosen topping " + topping + " added.");
System.out.println();
double pizzaCost = Math.PI * Math.pow(diameter / 2.0 , 2) * (cheesePerInch + saucePerInch + doughPerInch + numToppings * toppingPerInch);
total += pizzaCost;
System.out.println("You have ordered " + pizzaName + " with toppings: " + toppings[j] + ", ");
System.out.printf("This pizza costs $%.2f", total);
}
break;
case "t":
System.out.println("print total" + total);
//code
break;
case "r":
System.out.println("read the order");
//code
break;
case "q":
System.out.println("quit");
System.exit(0);
break;
default:
System.out.println("invalid please try again");
selection = scnr.nextLine();
break;
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
