public class RealtimeSwitchcase {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Display menu
System.out.println("Welcome to ChatGPT Café!");
System.out.println("1. Idli - ₹30");
System.out.println("2. Dosa - ₹50");
System.out.println("3. Pongal - ₹40");
System.out.println("4. Exit");
System.out.print("Please enter your choice (1-4): ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("You ordered Idli. Please pay ₹30.");
break;
case 2:
System.out.println("You ordered Dosa. Please pay ₹50.");
break;
case 3:
System.out.println("You ordered Pongal. Please pay ₹40.");
break;
case 4:
System.out.println("Thank you! Visit again.");
break;
default:
System.out.println("Invalid choice. Please select a valid option.");
}
scanner.close();
}
}
Using if and else if
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);// take input from the user at runtime
// Display menu
System.out.println("Welcome to Simple Bank");
System.out.println("1. Check Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Exit");
System.out.print("Please select an option (1-4): ");
int option = scanner.nextInt();
double balance = 1000.00; // Starting balance
if (option == 1) {
System.out.println("Your current balance is: ₹" + balance);
} else if (option == 2) {
System.out.print("Enter amount to deposit: ₹");
double deposit = scanner.nextDouble();
balance += deposit;
System.out.println("Amount deposited successfully. New balance: ₹" + balance);
} else if (option == 3) {
System.out.print("Enter amount to withdraw: ₹");
double withdraw = scanner.nextDouble();
if (withdraw <= balance) {
balance -= withdraw;
System.out.println("Amount withdrawn successfully. New balance: ₹" + balance);
} else {
System.out.println("Insufficient balance.");
}
} else if (option == 4) {
System.out.println("Thank you for banking with us!");
} else {
System.out.println("Invalid option. Please try again.");
}
scanner.close();
}
}
Top comments (0)