In my code, I am trying to allow the customer to order cheesecake topping and size then give them the price of that cheesecake then add the total to a running total until they finish ordering then give them grand total order at the end when they are finished ordering.
When the code runs it gets to end gives total but no matter whether I hit yes or no on the second time around it immediately goes to cake size instead of letting me choose my flavor again and never ends to let me get my grand total.
float biteSize = 3;
float small = 9;
float large = 12;
String chosenSize = "";
double pricePerInch = 0;
double total = 0;
String userFlavor = "";
String[] chooseSizes = {"bite size", "small", "large"};
String[] chooseFlavors = {"plain", "strawberry", "raspberry", "caramel", "chocolate"};
boolean flavorFound = false;
String want = "";
double newTotal = 0;
Scanner scnr = new Scanner(System.in);
System.out.println("Flavors to choose from: plain, strawberry, raspberry, caramel, chocolate."); //giving user flavor list
while (want != "n") {
while (!flavorFound) {
System.out.println("Please choose flavor:"); // Loop until flavorFound is true
userFlavor = scnr.nextLine();
for(int i = 0; i < chooseFlavors.length; i++) { // Loop from 0 to choosFlavors.length
if(userFlavor.equals(chooseFlavors[i])) { // Compare user input to flavor at index i
System.out.println(userFlavor);
flavorFound = true;
// This is the flag to break out of while loop
break;
// Only breaks out of for loop
} else {
System.out.println("Please choose from flavors above.");
}
}
}
if (userFlavor.contains("plain")) { // setting prices for all the flavors
pricePerInch = 0.50;
}
else if (userFlavor.contains("strawberry")) {
pricePerInch = 1.25;
}
else if (userFlavor.contains("raspberry")) {
pricePerInch = 1.15;
}
else if (userFlavor.contains("caramel")) {
pricePerInch = 0.75;
}
else if (userFlavor.contains("chocolate")) {
pricePerInch = 0.85;
}
System.out.println("Sizes to choose from: bite size, small, large");
boolean sizeFound = false;
while (!sizeFound) {
// Loop until flavorFound is true
System.out.println("Please choose cheese cake size: ");
chosenSize = scnr.nextLine();
for(int i = 0; i < chooseSizes.length; i++) {
// Loop from 0 to choosFlavors.length
if(chosenSize.equals(chooseSizes[i])) {
// Compare user input to flavor at index i
System.out.println(chosenSize);
sizeFound = true;
// This is the flag to break out of while loop
break;
// Only breaks out of for loop
} else {
System.out.println("Please choose from size above.");
}
}
}
//chosenSize = scnr.nextLine();
if (chosenSize.contains("bite size")) { //setting the prices for the sizes
total = pricePerInch * biteSize;
}
else if (chosenSize.contains("small")) {
total = pricePerInch * small;
}
else if (chosenSize.contains("large")) {
total = pricePerInch * large;
}
System.out.println("Your chosen flavor: " + userFlavor); /*printing out a receipt for the customer with
the size, toping, and total cost*/
System.out.println("Your chosen size: " + chosenSize);
System.out.println("Price for topping: " + "$" + pricePerInch + " per inch.");
System.out.println("Size of cheesecake: " + chosenSize + " inches.");
System.out.printf("Your total cost will be: $" + newTotal + " dollars.");
newTotal = total + newTotal;
System.out.println("Would you like to order a cheesecake please choose y/n");
want = scnr.next();
}
System.out.printf("Your grand total cost will be: $" + newTotal + " dollars.");
}
}