I'm using a loop to get user input and compare it; Idk why but on the first iteration of the while it works fine and displays only once. Second run thru I get two prompts, hoping someone can point out where I'm screwing up.

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        // Creates instance of Store and populates it with cakes and cookies
        Store bakery = fillInventory();

        // Creates instance of Customer and asks user to provide input
        Customer ourCustomer = new Customer("TheCustomer", 5.00);

        Cake theCake = null;
        Cookie theCookie = null;

        boolean validChoice = false;
        boolean validQuantity = false;
        String userChoice = null;
        String cakeOrCookie = "";
        int userQuantity = 0;

        System.out.print("Welcome to the Bakery!\n\n");

        while (ourCustomer.getCustomerMoney() > 0.25 ) {
                System.out.print("Would you like a cake or cookie?(-1 to quit): ");
                cakeOrCookie = input.nextLine();

            if (cakeOrCookie.equalsIgnoreCase("cake")) {
                System.out.print("Here is our cake selection:\n");
                bakery.cakeShop();

                validChoice = false;
                System.out.print("Please select one: ");
                userChoice = input.nextLine();
                validChoice = bakery.validCake(userChoice);
                while (validChoice == false) {
                    System.out.print("I didn't understand your selection, please re-renter: ");
                    userChoice = input.nextLine();
                    validChoice = bakery.validCake(userChoice);
                }

                validQuantity = false;
                System.out.print("How many would you like?: ");
                userQuantity = input.nextInt();
                validQuantity = bakery.checkCake(userChoice, userQuantity);
                while (validQuantity == false) {
                    System.out.print("I'm sorry we can't allow that many, please re-enter: ");
                    userQuantity = input.nextInt();
                    validQuantity = bakery.checkCake(userChoice, userQuantity);
                }

                if (validChoice == true && validQuantity == true) {
                    for (Cake index : bakery.cakeList) {
                        if (userChoice.equalsIgnoreCase(index.getCakeName())) {
                            theCake = index;
                        }
                    }
                }

                bakery.buy(ourCustomer, theCake, userQuantity);
                bakery.printReceipt();
            } else if (cakeOrCookie.equalsIgnoreCase("cookie")) {
                System.out.print("Here is our cookie selection:\n");
                bakery.cookieShop();

                validChoice = false;
                System.out.print("Please select one: ");
                userChoice = input.nextLine();
                validChoice = bakery.validCookie(userChoice);
                while (validChoice == false) {
                    System.out.print("I didn't understand your selection, please re-renter: ");
                    userChoice = input.nextLine();
                    validChoice = bakery.validCookie(userChoice);
                }

                validQuantity = false;
                System.out.print("How many would you like?: ");
                userQuantity = input.nextInt();
                validQuantity = bakery.checkCookie(userChoice, userQuantity);
                while (validQuantity == false) {
                    System.out.print("I'm sorry we can't allow that many, please re-enter: ");
                    userQuantity = input.nextInt();
                    validQuantity = bakery.checkCookie(userChoice, userQuantity);
                }

                if (validChoice == true && validQuantity == true) {
                    for (Cookie index : bakery.cookieList) {
                        if (userChoice.equalsIgnoreCase(index.getCookieName())) {
                            theCookie = index;
                        }
                    }
                }

                bakery.buy(ourCustomer, theCookie, userQuantity);
                bakery.printReceipt();
            } else if ( cakeOrCookie.equalsIgnoreCase("-1") ) {
                break;
            }            
        }



        // once the customer is broke and can't afford anything thank them and print final receipt
        System.out.print("Thanks for shopping! Come back when you have more money!\n");
        System.out.print("Total receipt:\n");

    }

    public static Store fillInventory() {
        Store bakery = new Store();

        bakery.createCake("Cake 1", "Flavor 1", 0.50, 15);
        bakery.createCake("Cake 2", "Flavor 2", 0.50, 15);
        bakery.createCake("Cake 3", "Flavor 3", 0.50, 15);

        bakery.createCookie("Cookie 1", "Flavor 1", 0.25, 15);
        bakery.createCookie("Cookie 2", "Flavor 2", 0.25, 15);
        bakery.createCookie("Cookie 3", "Flavor 3", 0.25, 15);

        return bakery;
    }
}

The code isn't the cleanest but I'm teaching myself so give me a break lol

Recommended Answers

All 3 Replies

I kinda for the most part get what your code does but, which loops is prompting twice? You have 3 loops. And what is being prompted twice?

Put a load of print statements into your code, printing the values of the key variables at each stage so you can see where it's going wrong.

You are probably having a problem with the Scanner class. Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only "A" is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

Try calling the nextLine() method to clear the lineend from the buffer after using the nextInt() method.

commented: Thanks this did the trick... Thats a funky little ?bug? there. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.