GITHUB REPO | FOLLOW ME ON -> TWITTER | LINKEDIN

How to Build a Juice Bar in Python: A Step-by-Step Guide

Al-Junaed Islam
8 min readMay 15, 2023

Have you ever wanted to open a juice bar, but don’t have the funds to get started? Look no further than Python. This programming language can help you build a virtual juice bar. In this article, we will create a juice bar program from scratch that will allow users to order different types of juice. We will use Python concepts like dictionaries, loops, and functions. By the end of this article, you will have a better understanding of Python and how it can be used to create programs.

Welcome Board

Experience the thrill of playing the game — click the play button below and play now!

The first thing we need to do is set up our Python Juice Machine. Open up your preferred code editor, create a new file, and save it with a descriptive name such as “juice_bar.py.”. Also, create other files which will be used as modules art.py, data.py.

art.py

logo = """
_ _ _
/ \ | |( )___
/ _ \ _ | ||// __|
/ ___ \| |_| | \__ \
_ _ /_/ \_\\___/ |___/
| | _ _ (_) ___ ___ ___ ___ _ __ _ __ ___ _ __
_ | || | | || | / __|/ _ \ / __|/ _ \ | '__|| '_ \ / _ \| '__|
| |_| || |_| || || (__| __/ | (__| (_) || | | | | || __/| |
\___/ \__,_||_| \___|\___| \___|\___/ |_| |_| |_| \___||_|
"""

admin_logo = """
_ _ _
/ \ __| | _ __ ___ (_) _ __
/ _ \ / _` || '_ ` _ \ | || '_ \
/ ___ \| (_| || | | | | || || | | |
/_/ \_\\__,_||_| |_| |_||_||_| |_|
"""

ASCII art of welcome logo and admin logo.

data.py

menu = {
"apple": {
"cost": 1,
"price": 2
},
"papaya": {
"cost": 0.5,
"price": 1
},
"pineapple": {
"cost": 0.25,
"price": 0.5
},
"watermelon": {
"cost": 1,
"price": 2
},
"kiwi": {
"cost": 2.25,
"price": 5
},
"banana": {
"cost": 0.5,
"price": 1
},
"lemon": {
"cost": 0.1,
"price": 0.25
}
}

resources = {
"apple": 5,
"papaya": 5,
"pineapple": 5,
"watermelon": 5,
"kiwi": 5,
"banana": 5,
"lemon": 5,
"water": 500,
"suger": 30
}

payment = {
"Quarter": 0.25,
"1 Dollar Note": 1,
"5 Dollar Note": 5,
"10 Dollar Note": 10
}

This file consists of the data of menu, resources and payment. Which are dictionaries.

juice_bar.py

First, let’s import the necessary modules for our program. The data module contains a dictionary for our menu, resources, and payment options, while the ‘art’ module contains ASCII art logos for our program.

from data import menu, resources, payment
from art import logo, admin_logo

When the program runs, sets the initial values of profit, bill, and machine_run, and then starts the vending machine loop. It first displays the main menu of juice options, prompts the user for their choice, If the user types ajbrohi(ssshh secret input to access admin) in place of a juice name, the program switches to the admin function. Otherwise, the program will execute the user function to process the user’s juice request.

##########
# all the functions define here.
##########

profit = 0
bill = 0
machine_run = True

while machine_run:
print(logo)

print("Welcome To Our Juice Vending Machine. Here you can create your own juice from your preference.")

print_menu()

juice_choice = input("What would you like to have? - ").lower()

if juice_choice == 'ajbrohi':
admin()
else:
user(juice_choice)

print_menu(): function prints the names of all the juices in the menu dictionary in capitalized form.

def print_menu():
for key in menu:
print(key.capitalize())

admin(): This function provides an interface for the admin user to manage the resources and view the current profit. It displays a menu of options, such as "Report" to see resources, "Update" to add resources, "Profit" to view the current profit, and "Cash Out" to cash out the profit. The function waits for user input and executes the corresponding option. If the user chooses "Off", they are logged out and the vending machine stops running.

def admin():
global machine_run, profit
log_out = False

while not log_out:
print(admin_logo)
print("Welcome to admin panel. Type to execute the options.")
admin_choice = input(
"""Report: To see resources
Update: To update resources
Profit: To see current profit
Cash Out: To cash out the profit
Off: Logout
- """).lower()
if admin_choice == "report":
for key in resources:
print(f"{key}: {resources[key]}")
elif admin_choice == "update":
item = input("What item would you like to add? - ").lower()
amount = int(input("How much would you like to add? - "))
resources[item] += amount
elif admin_choice == "profit":
print(f"Current profit is - {profit}")
elif admin_choice == "cash out":
print(f"Current profit is - {profit}")
profit = 0
print(f"Profit cashed out. Now balance is {profit}")
else:
log_out = True
machine_run = False

get_cost(juice_choice): This function takes a juice_choice parameter and returns the cost of the juice from the menu dictionary.

def get_cost(juice_choice):
return menu[juice_choice]["cost"]

get_price(juice_choice): This function takes a juice_choice parameter and returns the price of the juice from the menu dictionary.

def get_price(juice_choice):
return menu[juice_choice]["price"]

update_resources(size_choice, suger_choice): This function takes two parameters: size_choice and suger_choice. It updates the resources dictionary by subtracting the appropriate amounts of water and sugar depending on the size and whether or not sugar is desired. If any of the resources go below zero, the function returns False to indicate that the juice cannot be made. Otherwise, it returns True.

def update_resources(size_choice, suger_choice):
resources["water"] -= 150 if size_choice == "large" else 75
resources["suger"] -= 5 if suger_choice == "yes" else 0
return (
resources[juice_choice] >= 0
and resources['water'] >= 0
and resources['suger'] >= 0
)

bill_payment(juice_price): This function takes a juice_price parameter and handles the payment process for the juice. It keeps track of the amount of money inserted and prompts the user to insert more money if the current amount is not enough to cover the juice price. It returns when the bill is fully paid.

def bill_payment(juice_price):
global bill
bill_paid = False

while not bill_paid:
for key in payment:
if bill >= juice_price:
bill_paid = True
break
else:
bill += payment[key] * int(input(f"How many {key}? = "))
print(f"You paid - {bill}")
if not bill_paid:
pay_more = juice_price - bill
print(
f"Sorry that's not enough. You have to pay more ${pay_more}")

update_profit(juice_cost, juice_price): This function takes two parameters: juice_cost and juice_price. It updates the profit variable by adding the difference between the juice price and cost.

def update_profit(juice_cost, juice_price):
global profit
profit = profit + (juice_price - juice_cost)

juice_processing(juice_choice, juice_price, juice_cost): This function takes three parameters: juice_choice, juice_price, and juice_cost. It handles the process of making the juice. It first displays the price of the juice and asks the user to pay the bill. It then dispenses the juice and updates the profit. Finally, it prompts the user to continue or end the program.

def juice_processing(juice_choice, juice_price, juice_cost):
global bill, machine_run

print(f"Price of {juice_choice} juice is ${juice_price}")

print("Please pay the bill by inserting coins or dollars.")

bill_payment(juice_price)

if bill > juice_price:
user_return = bill - juice_price
print(f"Here is ${user_return} in change")

print(f"Here is your {juice_choice} juice. Enjoy!!")

update_profit(juice_cost, juice_price)

user_choice = input(
"Would you like to have juice again? (Yes/No) - ").lower()
if user_choice == 'no':
machine_run = False
print("Thank you for using our program.")

user(juice_choice): This function takes a juice_choice parameter and handles the user's interaction with the vending machine. It prompts the user for the cup size and whether or not sugar is desired. It then calls update_resources() to check if there are enough resources to make the juice. If there are, it calls juice_processing() to make the juice. If there aren't, it displays an error message

def user(juice_choice):
global machine_run
juice_price = get_price(juice_choice)
juice_cost = get_cost(juice_choice)

size_choice = input(
"What size of cup would you like to have? (Large/Regular) - ").lower()
suger_choice = input("Do you like to have suger? (Yes/No) - ").lower()
if update_resources(size_choice, suger_choice):
juice_processing(juice_choice, juice_price, juice_cost)
else:
print("Sorry one of the item is not enough in stock. Try again later.")
machine_run = False

Full code

Don’t miss out on the full code — check it out below!

Sample Run

From Left — User Part and Admin Panel

Features

Program’s Features

The program’s features include:

  • The program allows users to order their favorite juice from a menu and pay the bill using coins or dollars.
  • The program updates the resources dictionary based on the user’s juice size and sugar choice.
  • The program keeps track of the profit and displays it to the admin.
  • The program has an admin panel by inputting a secret code that allows the admin to see, update resources and check profits and cash out them.

Python Concepts and Techniques Used

The Python concepts and techniques used in the program include:

  • Dictionaries are used to store the menu, resources, and payment options.
  • Loops are used to keep the program running until the user wants to.
  • Functions are used to encapsulate the code and reduce redundancy.
  • Variables are used to keep track of the bill, profit, and resources.

In this article, we learned how to create a juice bar program with Python. We learned how to use dictionaries, loops, functions, and variables to create a program that allows users to order their favorite juice and pay the bill using coins or dollars. We also learned how to create an admin panel that can update resources and check profits.

Ready to Level Up? Complete These Challenges

Now that you’ve learned how to create a customizable juice machine, it’s time to put your skills to the test! Here are some fun challenges to help you take your Python knowledge to the next level. Are you ready to level up? Let’s go!

  1. Add a feature to the admin panel that allows an admin to add a new item to the menu and set its cost and price.
  2. Implement a check to ensure that the user input for juice selection is valid and matches a menu item.
  3. Add an option for the user to customize their juice by adding more fruits.
  4. Modify the print_menu() function to display all menu items with their price.
  5. Create a function that calculates and returns the total profit made by the juice bar.

This article is a part of my Python Project Journal. To learn more about my journey in learning Python and to see my other projects, check out this project journal. Thank you!

--

--