I'm writing this piece of code for an assignment and I can't for the life of me figure out why my code isn't looping. I have the 'moreTablesToServe' static in a while loop, and the loop seems to not even check a second time to see if the static is true or false. While looking at the loop, I don't understand why it wouldn't repeat the entire program, once again prompting the user if there were any more tables.
I linked all my code below, if anyone could help me to not only fix the issue (which is probably super easy) or teach me what exactly is going on, I'd really appreciate it!
Thank you!
import java.util.Scanner;
public class AllTableOrders {
public static Scanner scnr = new Scanner(System.in);
public static void main(String[] args)
{
// getTableOrder();
getAllTableOrders();
}
public static void displayMenu()
{
System.out.println("1) eggs $3.25");
System.out.println("2) bacon $4.00");
System.out.println("3) pancakes $2.50");
System.out.println("4) orange juice $1.25");
System.out.println("5) oatmeal $3.99");
System.out.println("6) milk $1.25");
System.out.println("7) donut $2.00");
System.out.println("");
}
public static boolean dinerWantsAnotherItem()
{
String ans = "";
System.out.print("Another item ('yes' or 'no')? ");
ans = scnr.next();
if (ans.equals("yes"))
{
return true;
}
else
{
System.out.println("");
return false;
}
}
public static int getMenuChoice()
{
System.out.print("Enter a menu choice : ");
int order = scnr.nextInt();
return order;
}
public static double getPriceOfMenuChoice(int order)
{
if (order == 1){
return 3.25;}
else if(order == 2){
return 4.00;}
else if(order == 3){
return 2.50;}
else if(order == 4){
return 1.25;}
else if(order == 5){
return 3.99;}
else if(order == 6){
return 1.25;}
else if(order == 7){
return 2.00;}
else{
return 0.00;}
}
public static double getDinersOrder()
{
displayMenu();
double totalCost = 0.00;
while (dinerWantsAnotherItem())
{
int choice = getMenuChoice();
totalCost = totalCost + getPriceOfMenuChoice(choice);
}
//System.out.println(totalCost);
return totalCost;
}
public static int getNumberOfDinersAtTable()
{
System.out.println("Enter number of diners at this table: ");
int diners = scnr.nextInt();
return diners;
}
public static void displaySuggestedTipAmounts(double amount)
{
double tenPercent = 0.10 * amount;
System.out.println("10% tip: " + String.format("%.2f", tenPercent));
double fifteenPercent = 0.15 * amount;
System.out.println("15% tip: " + String.format("%.2f", fifteenPercent));
double twentyPercent = 0.20 * amount;
System.out.println("20% tip: " + String.format("%.2f", twentyPercent));
double twentyFivePercent = 0.25 * amount;
System.out.println("25% tip: " + String.format("%.2f", twentyFivePercent));
}
public static double getTableOrder()
{
double totalTableCost = 0.00;
double total = 0.00;
int diners = getNumberOfDinersAtTable();
while (diners > 0)
{
totalTableCost = totalTableCost + getDinersOrder();
diners--;
}
double taxAmount = (totalTableCost * 0.08);
System.out.println("Table total : " + String.format("%.2f", totalTableCost));
System.out.println("Tax amount : " + String.format("%.2f", taxAmount));
System.out.println("");
displaySuggestedTipAmounts(totalTableCost);
total = (totalTableCost + taxAmount);
return total;
}
public static boolean moreTablesToServe()
{
System.out.println("Another table ('yes' or 'no')? ");
String moreTables = scnr.nextLine();
if (moreTables.equalsIgnoreCase("yes"))
{
return true;
}
else
{
return false;
}
}
public static double getAllTableOrders()
{
double totalInRegister = 0.00;
while (moreTablesToServe())
{
totalInRegister = totalInRegister + getTableOrder();
}
return totalInRegister;
}
}