Home » Java » java – How can I execute a print statement after a menu loops and then returns back to the main menu?-Exceptionshub

java – How can I execute a print statement after a menu loops and then returns back to the main menu?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

In the printMenuItems method, the program should loop while the user selects items to add into a shopping cart. While the user is in the items menu, the program should display “your current total is: $” at the bottom until the user chooses to go back to the main menu. Once back at the main menu, it should display “Your total due is: $” at the bottom of the program. I currently have the “Your total due is: $” statement executing before the the program goes back to the main menu, which puts the “Your total due is” statement above the main menu when the user goes back. I’m trying to figure out how to put that statement below the main menu

import java.util.Scanner;
public class Homework1 {

    static double userTotal = 0;
    static double userCredit = 100;
    static double [] menuPrices = {19.99, 29.49, 15.79, 24.99, 24.99};
    static Scanner myScanner = new Scanner(System.in);

    public static int printMainMenu() {
        System.out.println("Please select a menu item from the list below: \n" +
                "1 - View item menu \n" +
                "2 - Pay total due \n" +
                "3 - Add $5 in credit \n" +
                "4 - Clear order");

        int userChoice = myScanner.nextInt();
        if (userChoice > 0 && userChoice < 5) {
            return userChoice;
        }
        else {
            System.out.println("Please enter a valid option!");
            printMainMenu();
        }
        return 0;
    }

    public static double printMenuItems(double userTotal) {
        System.out.println("What would you like to add to your order? \n" +
                "(1) Toaster: $19.99 \n" +
                "(2) Coffee maker: $24.49 \n" +
                "(3) Waffle maker: $15.79 \n" +
                "(4) Blender: $24.99 \n" +
                "(5) Kettle: $24.99 \n" +
                "(6) Go to the main menu");
        System.out.println("Your current total is: $" + userTotal);
        int menuSelections = myScanner.nextInt();

        if (menuSelections == 1) {
            System.out.println("You have added a Toaster to your order.");
            userTotal += menuPrices[0];
        }
        else if (menuSelections == 2) {
            System.out.println("You have added a Coffee maker to your order.");
            userTotal += menuPrices[1];
        }
        else if (menuSelections == 3) {
            System.out.println("You have added a Waffle maker to your order.");
            userTotal += menuPrices[2];
        }
        else if (menuSelections == 4) {
            System.out.println("You have added a Blender to your order.");
            userTotal += menuPrices[3];
        }
        else if (menuSelections == 5) {
            System.out.println("You have added a Kettle to your order.");
            userTotal += menuPrices[4];
        }
        else if (menuSelections == 6) {
            System.out.println("Your total due is: $" + userTotal); 
            return userTotal;
        }
        else {
            System.out.println("Invalid item number please try again");
        }
        return printMenuItems(userTotal);

    }

    public static boolean payTotalDue(double userTotal) {
        double preTaxTotal = userTotal;
        if (preTaxTotal > 50) {
            preTaxTotal -= (preTaxTotal * 0.2);
            preTaxTotal += (preTaxTotal * 0.085);

            if (preTaxTotal <= userCredit) {
                System.out.println("Thank you! You saved: $" + (userTotal + (userTotal * 0.085) - preTaxTotal) +
                        " Your change is: $" + (userCredit - preTaxTotal));
                System.out.println("Your items will be on their way soon!");

            }
            else {
                System.out.println("Insufficient funds!");
                System.exit(0);
            }

        }
        else {
            preTaxTotal += (preTaxTotal * 0.085);
            System.out.println("Thank you! Your change is: $" + (userCredit - preTaxTotal));
            System.out.println("Your items will be on their way soon!");
        }
        return true;
    }

    public static void main(String[] args) {

        while (true) {
            int mainMenuSelection = printMainMenu();
            if (mainMenuSelection == 1) {
                userTotal = printMenuItems(userTotal);

            }
            else if (mainMenuSelection == 2) {
                if (payTotalDue(userTotal)) {
                    userTotal = 0;
                    break;
                }
            }
            else if (mainMenuSelection == 3) {
                userCredit += 5;
                System.out.println("Credit available: $" + userCredit);
            }
            else if (mainMenuSelection == 4) {
                System.out.println("Current order balance has been cleared. Current due: $0.00");
                userTotal = 0;
            }
        }
    }
}
How to&Answers:

Simply under the bloc

 public static int printMainMenu() {
    System.out.println("Please select a menu item from the list below: \n" +
            "1 - View item menu \n" +
            "2 - Pay total due \n" +
            "3 - Add $5 in credit \n" +
            "4 - Clear order");

add

if ( userTotal > 0 ) {
            System.out.println("Your total due is: $" + userTotal);
        }

and remove from printing from bloc

 else if (menuSelections == 6) {
        System.out.println("Your total due is: $" + userTotal); 
        return userTotal;
    }

Also it would be nicer/simpler to use java switch instead of many if else

 switch (myScanner.nextInt()) {
            case 1:
                System.out.println("You have added a Toaster to your order.");
                userTotal += menuPrices[0];
                break;
            case 2:
                System.out.println("You have added a Coffee to your order.");
                userTotal += menuPrices[0];
                break;
            case 3: (continue with same..)
            default:
                System.out.println("Invalid item number please try again");
        }

as per question how do I run code printout once you can use this simple snippet

static boolean doOnce = true; //put this somewhere to be executed once , like begin of your program
...
if ( doOnce ) {
        System.out.println("Your total due is: $" + userTotal);
        doOnce = false;
}