Day 8 – Switch Case with Coffee Vending Machine

Java Switch Case

Hey friends, welcome back to our 100 Days of Java journey!
Yesterday we learned about if else conditions and made a movie recommender.
But what if you have many choices? Writing ten if else statements would be messy. That’s where Switch Case comes in.

What is Switch Case

Think of it like a menu board.

If you press 1, you get an Espresso.
If you press 2, you get a Latte.
If you press 3, you get a Cappuccino.
And so on.

Instead of checking every condition one by one, Switch jumps directly to the right choice.

Coffee Machine Example in Java

Here’s our Coffee Vending Machine logic:

import java.util.Scanner;

public class CoffeeMachine {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Welcome to Java Coffee Machine!");
        System.out.println("Press 1 for Espresso");
        System.out.println("Press 2 for Latte");
        System.out.println("Press 3 for Cappuccino");
        System.out.println("Press 4 for Black Coffee");

        System.out.print("Enter your choice: ");
        int choice = sc.nextInt();

        switch (choice) {
            case 1:
                System.out.println("Brewing your Espresso ☕...");
                break;
            case 2:
                System.out.println("Brewing your Latte ☕🥛...");
                break;
            case 3:
                System.out.println("Brewing your Cappuccino ☕🍫...");
                break;
            case 4:
                System.out.println("Brewing your Black Coffee ☕🔥...");
                break;
            default:
                System.out.println("Invalid choice! Please select 1-4.");
        }

        System.out.println("Enjoy your coffee! 🚀");
    }
}

Example Run

Welcome to Java Coffee Machine!
Press 1 for Espresso
Press 2 for Latte
Press 3 for Cappuccino
Press 4 for Black Coffee
Enter your choice: 3
Brewing your Cappuccino ☕🍫...
Enjoy your coffee! 🚀

Avengers Example

Think of it like Nick Fury choosing Avengers:

Case 1 → Call Iron Man
Case 2 → Call Captain America
Case 3 → Call Thor
Default → Call Hawkeye (poor guy 😂)

That’s how Switch Case works; pick one action out of many.

Wrap Up

Today you learned:

  • Why Switch Case is better than multiple if else statements for many options
  • How to use Switch in a real program
  • Built a fun Coffee Machine project

Tomorrow we’ll dive into loops, the secret power that lets your code repeat tasks automatically.

References

You can also check out:

About Us

If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.

Leave a Comment

Your email address will not be published. Required fields are marked *