Search⌘ K
AI Features

Solution: Secure Banking Menu

Explore how to implement a secure banking menu in Java by effectively using control flow statements such as do-while loops and switch cases. Learn to validate user inputs, handle menu options, and manage resources with Scanner, ensuring robust and user-friendly interaction.

We'll cover the following...
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        // Step 1: Input Validation Loop
        do {
            System.out.println("\n--- ATM Menu ---");
            System.out.println("1. Check Balance");
            System.out.println("2. Deposit Funds");
            System.out.println("3. Withdraw Cash");
            System.out.println("4. Transfer Funds");
            System.out.println("5. Exit");
            System.out.print("Select an option: ");
            
            choice = scanner.nextInt();

            // Logic check: Warn the user if input is invalid
            if (choice < 1 || choice > 5) {
                System.out.println("Invalid option. Please try again.");
            }

        } while (choice < 1 || choice > 5);

        // Step 2: Handle Valid Input
        switch (choice) {
            case 1 -> System.out.println("Displaying account balance...");
            case 2 -> System.out.println("Opening deposit window...");
            case 3 -> System.out.println("Opening withdrawal window...");
            case 4 -> System.out.println("Initiating transfer...");
            case 5 -> System.out.println("Logging out...");
        }
        
        scanner.close();
    }
}
A menu system using do-while for validation and switch for execution
...