user10097100
user10097100

Reputation:

how to return to beginning of code in java

I'm currently doing a project in a beginners programming class with java. The project involves making a vending machine. Essentially, the vending machine will ask what item you want and then it will ask for your money. Each time someone chooses a certain item, the quantity of the particular item is decreased by one. What I want to do is to make it so that when a certain item is out of stock (the quantity is 0), the code returns to the beginning and asks again what item you want. I'd also like to make it so if someone inputs an invalid number the code also restarts. How would I do that? Here's the code.

import java.util.Scanner;

public class VendingMachine {

public static void main(String[] args) {

    Scanner user_input = new Scanner(System.in);

    int potatoChips = 0;
    int cookies = 3;
    int candies = 4;
    int quarters;
    int dimes;
    int nickels;

    System.out.println("Select the number for the item you would like");
    System.out.println("For Potato Chips, Enter 1");
    System.out.println("For Cookies, Enter 2");
    System.out.println("For Candies, Enter 3");
    int itemSelection = user_input.nextInt();

        if (itemSelection == 1) {

            if (potatoChips > 0) {
                potatoChips = potatoChips - 1;
                System.out.println("You chose potato chips.");
                System.out.println("That will be $1.25");
                System.out.println("How many quarters do you have?");
                quarters = user_input.nextInt();
                System.out.println("How many dimes do you have?");
                dimes = user_input.nextInt();
                System.out.println("How many nickels do you have?");
                nickels = user_input.nextInt();

                int nickelsToPennies = (nickels * 5);
                int dimesToPennies = (dimes * 10);
                int quartersToPennies = (quarters * 25);

                int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

                if (pennies < 125) {
                    System.out.println("You have not entered enough money. Have a great day.");
                }
                else if (pennies == 125 ) {
                    System.out.println("Here is your snack. Have a great day");
                }
                else {
                    double changeInPennies = (pennies - 125);
                    double change = (changeInPennies / 100);
                    System.out.println("Your change is $" + change + ". Have a great day.");
                }
            } 

            else {
                System.out.println("This item is out of stock. Please select another item.");
            }
        }

        if (itemSelection == 2) {

            if (cookies > 0) {
                cookies = cookies - 1;
                System.out.println("You chose a cookie.");
                System.out.println("That will be $0.85");
                System.out.println("How many quarters do you have?");
                quarters = user_input.nextInt();
                System.out.println("How many dimes do you have?");
                dimes = user_input.nextInt();
                System.out.println("How many nickels do you have?");
                nickels = user_input.nextInt();

                int nickelsToPennies = (nickels * 5);
                int dimesToPennies = (dimes * 10);
                int quartersToPennies = (quarters * 25);

                int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

                if (pennies < 85) {
                    System.out.println("You have not entered enough money. Have a great day.");
                }
                else if (pennies == 85 ) {
                    System.out.println("Here is your snack. Have a great day");
                }
                else {
                    double changeInPennies = (pennies - 85);
                    double change = (changeInPennies / 100);
                    System.out.println("Your change is $" + change + ". Have a great day.");
                }
            } 

            else {
                System.out.println("This item is out of stock. Please select another item.");
            }
        }

        if (itemSelection == 3) {

            if (candies > 0) {
                candies = candies - 1;
                System.out.println("You chose a candy.");
                System.out.println("That will be $0.95");
                System.out.println("How many quarters do you have?");
                quarters = user_input.nextInt();
                System.out.println("How many dimes do you have?");
                dimes = user_input.nextInt();
                System.out.println("How many nickels do you have?");
                nickels = user_input.nextInt();

                int nickelsToPennies = (nickels * 5);
                int dimesToPennies = (dimes * 10);
                int quartersToPennies = (quarters * 25);

                int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

                if (pennies < 95) {
                    System.out.println("You have not entered enough money. Have a great day.");
                }
                else if (pennies == 95 ) {
                    System.out.println("Here is your snack. Have a great day");
                }
                else {
                    double changeInPennies = (pennies - 95);
                    double change = (changeInPennies / 100);
                    System.out.println("Your change is $" + change + ". Have a great day.");
                }
            } 

            else {
                System.out.println("This item is out of stock. Please select another item.");
            }
        }

        else {
            System.out.println("This is not an item. Please select another item.");
            }
    }
}

Upvotes: 0

Views: 1898

Answers (3)

Simeon Ikudabo
Simeon Ikudabo

Reputation: 2190

I did a mini example. The basic concept is that you can do a while loop. You can add something such as an additional condition for breaking out of the loop, which you may need to prevent an infinite loop. But the basic concept is this mini example is to obviously deincrement the item each time someone selects it. If they do so, we subtract 1 from the quantity of that item. Then, if the item is equal to 0, we use a CONTINUE statement in order to start the loop over again and ask them to select again. I think that the meet of what you wanted here was a continue statement. That will enable you to start the loop over again without breaking out of it. It is similar to a break statement, except that instead of exiting the loop entirely, it simply starts the loop over again if a particular condition is "true". Our condition will be true when 1 or both of the items in this example has a quantity of 0:

public class ChapterOneBasics {
     public static void main(String[] args) {
        int butterfinger = 5;
        int bQuantity = 3;
        int snickers = 4;
        int sQuantity = 3;
        Scanner reader = new Scanner(System.in);

        while(true){
             System.out.println("Please select an item (5 for butter finger, 4 for snickers)");
             int item = reader.nextInt();
             if((item == 5 && bQuantity == 0) || (item == 4 && sQuantity == 0)) {
                 System.out.println("We are out of that item!");
                    continue;
        }
             else {
                  if(item == 5) {
                  System.out.println("Here's your butter finger");
                  bQuantity--;
        }
                  else if(item == 4) {
                      System.out.println("Here is your snickers");
                      sQuantity--;
        }
                 else if(item != 5 || item != 4){
                      System.out.println("Enter a valid item");
                      continue;
            }
         }

    }
  }
}

Here is your output:

Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
Here's your butter finger
Please select an item (5 for butter finger, 4 for snickers)
5
We are out of that item!
Please select an item (5 for butter finger, 4 for snickers)

As you can see, the continue statement forces the loop to start over when they select an item that we are out of.

Upvotes: 0

MC1010
MC1010

Reputation: 106

You have to use a continuous while loop. Something like:

while(true) {
    //collect user input
    //process
}

That way when it’s done processing your users request it loops back to the beginning. If you want to stop it short of completion, like if the input is bad, you can use the continue keyword to stop the current loop and go back to the beginning. break will get you out of the entire while loop and end the code.

Upvotes: 0

Dacre Denny
Dacre Denny

Reputation: 30390

You could use a while loop to achieve this.

Consider structuring your code in the following way:

Scanner user_input = new Scanner(System.in);

// This will be used to track if the vending maching is finished or not
boolean isFinished = false;

// Use a while loop, which causes the program to repeat until the variable 
// isFinished is true (see below)
while(isFinished == false) {

    int potatoChips = 0;
    int cookies = 3;
    int candies = 4;
    int quarters;
    int dimes;
    int nickels;

    System.out.println("Select the number for the item you would like");
    System.out.println("For Potato Chips, Enter 1");
    System.out.println("For Cookies, Enter 2");
    System.out.println("For Candies, Enter 3");
    int itemSelection = user_input.nextInt();

    if (itemSelection == 1) {

        if (potatoChips > 0) {
            potatoChips = potatoChips - 1;
            System.out.println("You chose potato chips.");
            System.out.println("That will be $1.25");
            System.out.println("How many quarters do you have?");
            quarters = user_input.nextInt();
            System.out.println("How many dimes do you have?");
            dimes = user_input.nextInt();
            System.out.println("How many nickels do you have?");
            nickels = user_input.nextInt();

            int nickelsToPennies = (nickels * 5);
            int dimesToPennies = (dimes * 10);
            int quartersToPennies = (quarters * 25);

            int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

            if (pennies < 125) {
                System.out.println("You have not entered enough money. Have a great day.");
            }
            else if (pennies == 125 ) {
                System.out.println("Here is your snack. Have a great day");
            }
            else {
                double changeInPennies = (pennies - 125);
                double change = (changeInPennies / 100);
                System.out.println("Your change is $" + change + ". Have a great day.");
            }

            // This causes the machine loop to end
            isFinished = true;
        } 

        else {
            System.out.println("This item is out of stock. Please select another item.");
        }
    }

    if (itemSelection == 2) {

        if (cookies > 0) {
            cookies = cookies - 1;
            System.out.println("You chose a cookie.");
            System.out.println("That will be $0.85");
            System.out.println("How many quarters do you have?");
            quarters = user_input.nextInt();
            System.out.println("How many dimes do you have?");
            dimes = user_input.nextInt();
            System.out.println("How many nickels do you have?");
            nickels = user_input.nextInt();

            int nickelsToPennies = (nickels * 5);
            int dimesToPennies = (dimes * 10);
            int quartersToPennies = (quarters * 25);

            int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

            if (pennies < 85) {
                System.out.println("You have not entered enough money. Have a great day.");
            }
            else if (pennies == 85 ) {
                System.out.println("Here is your snack. Have a great day");
            }
            else {
                double changeInPennies = (pennies - 85);
                double change = (changeInPennies / 100);
                System.out.println("Your change is $" + change + ". Have a great day.");
            }

            // This causes the machine loop to end
            isFinished = true;
        } 

        else {
            System.out.println("This item is out of stock. Please select another item.");
        }
    }

    if (itemSelection == 3) {

        if (candies > 0) {
            candies = candies - 1;
            System.out.println("You chose a candy.");
            System.out.println("That will be $0.95");
            System.out.println("How many quarters do you have?");
            quarters = user_input.nextInt();
            System.out.println("How many dimes do you have?");
            dimes = user_input.nextInt();
            System.out.println("How many nickels do you have?");
            nickels = user_input.nextInt();

            int nickelsToPennies = (nickels * 5);
            int dimesToPennies = (dimes * 10);
            int quartersToPennies = (quarters * 25);

            int pennies = (nickelsToPennies + dimesToPennies + quartersToPennies);

            if (pennies < 95) {
                System.out.println("You have not entered enough money. Have a great day.");
            }
            else if (pennies == 95 ) {
                System.out.println("Here is your snack. Have a great day");
            }
            else {
                double changeInPennies = (pennies - 95);
                double change = (changeInPennies / 100);
                System.out.println("Your change is $" + change + ". Have a great day.");
            }

            // This causes the machine loop to end
            isFinished = true;
        } 

        else {
            System.out.println("This item is out of stock. Please select another item.");
        }
    }

    else {
        System.out.println("This is not an item. Please select another item.");
    }
}

Upvotes: 1

Related Questions