简体   繁体   中英

Newbie to Java trying to figure out how to jump BACK in a do-while loop if a certain input is given

This is the code I have at the moment and I am trying to figure out how to jump back up from case "A" to the top of the do-while loop to allow the user to selection another action. The idea is that the user would be able to check their balance and then go back after checking their balance and either top-up their card or buy a wash. I only know how to end the loop from this point, not how to jump backwards to the previous input.

do {

    System.out.println("Please select an option:\nPress [A] to CHECK FUNDS, press [B] to TOP-UP CARD, press [C] to BUY WASH or press [Q] to quit.");
    String actionSelection = scan.nextLine();

    switch (actionSelection) { //I haven't defined all the cases yet! //The other cases should not need to refer to other classes, all the actions are within the WashCard
    case "A":
        System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        sentinel = true;
        System.out.println("Would you like to do something else? Press [B] to go back or [Q] to quit.");

        break;

    case "B":
        System.out.println("How much would you like to deposit?\nPlease type in a number between 200 and 1000.");
        //user input
        break;

    case "C":
        //any selection should display card balance
        //insufficient funds conditional statement:
        if (this.cardBalance < 50) {
            sentinel = true;
            System.out.println("Not enough credit! Please top-up your card first");
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");

        }

        //only 50 DKK left on the card:
        else if (this.cardBalance == 50) {
            sentinel = false;
            System.out.println("You only have enough credit for the ECONOMY WASH.");
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        }

        //any other choice:
        else {
            sentinel = false;
            System.out.println("Your current balance is: " + this.cardBalance + ".00 DKK.");
        }

        break;

    default:
        sentinel = true;
        System.out.println("Please enter a valid input!");

    }

} while ( sentinel == true );

You do not need this. This soulution breaks OOP pricniples. To solve it you should encapsulate different logic in different methods.

public class Foo {

    public static void main(String... args) {
        Scanner scan = new Scanner(System.in);
        proceed(scan);
    }

    private static final char CHECK_FUNDS = 'A';
    private static final char TOP_UP_CARD = 'B';
    private static final char BUY_WASH = 'C';
    private static final char QUIT = 'Q';

    public static void proceed(Scanner scan) {
        while (true) {
            System.out.println("Please select an option:");
            System.out.format("[%s] - CHECK FUNDS\n", CHECK_FUNDS);
            System.out.format("[%s] - TOP-UP CARD\n", TOP_UP_CARD);
            System.out.format("[%s] - BUY WASH\n", BUY_WASH);
            System.out.format("[%s] - quit\n", QUIT);
            System.out.print("> ");

            String menu = scan.nextLine().toUpperCase();
            char ch = menu.length() == 1 ? menu.charAt(0) : '\0';

            if (ch == CHECK_FUNDS)
                onCheckFunds();
            else if (ch == TOP_UP_CARD)
                onTopUpCard();
            else if (ch == BUY_WASH)
                onBuyWash();
            else if (ch == QUIT)
                return;

            System.err.println("incorrect input");
        }
    }

    private static void onCheckFunds() {
        System.out.println("onCheckFunds");
    }

    private static void onTopUpCard() {
        System.out.println("onCheckFunds");
    }

    private static void onBuyWash() {
        System.out.println("onBuyWash");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM