Solution: Secure Banking Menu
Understand how to create a secure, user-friendly banking menu in Java. This lesson guides you through using Scanner for input, validating user choices with loops, and applying switch statements for option handling to ensure robust and maintainable code.
We'll cover the following...
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
...