Coffee Machine | Python

TRY IT HERE

It’s fair to say coffee is a staple of most lives, as one of the most consumed beverages in the world. That’s why I have built a virtual coffee machine for my next Python project. In its current state, the virtual machine can make you an espresso, latte, or cappuccino to serve your digital caffeine needs.

For those more interested in maintaining the virtual machine, you can enter ‘report’ to see the current money in the machine, and the current water, milk, and coffee levels. Alternatively, entering ‘service’ will refill the resources if they are too depleted to make the next beverage. Finally, entering ‘off’ will restart the machine and cash out the current balance.

Let’s take a look at the code…

First, a couple of dictionaries are created in a file called data. The first dictionary, named ‘MENU’, has three nested dictionaries for each of the three types of drink made. Within each nested dictionary is “cost” and another nested dictionary named “ingredients”. Separate from the dictionaries within ‘MENU’, another dictionary named ‘resources’ is defined.

The ‘resources’ dictionary holds the amount of water, milk, and coffee that can be held within the machine for usage.

Now, within the main.py file, both ‘MENU’ and ‘resources’ are imported. Additionally, the variable ‘money(which will hold the profits of the machine) is defined with the value 0 and ‘machine_active’ is set to the Boolean value True.

The first process that needs to occur is to ensure the coffee machine has sufficient resources (water, milk, and coffee) to make the requested drink. To do this, a function named check_resources() is then defined, which takes the argument ‘order_ingredients’.

When called, this function checks each item listed in 'order_ingredients’. If an item within ‘order_ingredients’ is greater than the value for that item in ‘resources’, a message is displayed advising that there is not enough of the specific item and returns the Boolean value False. Otherwise, the Boolean value True is returned, indicating the resource check was successful.

After ensuring the coffee machine has enough resources to fulfill the order, it’s time to request payment from the customer.

This is accomplished with the defined request_payment() function, which prompts the user to enter each type of coin as an integer. Each value is then multiplied by the value of said coin and assigned to the variable ‘total’; which holds the total of all coins entered.

After taking payment, the machine then processes the payment against the cost of the beverage selected.

The defined process_payment() function takes the arguments ‘payment’ and ‘drink_cost’. If the payment is greater than or equal to the drink cost, the difference from the transaction is returned to the customer, the profit or cost of the beverage is added to the variable ‘money’, and the function returns True. Otherwise, the customer is notified that their payment was not sufficient and that the money has been refunded. This returns the Boolean value of False.

After processing the payment, the machine then makes the requested beverage.

The function make_drink() is defined and takes the two arguments of ‘drink_name’ and ‘order_ingredients’. Each item’s value required to make the drink is deducted from the machine’s resources and a message displaying that the drink is hot is then presented.

If this software was in a physical machine, it would be at this point that the beverage is dispensed.

Now, it’s time for the machine to actually interact with the customer. While the machine is active, it will prompt the user to make a drink selection; however, three administrative functions are checked first:

  • If “off” is entered:

    • The machine will turn off by setting ‘machine_active’ to False. This will also cash out the machine and set ‘resources‘ to its default values.

  • If “report” is entered:

    • A report with the current ‘resources‘ values and the value of ‘money ‘ stored will be displayed.

  • If “service” is entered:

    • The machine will print a confirmation and replenish the values of ‘resources’ to their default values without cashing out the machine.

Otherwise, the input will be one of the three drink selections and will be stored in the variable ‘drink’. If check_resources() returns True, the variable ‘payment’ is assigned with the value of ‘total’ returned from the request_payment() function. If process_payment() returns True, make_drink() is called, and the program is returned back to the main prompt for the next customer.


This was a pretty fun project to make, as I can see its practical usage embedded within a system. You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!

Next
Next

Higher/Lower Game | Python