Build a Coffee Machine Simulator

Build a console-based coffee machine simulator using Java OOP principles.

Introduction

30 Second Summary

Think about what happens when you press a button on a coffee vending machine. Behind the scenes, the machine checks its water level, counts its beans, and decides whether it can make your drink.

In this project, you will build a console-based Coffee Machine Simulator in Java. You will create classes that manage ingredients, track money, and serve drinks through an interactive menu.

What You'll Build

A menu-driven command line application that simulates a coffee vending machine with resource management and drink preparation.

By the end of this project, you'll have:

  • A Coffee enum defining drink types with their ingredient requirements and cost.
  • A CoffeeMachine class that encapsulates the machine's internal state and validates orders before serving.
  • An interactive menu loop that lets users buy drinks, refill supplies, and collect money.
  • Secret Mission: Add a maintenance system that tracks cups served and requires cleaning every 10 drinks.

Are there any prerequisites?

None. This guide includes full setup instructions for all required tools, including the Java Development Kit and IntelliJ IDEA.

Before We Start

Before you can write Java code, you need two things: the Java Development Kit (JDK) to compile and run your programs, and an IDE to write them in.

In this step, you will install JDK 25 and IntelliJ IDEA. Together, these give you everything you need to start building your Coffee Machine Simulator.

In this step, get ready to:
  • Install JDK 25 on your computer.
  • Verify that Java is working correctly.
  • Install IntelliJ IDEA as your code editor.
Download and install JDK 25

The JDK (Java Development Kit) includes the Java compiler and runtime. You need it to turn your Java source code into programs your computer can execute.

Windows

  • Click the Windows tab on the downloads page.
  • Download the x64 Installer file (jdk-25_windows-x64_bin.exe).
  • Run the downloaded .exe file and follow the installation prompts.
  • After installation completes, delete the downloaded file from your Downloads folder to recover disk space.

What about admin privileges?

The JDK installer requires administrator privileges on Windows. If you see a prompt asking for permission to make changes, click Yes to allow the installation.

macOS

  • Click the macOS tab on the downloads page.
  • Download the ARM64 DMG Installer if you have an Apple Silicon Mac (M1/M2/M3/M4). Download the x64 DMG Installer if you have an Intel Mac.
  • Open the downloaded .dmg file and double-click the JDK installer icon.
  • Follow the installation prompts.
  • After installation completes, delete the downloaded file from your Downloads folder to recover disk space.

Not sure which Mac you have?

Click the Apple menu in the top left corner, then About This Mac. If the Chip line says "Apple M1" or similar, download the ARM64 installer. If it says "Intel", download the x64 installer.

Why JDK 25?

JDK 25 is the latest Long-Term Support (LTS) release of Java. LTS versions receive updates for years, making them the most stable choice for learning and production use.

JDK 25 binaries are free to use in production and free to redistribute under the Oracle No-Fee Terms and Conditions (NFTC).

Verify your Java installation

After installing, you need to confirm Java is accessible from your terminal. This ensures the installer added Java to your system PATH correctly.

  • Press Cmd+Space (macOS) or the Windows key (Windows) to open your search bar.
  • Type Terminal (macOS) or Command Prompt (Windows) and press Enter to open it.
  • Check your Java version by running this command:
java -version

✔️ I see version 25

Your output contains 25 in the version string. Java is installed and ready to go.

  • Now verify the Java compiler by running:
javac -version

You should see output containing javac 25 (possibly followed by a patch number). Both tools are working.

ⓧ I see an older version

An older version of Java is installed on your system and is taking priority over the new JDK 25 installation.

  • On Windows, open Settings, then Apps, then Installed apps. Find any older Java versions and uninstall them.
  • On macOS, run /usr/libexec/java_home -V to see all installed JDKs. Set JDK 25 as default by adding export JAVA_HOME=$(/usr/libexec/java_home -v 25) to your shell profile.
  • Close and reopen your terminal, then run java -version again to confirm it now shows 25.

ⓧ Command not found

Your system cannot find the java command. The JDK installer may not have added Java to your PATH.

  • On Windows, try closing your Command Prompt and opening a new one. The PATH update only takes effect in new terminal windows.
  • If that does not work, re-run the JDK installer from the Oracle downloads page. Make sure to select the x64 Installer (.exe) format, not the compressed archive.
  • On macOS, close Terminal and reopen it. Run java -version again. If it still fails, re-download and run the DMG installer.

After resolving the issue, run java -version one more time to confirm it outputs 25.

Download and install IntelliJ IDEA

IntelliJ IDEA is the industry-standard IDE for Java development. It provides smart code completion, built-in debugging, and project management tools that make writing Java significantly easier.

Windows

  • Make sure the Windows tab is selected at the top of the page.
  • Click Download.
  • Run the downloaded .exe installer and follow the wizard steps.
  • On the Installation Options step, check Add "bin" folder to the PATH and Add "Open Folder as Project".
  • Complete the installation and launch IntelliJ IDEA.

macOS

  • Make sure the macOS tab is selected at the top of the page.
  • Select the correct installer for your Mac (Intel or Apple Silicon) and click Download.
  • Open the downloaded .dmg file.
  • Drag the IntelliJ IDEA icon into the Applications folder.
  • Open IntelliJ IDEA from your Applications folder or Spotlight search.

Is IntelliJ IDEA free?

The core functionality of IntelliJ IDEA is free. It includes everything you need for Java and Kotlin development.

When you first launch it, you will see a 30-day Ultimate trial offer. You can ignore this and continue using the free core features without subscribing.

When IntelliJ IDEA opens, you should see the Welcome to IntelliJ IDEA screen with options to create or open a project.

IntelliJ IDEA not opening?

Make sure the installation completed fully before trying to launch. On Windows, try searching for "IntelliJ" in the Start menu. On macOS, check your Applications folder.

If it still will not open, restart your computer and try again.

Help me troubleshoot IntelliJ IDEA not launching after installation.

Your development environment is ready. Next up, you will create your Java project and define the different coffee types your machine can serve.

Install JDK and IntelliJ IDEA

Before writing any Java code, you need a project workspace. A Java project organizes your source files, compile settings, and run configurations in one place.

Your tools are ready to go. IntelliJ IDEA is open and JDK 25 is installed. Your next move is to create the project that will hold all three source files for your Coffee Machine Simulator.

In this step, get ready to:
  • Create a new Java project named CoffeeMachineSimulator.
  • Confirm the project structure includes a src folder for your source files.
Create the CoffeeMachineSimulator project

A project in IntelliJ IDEA bundles your source code, libraries, and build instructions into a single unit. Creating one now gives you a dedicated workspace for the three Java files you will write.

  • In IntelliJ IDEA, click New Project on the Welcome screen.
  • In the New Project wizard, select Java from the list on the left.
  • In the Name field, type CoffeeMachineSimulator.
  • Ensure the Build system is set to IntelliJ.
  • In the JDK dropdown, select 25. IntelliJ IDEA automatically detects the JDK you installed earlier.
  • Leave the Add sample code checkbox unchecked. You will write everything from scratch.
  • Click Create.

IntelliJ IDEA creates and loads the new project. In the Project panel on the left, you should see a src folder listed under CoffeeMachineSimulator.

What is the src folder?

The src folder is where all your Java source files live. IntelliJ compiles everything inside src when you run the project.

You will create three files here: Coffee.java, CoffeeMachine.java, and Main.java.

Don't see JDK 25 in the dropdown?

If the JDK dropdown does not show 25, click the dropdown and select Add JDK from Disk. Navigate to the folder where you installed JDK 25 (the default on Windows is C:\Program Files\Java\jdk-25).

Help me find my JDK 25 installation path so IntelliJ IDEA can detect it.

Your project workspace is ready. Next up, you will define the coffee types your machine can serve using a Java enum.

Create the Project and Define Coffee Types

With Java installed and IntelliJ IDEA ready, it's time to define the core data your coffee machine will work with.

Every coffee drink has fixed properties: the amount of water, milk, and beans it needs, plus its price. Java has a special structure called an enum that's perfect for representing a fixed set of options like this.

In this step, get ready to:
  • Create a Coffee.java enum file in your project.
  • Define three drink types with their ingredient requirements and cost.
  • Add fields, a constructor, and getter methods to support encapsulation.
Create the Coffee enum

An enum in Java defines a fixed set of constants. Each constant can carry data with it, making enums ideal for representing coffee types that always have the same recipe.

  • In IntelliJ IDEA, find the src folder in the Project panel on the left.
  • Right-click the src folder.
  • Select New, then Java Class.
  • Type Coffee as the name.
  • Change the Kind dropdown from Class to Enum.
  • Press Enter to create the file.

IntelliJ creates an empty Coffee.java file and opens it in the editor.

  • Replace the contents of Coffee.java with the following code to define three coffee types:
public enum Coffee {
    ESPRESSO(250, 0, 16, 4),
    LATTE(350, 75, 20, 7),
    CAPPUCCINO(200, 100, 12, 6);
}

What does this code do?

Each line declares a coffee type as an enum constant. The numbers in parentheses represent the drink's recipe and price in this order:

  • Water (ml), milk (ml), coffee beans (g), cost ($).
  • Espresso: 250ml water, no milk, 16g beans, $4.
  • Latte: 350ml water, 75ml milk, 20g beans, $7.
  • Cappuccino: 200ml water, 100ml milk, 12g beans, $6.

You will see red underlines in the editor. This is expected because Java needs a matching constructor to assign those values. You will fix this in the next section.

Add fields, constructor, and getters to the enum

The constants above pass four values, but the enum doesn't know what to do with them yet. You need fields to store the values, a constructor to assign them, and getter methods so other classes can read them.

What is encapsulation?

Encapsulation means bundling data (fields) with the methods that access it, and hiding the internal details from outside code.

By making fields private final, no code outside the enum can change a coffee's recipe after it's defined. Getters provide read-only access.

  • In Coffee.java, place your cursor on the blank line below CAPPUCCINO(200, 100, 12, 6); and paste the following code:
    private final int water;
    private final int milk;
    private final int beans;
    private final int cost;

    Coffee(int water, int milk, int beans, int cost) {
        this.water = water;
        this.milk = milk;
        this.beans = beans;
        this.cost = cost;
    }

    public int getWater() {
        return water;
    }

    public int getMilk() {
        return milk;
    }

    public int getBeans() {
        return beans;
    }

    public int getCost() {
        return cost;
    }

What does this code do?

  • private final int water (and the other three fields) store each drink's recipe values. They are private so outside code cannot modify them, and final so they can only be set once.
  • The constructor Coffee(int water, int milk, int beans, int cost) runs automatically for each constant, assigning the values in parentheses to the fields.
  • Getter methods like getWater() provide controlled, read-only access to each field.
  • Save Coffee.java by pressing Cmd+S (macOS) or Ctrl+S (Windows).
  • Check the editor. The red underlines should be gone, and no errors should appear in the bottom Problems panel.

Still seeing red underlines?

  • Make sure the constructor parameters match the order and count of values in each constant. Each constant passes four integers (water, milk, beans, cost).
  • Check that the semicolon after CAPPUCCINO(200, 100, 12, 6); is present. Enum constants must end with a semicolon before any fields or methods.
  • Ensure your closing brace } appears after all the getter methods, not between the constants and the fields.

Help me fix errors in my Coffee enum

✔️ Awesome, I've got everything!

Great. Double check you have saved Coffee.java and the editor shows no errors.

ⓧ I'd like to double check the full code

Here is the complete Coffee.java file. Make sure yours matches exactly.

public enum Coffee {
    ESPRESSO(250, 0, 16, 4),
    LATTE(350, 75, 20, 7),
    CAPPUCCINO(200, 100, 12, 6);

    private final int water;
    private final int milk;
    private final int beans;
    private final int cost;

    Coffee(int water, int milk, int beans, int cost) {
        this.water = water;
        this.milk = milk;
        this.beans = beans;
        this.cost = cost;
    }

    public int getWater() {
        return water;
    }

    public int getMilk() {
        return milk;
    }

    public int getBeans() {
        return beans;
    }

    public int getCost() {
        return cost;
    }
}

Your coffee types are defined and locked in. Next up, you will build the CoffeeMachine class that uses these types to track resources and serve drinks.

Build the CoffeeMachine Class

Your Coffee enum now defines what each drink needs. The next question is: where do all the ingredients actually live?

In this step, you will build the CoffeeMachine class. This class holds the machine's resources (water, milk, beans, cups, money) in private fields and exposes public methods to interact with them. This pattern is called encapsulation. Outside code can only change the machine's state through the methods you define.

In this step, get ready to:
  • Create the CoffeeMachine class with private resource fields.
  • Add a buy method that validates and deducts resources.
  • Add fill, take, and showResources utility methods.
Create the CoffeeMachine class

The machine needs to track five resources internally. You will store each one in a private field with a starting value representing a freshly stocked machine.

  • In the Project tool window on the left side of IntelliJ IDEA, right-click the src folder.
  • Select New, then Java Class.
  • Type CoffeeMachine as the name and press Enter.
  • IntelliJ creates the file with an empty class body. Replace the entire contents of CoffeeMachine.java with this code:
public class CoffeeMachine {
    // Private fields store the machine's current resource levels
    private int water = 400;
    private int milk = 540;
    private int beans = 120;
    private int cups = 9;
    private int money = 550;
}

What does this code do?

  • Each private field stores one resource the machine tracks internally. No outside code can read or change these values directly.
  • The initial values (400ml water, 540ml milk, 120g beans, 9 cups, $550 money) represent a freshly stocked machine ready to serve.
  • Making fields private is encapsulation in action. You will add public methods next to control how the state changes.
  • Save CoffeeMachine.java by pressing Cmd+S (macOS) or Ctrl+S (Windows).
  • Check the editor for red underlines. You should see none, which confirms the code compiles without errors.

Seeing red underlines?

  • Make sure the class name CoffeeMachine matches the filename CoffeeMachine.java exactly. Java is case-sensitive.
  • Confirm the file is inside the src folder. If it ended up in the project root, drag it into src in the Project tool window.

Help me fix red underlines in my CoffeeMachine class

Add the buy method

The buy method is the core logic of the machine. It checks whether enough of each resource is available, and either makes the drink or tells the user what is missing.

  • In CoffeeMachine.java, place your cursor inside the class body (after the money field, before the closing }).
  • Add the following buy method:
    // Check if the machine has enough resources, then make the drink
    public void buy(Coffee coffee) {
        if (water < coffee.getWater()) {
            System.out.println("Sorry, not enough water!");
            return;
        }
        if (milk < coffee.getMilk()) {
            System.out.println("Sorry, not enough milk!");
            return;
        }
        if (beans < coffee.getBeans()) {
            System.out.println("Sorry, not enough coffee beans!");
            return;
        }
        if (cups < 1) {
            System.out.println("Sorry, not enough disposable cups!");
            return;
        }

        water -= coffee.getWater();
        milk -= coffee.getMilk();
        beans -= coffee.getBeans();
        cups--;
        money += coffee.getCost();
        System.out.println("I have enough resources, making you a coffee!");
    }

What does this code do?

  • The method accepts a Coffee enum value and uses its getter methods (getWater(), getMilk(), etc.) to find how much of each resource the drink needs.
  • Each if block checks one resource. If the machine does not have enough, it prints a message and return exits the method early.
  • If all checks pass, the method deducts the required amounts, decrements cups by one, adds the drink's cost to money, and prints a success message.
  • Save CoffeeMachine.java.
  • Confirm there are no red underlines in the editor. The Coffee reference should resolve automatically since both files are in the same src folder.

Seeing "cannot find symbol" for Coffee?

  • Confirm that Coffee.java is saved and sits in the same src folder as CoffeeMachine.java.
  • Check that your Coffee.java file starts with public enum Coffee and includes the getter methods.

Why can't IntelliJ find my Coffee enum?

Add fill, take, and showResources methods

The machine needs three more capabilities: restocking supplies, collecting earned money, and displaying the current state. Each one is a short public method.

  • In CoffeeMachine.java, place your cursor after the closing } of the buy method.
  • Add the fill and take methods:
    // Add resources to the machine
    public void fill(int addWater, int addMilk, int addBeans, int addCups) {
        water += addWater;
        milk += addMilk;
        beans += addBeans;
        cups += addCups;
    }

    // Collect all money from the machine
    public void take() {
        System.out.println("I gave you $" + money);
        money = 0;
    }

What does this code do?

  • The fill method accepts four parameters and adds each to the corresponding resource total. This simulates restocking the machine.
  • The take method prints how much money the machine holds, then resets it to zero. This simulates an operator collecting the cash.
  • Save CoffeeMachine.java and confirm no red underlines appear.

Red underline on money or cups?

  • Make sure the fill and take methods are inside the class braces, not accidentally pasted after the closing } of the class.
  • Verify the field names match exactly: water, milk, beans, cups, money.

Help me fix errors in my fill or take methods

  • Now add the showResources method below take:
    // Display the current state of all resources
    public void showResources() {
        System.out.println("The coffee machine has:");
        System.out.println(water + " ml of water");
        System.out.println(milk + " ml of milk");
        System.out.println(beans + " g of coffee beans");
        System.out.println(cups + " disposable cups");
        System.out.println("$" + money + " of money");
    }

What does this code do?

The showResources method prints each resource value on its own line with a descriptive label. This gives the user a quick snapshot of the machine's current state at any time.

  • Save CoffeeMachine.java.
  • Confirm no red underlines appear anywhere in the file. All four methods should be error-free.

Still seeing errors?

  • Count your curly braces. The class should have one opening { after public class CoffeeMachine and one final closing } at the very end of the file.
  • Each method should have its own matched pair of braces inside the class.

Help me debug CoffeeMachine.java errors

✔️ Awesome, I've got everything!

Great. Double check you have saved CoffeeMachine.java and there are no errors in the editor.

ⓧ I'd like to double check the full code

Here is the complete CoffeeMachine.java file. Compare it to what you have:

public class CoffeeMachine {
    private int water = 400;
    private int milk = 540;
    private int beans = 120;
    private int cups = 9;
    private int money = 550;

    public void buy(Coffee coffee) {
        if (water < coffee.getWater()) {
            System.out.println("Sorry, not enough water!");
            return;
        }
        if (milk < coffee.getMilk()) {
            System.out.println("Sorry, not enough milk!");
            return;
        }
        if (beans < coffee.getBeans()) {
            System.out.println("Sorry, not enough coffee beans!");
            return;
        }
        if (cups < 1) {
            System.out.println("Sorry, not enough disposable cups!");
            return;
        }

        water -= coffee.getWater();
        milk -= coffee.getMilk();
        beans -= coffee.getBeans();
        cups--;
        money += coffee.getCost();
        System.out.println("I have enough resources, making you a coffee!");
    }

    public void fill(int addWater, int addMilk, int addBeans, int addCups) {
        water += addWater;
        milk += addMilk;
        beans += addBeans;
        cups += addCups;
    }

    public void take() {
        System.out.println("I gave you $" + money);
        money = 0;
    }

    public void showResources() {
        System.out.println("The coffee machine has:");
        System.out.println(water + " ml of water");
        System.out.println(milk + " ml of milk");
        System.out.println(beans + " g of coffee beans");
        System.out.println(cups + " disposable cups");
        System.out.println("$" + money + " of money");
    }
}

Your machine class is fully built. It can check resources, make drinks, restock, collect money, and report its status. Next up, you will wire everything together with an interactive menu so users can actually operate the machine from the command line.

Build the Interactive Menu

Your CoffeeMachine class can now buy drinks, refill supplies, collect money, and report its status. The missing piece is a way for a user to actually talk to the machine.

In this step, you will create an interactive command line menu that runs in a loop, accepts typed commands, and routes each one to the correct method on your CoffeeMachine object. This is how real console applications work.

In this step, get ready to:
  • Create the Main class with a Scanner for user input.
  • Build a menu loop with a switch statement routing commands.
  • Run and test the full application end to end.
Create the Main class

Every Java application needs an entry point. The main method is where the JVM starts execution. You will also create a Scanner object to read typed input from the console.

  • In IntelliJ, right-click the src folder in the **Project** panel on the left.
  • Select New > Java Class.
  • Type Main as the name and press Enter.
  • Replace the generated contents of Main.java with the following code:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Read user input from the console
        Scanner scanner = new Scanner(System.in);
        // Create one machine instance that persists across commands
        CoffeeMachine machine = new CoffeeMachine();
    }
}

What does this code do?

import java.util.Scanner brings in Java's built-in class for reading keyboard input. Without it, your program cannot receive typed commands.

Scanner scanner = new Scanner(System.in) creates a scanner attached to the standard input stream. CoffeeMachine machine = new CoffeeMachine() creates the machine object whose state will persist as the user buys drinks and refills.

  • Save Main.java by pressing Ctrl+S.

Seeing a red underline on CoffeeMachine?

Make sure CoffeeMachine.java is saved and sits inside the same src folder as Main.java. IntelliJ needs both files in the same source root to find the class.

Help me fix my CoffeeMachine import error

Implement the menu loop

A real vending machine waits for someone to press a button, serves the drink, then waits again. You will replicate that behavior with an infinite loop that only breaks when the user types exit.

  • In Main.java, find the line CoffeeMachine machine = new CoffeeMachine(); and add the following while loop directly below it (still inside the main method):
        while (true) {
            System.out.println("Write action (buy, fill, take, remaining, exit):");
            String action = scanner.nextLine();

            switch (action) {
                case "buy":
                    // Ask which drink and map choice to enum
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    String choice = scanner.nextLine();
                    switch (choice) {
                        case "1":
                            machine.buy(Coffee.ESPRESSO);
                            break;
                        case "2":
                            machine.buy(Coffee.LATTE);
                            break;
                        case "3":
                            machine.buy(Coffee.CAPPUCCINO);
                            break;
                        default:
                            System.out.println("Invalid choice!");
                            break;
                    }
                    break;
                case "fill":
                    // Prompt for each resource amount
                    System.out.println("Write how many ml of water you want to add:");
                    int addWater = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many ml of milk you want to add:");
                    int addMilk = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many grams of coffee beans you want to add:");
                    int addBeans = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many disposable cups you want to add:");
                    int addCups = Integer.parseInt(scanner.nextLine());
                    machine.fill(addWater, addMilk, addBeans, addCups);
                    break;
                case "take":
                    machine.take();
                    break;
                case "remaining":
                    machine.showResources();
                    break;
                case "exit":
                    scanner.close();
                    return;
                default:
                    System.out.println("Unknown action!");
                    break;
            }
            System.out.println();
        }

What does this code do?

  • while (true) keeps the program running until the user explicitly types exit.
  • The outer switch reads the user's command and routes it to the matching method on the machine.
  • The buy case uses a nested switch to map numbers (1, 2, 3) to Coffee enum values.
  • The fill case prompts for four numbers and uses Integer.parseInt() to convert the typed strings into integers.
  • The exit case calls scanner.close() to release the input stream, then return ends the main method (and the program).
  • Save Main.java.

Seeing a compile error about a duplicate variable?

Make sure the while loop is inside the main method's curly braces and not accidentally pasted outside them. Also check that you did not leave the original empty main method braces above.

Help me fix my compile error in Main.java

✔️ Awesome, I've got everything!

Great. Make sure you have saved Main.java before running in the next substep.

ⓧ I'd like to double check the full code

import java.util.Scanner;

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

        while (true) {
            System.out.println("Write action (buy, fill, take, remaining, exit):");
            String action = scanner.nextLine();

            switch (action) {
                case "buy":
                    System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino:");
                    String choice = scanner.nextLine();
                    switch (choice) {
                        case "1":
                            machine.buy(Coffee.ESPRESSO);
                            break;
                        case "2":
                            machine.buy(Coffee.LATTE);
                            break;
                        case "3":
                            machine.buy(Coffee.CAPPUCCINO);
                            break;
                        default:
                            System.out.println("Invalid choice!");
                            break;
                    }
                    break;
                case "fill":
                    System.out.println("Write how many ml of water you want to add:");
                    int addWater = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many ml of milk you want to add:");
                    int addMilk = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many grams of coffee beans you want to add:");
                    int addBeans = Integer.parseInt(scanner.nextLine());
                    System.out.println("Write how many disposable cups you want to add:");
                    int addCups = Integer.parseInt(scanner.nextLine());
                    machine.fill(addWater, addMilk, addBeans, addCups);
                    break;
                case "take":
                    machine.take();
                    break;
                case "remaining":
                    machine.showResources();
                    break;
                case "exit":
                    scanner.close();
                    return;
                default:
                    System.out.println("Unknown action!");
                    break;
            }
            System.out.println();
        }
    }
}
Run and test your application

With all three files saved, you can now compile and run the full program directly from IntelliJ.

  • Click the green Run button in the top toolbar, or press Shift+F10 to run the application.
  • IntelliJ opens a Run panel at the bottom. You should see the prompt: Write action (buy, fill, take, remaining, exit):.
  • Type remaining and press Enter. You should see the machine's starting resources printed.
  • Type buy and press Enter. When prompted, type 1 to buy an espresso.
  • Type remaining again. Verify that water dropped by 250ml, beans dropped by 16g, cups dropped by 1, and money increased by $4.
  • Type take to collect the money. You should see I gave you $554 (the starting $550 plus the $4 espresso).
  • Type exit to stop the program.

What should I see?

After buying one espresso and running remaining, the output shows 150 ml of water, 540 ml of milk, 104 g of coffee beans, 8 disposable cups, and $554 of money.

Program won't start or shows an error?

  • If you see "Cannot find main class", right-click Main.java in the Project panel and select Run 'Main.main()' to set the run configuration.
  • If you see a compile error in Coffee.java or CoffeeMachine.java, fix those files first. IntelliJ will not run the project until all files compile.

Help me get my Coffee Machine program to run

Secret mission

Add a Cleaning System

Real coffee machines need regular maintenance. Can you add a cleaning system that tracks how many drinks the machine has served and blocks new orders after every 10 cups until someone cleans it?

Clean Up Your Resources

Clean Up Your Resources

This project runs entirely locally, so there are no ongoing costs. Decide whether to keep your project files and tools installed, or delete them entirely.

Resources you used:

  • CoffeeMachineSimulator project folder (local files in IntelliJ IDEA).
  • JDK 25 (installed on your system).
  • IntelliJ IDEA (installed on your system).

Keep everything running

No action needed. Choose this if you plan to keep building on your Coffee Machine Simulator or want to explore more Java projects.

  • Your project files stay in the IntelliJ IDEA workspace.
  • JDK 25 and IntelliJ IDEA remain installed and ready for future Java development.

Pause - I'll come back to this later

Close IntelliJ IDEA to free up memory. Your project files and installed tools stay on your system for when you return.

  • Close IntelliJ IDEA if it is still open.
  • Your CoffeeMachineSimulator project will be available in IntelliJ's recent projects list when you reopen it.

Delete - I don't want to use this again

Remove all project files and optionally uninstall the tools you installed during this project.

  • Delete the CoffeeMachineSimulator project folder. In IntelliJ IDEA, right-click the project in the Welcome screen's project list and select Remove from Recent Projects. Then delete the project folder from your file system.
  • To uninstall JDK 25 on Windows, open Settings, select Apps, then Installed apps. Find Java(TM) SE Development Kit 25 in the list and click Uninstall.
  • To uninstall JDK 25 on macOS, delete the JDK directory by running:
sudo rm -rf /Library/Java/JavaVirtualMachines/jdk-25.jdk
  • To uninstall IntelliJ IDEA on Windows, open Settings, select Apps, then Installed apps. Find IntelliJ IDEA in the list and click Uninstall.
  • To uninstall IntelliJ IDEA on macOS, drag the IntelliJ IDEA app from your Applications folder to the Trash.

Nice Work!

Nice Work!

You just built a fully functional Coffee Machine Simulator in Java. From defining drink types to managing resources and handling user input, you created a multi-class application that demonstrates core object-oriented programming principles.

You've learned how to:

  • Create a Java enum with fields, a constructor, and getter methods to define type-safe constants with behaviour.
  • Build a class with encapsulation by using private fields and public methods to protect internal state from outside code.
  • Use control flow (while loops, switch statements, and conditionals) to create an interactive menu-driven application.
  • Added a maintenance system that tracks cups served and enforces periodic cleaning after every 10 drinks.

Ready to quiz yourself?