0

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;
    }
}
  • Is it terminating, or waiting at one of the Scanner.nextX statements? – NomadMaker Oct 28 '20 at 10:51
  • @NomadMaker it's terminating after doing through the class one time – Ryan Coffey Oct 28 '20 at 18:04
  • The first skill you need is how to create a [mre]. Second, you need to learn to create your program and test as you go. When I ran your program there were several places where the output did not match the input that was expected. – NomadMaker Oct 28 '20 at 18:13
  • Another problem with code readability is that you should be consistent with your use of braces. On the whole you're using one of the major styles (I find it annoying, but some like it) where you put the opening and closing brace on a blank line, with the closing brace indenting the same as the opening brace. If you always did it this way, I wouldn't have commented. You should be consistent. – NomadMaker Oct 28 '20 at 18:17

0 Answers0