(JAVA) do while loop for my vending machine

Noobprogrammer_69 :

this project i use do while loop with switch case to check the input case is not match or not. i run the code but the result not what i wanted. what i expect is if the user type the wrong case, the do while loop will loop back to the input where user need to enter the case.

here is the code

package vending.machine;
    import java.util.Scanner;
    import java.util.*;
    import java.util.ArrayList;
    import static vending.machine.adddrinks.drinksList;

public class VendingMachine {
    public static void main (String []args){
        Scanner sc= new Scanner(System.in);

        double money;
        double total;
        double balance;

        do{
            System.out.println("\nPlease insert money:");
            money = sc.nextDouble();
            if(money < 1.2){
                System.out.println("Not enough money");
            }
        }while(money < 1.2);
        System.out.println("What drinks are you looking for");
        adddrinks.showDrinks();
        adddrinks.viewDrinks();

        System.out.print("Select: 1 or 2 or 3 or 4\n");
        int select=sc.nextInt();

        do{
            switch(select){
                case 1:{
                    total = adddrinks.drinksList.get(0).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 2:{
                    total = adddrinks.drinksList.get(1).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 3:{
                    total = adddrinks.drinksList.get(2).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }
                case 4:{
                    total = adddrinks.drinksList.get(3).getdrinkPrice();
                    balance = money - total;
                    System.out.println("Here is your balance: " + balance);
                    break;
                }

                default:{
                    System.out.println("Invalid");
                    break;
                }
            }

        }while(select<5);
    }
}

here is the result enter image description here

Samuel Silver Moos :

You need to loop over your input, i was so free to improve your code a bit (sorry I do not like repetations):

private static void main10(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("What drinks are you looking for");
    adddrinks.showDrinks();
    adddrinks.viewDrinks();
    int select = 0;
    double balance = 0;
    boolean running = true;
    while (running) {
        if (sc.hasNextInt()) {
            select = sc.nextInt();
            if (0 < select && select <= adddrinks.drinksList.size()) {
                double price = adddrinks.drinksList.get(select - 1).getdrinkPrice();
                if (balance < price) {
                    System.out.println("Not enough money, " + select + " costs " + price);
                } else {
                    balance -= price;
                    System.out.println("You choosed " + select + " , you will find it in the dispenser");
                }
            } else {
                System.out.println("Invalid input, please retry");
            }
        } else if (sc.hasNextDouble()) {
            balance += sc.nextDouble();
        } else {
            String input = sc.next();
            if (input == "q") {
                running = false;
                if (0 < balance)
                    System.out.println("please don't forget your change with amount of: " + balance);
                System.out.println("Have a nice day, happy to see you again");
                break;
            } else if (input == "h") {
                System.out.println("What drinks are you looking for");
                adddrinks.showDrinks();
                adddrinks.viewDrinks();
            } else {
                System.out.println("Invalid input, please retry");
            }
        }
        System.out.println("Your balance is: " + balance);
        System.out.println(
                "please chouce your product (e.g 2), enter coins (e.g 2.0), click on 'h' to show product list or click on 'q' to get your change");

    }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=292425&siteId=1