Nested Menu-Driven Application
This program simulates a nested menu-driven application using nested switch.
Problem Statement
Write a Java program that simulates a nested menu-driven application with main menu and sub-menu options.
Source Code
| 1 | public class NestedMenu { |
| 2 | public static void main(String[] args) { |
| 3 | int mainChoice = 1; |
| 4 | int subChoice = 2; |
| 5 | switch(mainChoice) { |
| 6 | case 1: |
| 7 | switch(subChoice) { |
| 8 | case 1: System.out.println("Sub-option: Account Details - Eduinq"); break; |
| 9 | case 2: System.out.println("Sub-option: Transaction History - Eduinq"); break; |
| 10 | default: System.out.println("Invalid sub-option - Eduinq"); |
| 11 | } |
| 12 | break; |
| 13 | case 2: |
| 14 | switch(subChoice) { |
| 15 | case 1: System.out.println("Sub-option: Shopping Cart - Eduinq"); break; |
| 16 | case 2: System.out.println("Sub-option: Order History - Eduinq"); break; |
| 17 | default: System.out.println("Invalid sub-option - Eduinq"); |
| 18 | } |
| 19 | break; |
| 20 | default: System.out.println("Invalid main option - Eduinq"); |
| 21 | } |
| 22 | } |
| 23 | } |
Program Output
Sub-option: Transaction History - Eduinq
Explanation
The program uses a switch on mainChoice to select a main menu option. Inside each case, another switch checks subChoice for sub-menu options. This nested structure allows hierarchical decision-making, where one choice leads to another set of options. It demonstrates how nested switches can implement complex menu-driven applications, useful in banking, shopping, or administrative systems. Shows how nested switch can simulate hierarchical menu-driven applications.