Home > Mobile >  How do I fix a nested switch without breaking if statement?
How do I fix a nested switch without breaking if statement?

Time:11-23

I've been trying to get the switch statement to work without having to be nested, but it won't work because it's linked to the if statement I've made.

Does anybody know how i can move the boardType switch statement outside of the duration switch statement with the if statement still working? Thank you

int duration = input.nextInt();

switch (duration) {
    default -> System.out.println("You have not entered a valid number of nights.");
    case 2, 7, 14 -> {
        System.out.print("How many guests are in your party? ");
        int guestAmount = input.nextInt();

        if (guestAmount >= 1 && guestAmount <= 10) {
            System.out.print("What type of board would you like (full, half, or self-catering)? ");
            String boardType = input.next();

            switch (boardType) {
                case "full", "half", "self-catering" ->
                        System.out.println("Valid details entered - booking may proceed");
                default -> System.out.println("Sorry, we do not cater that type of board.");
            }
        } else {
            System.out.println("Sorry, you are only allowed 1 to 10 guests.");

CodePudding user response:

You can do it without switch statement also. Nested if else would be easy to implement.

CodePudding user response:

To improve the code, you can extract this logic into a separate method.

int duration = input.nextInt();
    
switch (duration) {
    default -> System.out.println("You have not entered a valid number of nights.");
    case 2, 7, 14 -> arrange(input);
}
public static void arrange(Scanner input) {
    System.out.print("How many guests are in your party? ");
    int guestAmount = input.nextInt();

    if (guestAmount >= 1 && guestAmount <= 10) {
        System.out.print("What type of board would you like (full, half, or self-catering)? ");
        String boardType = input.next();
    
        switch (boardType) {
            case "full", "half", "self-catering" -> System.out.println("Valid details entered - booking may proceed");
            default -> System.out.println("Sorry, we do not cater that type of board.");
        }
    } else {
        System.out.println("Sorry, you are only allowed 1 to 10 guests.");
    }
}

And logic for choosing the boarding type could reside in its own method as well. This way the code would be learner and easier to follow.

public static void arrange(Scanner input) {
    System.out.print("How many guests are in your party? ");
    int guestAmount = input.nextInt();

    if (guestAmount >= 1 && guestAmount <= 10) {
        chooseBoardingType(input);
    } else {
        System.out.println("Sorry, you are only allowed 1 to 10 guests.");
    }
}

public static void chooseBoardingType(Scanner input) {
    System.out.print("What type of board would you like (full, half, or self-catering)? ");
    String boardType = input.next();

    switch (boardType) {
        case "full", "half", "self-catering" -> System.out.println("Valid details entered - booking may proceed");
        default -> System.out.println("Sorry, we do not cater that type of board.");
    }
}
  • Related