Bakery Management System in Python | Class 12 Project

Bakery Management System in Python

In this article, we are going to build one such application that can manage a bakery. Yes! The bakery products are everyone’s favorite, right? We are going to build a console-based Bakery Management System in Python that can manage our bakery which cooks delicious kinds of stuff!

This article is your end of wander for your class 12 project on Bakery Management System.

Project Overview: Bakery Management System in Python

Our bakery’s name is Moon Pie Bakery. And we are going to build a Python application for it. This is going to be a console-based application.

Project Working:

We will store the data (product names and their prices) of our bakery in a database.
This data can be accessed as:
1. Admin Login:
The admin can access the data, view the items, add or delete an item, and update the price of an item.
2. Customer login:
This will be the login for customer billing. Biller will note the items and their quantities as per the customer’s input and generate the bill for the same.

Let’s design this system:

Workflow:

  1. Firstly, we are going to create a database to store the items and their prices that we are selling in our bakery.
  2. The user will have 3 choices: admin login, customer login, and exit. A while loop will be run infinite times until the user chooses the exit option.
  3. To log in as an admin, a password is needed. For the admin login, there will be a separate function with options to: add an item, remove an item, update an item, display the items, and exit from the section.
  4. For customer login, the biller will take the customer’s name and order as inputs and generate the bill. Here also, there will be an option to exit the section.
  5. Every step will have a time gap to make the result look systematic and readable.

Coding Class 12 Bakery Management System in Python

1. Import the modules

The only modules required here are sqlite3 for the database and the time module for the sleep time.
sqlite3 is the sqlite3 database integrated with Python. There is no need to additionally install this module as it comes bundled with Python.

import sqlite3
import time

2. Create the database

The steps to create an SQLite database in Python is simple. Just establish the connection and every time you will execute a query, execute it with a cursor object.

connection = sqlite3.connect('menu.db')
cursor = connection.cursor()

menu will be the name of our database. Now create the table, Menu. It will require the following three entities: product_id (primary key + autoincrement), product_name, and product_price.

cursor.execute('''
CREATE TABLE IF NOT EXISTS Menu([product_id] INTEGER PRIMARY KEY AUTOINCREMENT, 
[product_name] TEXT,[product_price] INTEGER)''')

The keywords IF NOT EXISTS will make sure that irrespective of how much code will be run, the database will only be created once.

Let’s insert values into the database. This can be done in two ways: you can either run a query inside the code for that or you can manually open the database file into SQLite studio and insert the values.

query = '''SELECT *  FROM Menu'''
cursor.execute(query)
results = cursor.fetchall()
if results==[]:
    cursor.execute('''
                    INSERT INTO Menu (product_name, product_price)
                    VALUES
                    ('Cake',400),
                    ('Bread',50),
                    ('Cookies',100),
                    ('Doughnuts',80),
                    ('Pie',120)
                    ''')
    connection.commit()

Again, to make sure that the values are inserted only once, check if the Menu table is empty.

3. Design a main function

This will be the main function of our code. Here we will display welcome messages and give choices to users for proceeding functions.
The password for the admin login is MoonPie and it will be verified first whenever an admin attempts to log in.
For both admin and customer login, we will design separate functions which will take the connection and cursor as parameters.

def main():
    inloop = 1
    while inloop:
        print()
        print("---------------------------------------------------------------------")
        print("---------------------Welcome to Moon Pie Bakery!---------------------")
        print("---------------------------------------------------------------------")
        
        print("How you want to Enter?")
        print("Choice 1: Admin Login","Choice 2: Customer Login", "Choice 3: Exit",sep="\n")

        choice = input("Enter your choice:")
       
  
        if choice=='1':
            password = input("Enter the password: ")
            if password=="MoonPie":
                admin_login(connection,cursor)
            else:
                print("Incorrect Password!")
                time.sleep(1)
        
        elif choice=='2':
            customer_login(connection,cursor)
        elif choice=='3':
            exit()
        else:
            print("Invalid Choice!")

The loop will only be broken in case the user chooses to exit.

4. Design function for admin login

The function admin_login() will have 5 choices:
1. add an item: The item name and price will be inputted and inserted into the table using an insert query.
2. remove an item: Using the product id as input, the corresponding row will be deleted using a delete query.
3. update an item: The product price will be updated by taking the product id and new price as input using an update query.
4. display all the items: A function will be designed to display the menu list with its prices. This function will be used whenever required in other choices as well.
5. exit from the function: This choice will redirect to the main function.

def admin_login(connection,cursor):
    print()
    print("---------Welcome! You are logged in as Admin!----------")
    print()
    print("Here are the list of choices:")
    print("Choice 1: Add an item",
            "Choice 2: Remove an item",
            "Choice 3: Update item price",
            "Choice 4: See all the items",
            "Choice 5:Exit",
            sep="\n")

    choice = int(input("Enter your choice: "))
    print()
    time.sleep(0.5)

    if choice==1:
        print("What you like to add?")
        product_name = input("Enter product name: ")
        product_price = input("Enter product price: ")

        try:
            query=f'''INSERT INTO Menu(product_name, product_price) VALUES ('{product_name}','{product_price}')'''
            cursor.execute(query)
            connection.commit()
            print("The item has been added to the list!")
        except Exception as e:
            print("Error occured!")
        
        time.sleep(1)
        admin_login(connection,cursor)

    elif choice==2:
        display_items(cursor)
        print("Which item you would like to remove?")
        id = int(input("Enter product id:"))
        try:
            query=f'''DELETE FROM Menu WHERE product_id={id}'''
            cursor.execute(query)
            connection.commit()
            print("The item has been removed from the shop!")
        except Exception as e:
            print("Invalid item!")
        time.sleep(1)
        admin_login(connection,cursor)
       
    elif choice==3:
        display_items(cursor)
        print("Which item price you would like to update?")
        id=int(input("Enter product ID:"))
        price=int(input("Enter the updated price:"))
        try:
            query=f'''UPDATE Menu SET product_price={price} WHERE product_id={id}'''
            cursor.execute(query)
            connection.commit()
            print("The item price has been updated!")
        except Exception as e:
            print("Invalid Product ID!")
        
        time.sleep(1)
        admin_login(connection,cursor)

    elif choice==4:
        display_items(cursor)
        time.sleep(1.5)
        admin_login(connection,cursor)
    elif choice==5:
        main()
    else:
        print("Invalid Choice!")
        time.sleep(1)
        admin_login(connection,cursor)

From the code, you can see that for the choices of update and delete we are displaying the menu first, making it suitable for the user to input the correct id and price.
Let’s code display_items() now.

def display_items(cursor):
    query='''SELECT * FROM Menu'''
    cursor.execute(query)
    results=cursor.fetchall()
    print("List of items: ")
    print("ID","Name","Price",sep=" ")
    for each in results:
        print(each[0],each[1],each[2],sep=" ")

time.sleep() takes seconds as parameters and set the given time as an interval between the two code lines.

5. Design a function for customer login

Customer login will have only 2 choices:
1. Billing: The customer’s name will be taken as input. Multiple items can be ordered with desired quantity. The function will then generate the bill containing the description of items with their quantities and total price.
2. Exit: This choice will redirect to the main function.

def customer_login(connection,cursor):
    print("-----------Welcome,You are logged in as a biller!-------------")
    print("Here is the list of choices:")
    print("Choice 1: Billing", "Choice 2: Exit",sep="\n")
    choice = int(input("Enter your choice:"))
    if(choice==1):
        name = input("Enter the customer name:")
        print(f"What do you wanna buy {name}?")
        time.sleep(0.5)
        display_items(cursor)

        print()
        total = 0
        items=[]
        while 1:

            id=int(input("Enter the product ID:"))
            quantity =int(input("Enter the quantity:"))
            try:
                query=f'''SELECT * FROM Menu WHERE product_id={id}'''
                cursor.execute(query)
                result=cursor.fetchone()
                
                total += result[2]*quantity
                items.append([result[1],quantity])
                i=input("Anything else?Answer Y for Yes and N for No! ")
                if(i=='N'):
                    break
            except Exception as e:
                print("Invalid Entry!")
                print(e)
                break
        
        if(total!=0):
            print()
            print("---------Moon Pie Bakery--------")
            print("-------Billing Details-------")
            print(f"Name:{name}")
            print(f"Items:")
            for each in items:
                print(each[0],each[1],sep=":")
            print(f"Total: {total}")
            print("Thank you! Have a sweet day!")
            print()

        time.sleep(1)
        customer_login(connection,cursor)
    elif choice==2:
        main()
    else:
        print("Invalid Choice!")
        time.sleep(1)
        customer_login(connection,cursor)

Now, the only thing left is to call the main function and start the program execution.

main()

Output:

Complete code for Bakery Management System in Python

'''
facilities: messages, view menu 
1. admin login : add new item | delete item | change item price
2. customer : billing | customer name, phone number
3. exit


'''

import sqlite3
import time

connection = sqlite3.connect('menu.db')
cursor = connection.cursor()
cursor.execute('''
                CREATE TABLE IF NOT EXISTS Menu([product_id] INTEGER PRIMARY KEY AUTOINCREMENT, 
                [product_name] TEXT, 
                [product_price] INTEGER)''')

query = '''SELECT *  FROM Menu'''
cursor.execute(query)
results = cursor.fetchall()
if results==[]:
    cursor.execute('''
                    INSERT INTO Menu (product_name, product_price)
                    VALUES
                    ('Cake',400),
                    ('Bread',50),
                    ('Cookies',100),
                    ('Doughnuts',80),
                    ('Pie',120)
                    ''')
    connection.commit()


def display_items(cursor):
    query='''SELECT * FROM Menu'''
    cursor.execute(query)
    results=cursor.fetchall()
    print("List of items: ")
    print("ID","Name","Price",sep=" ")
    for each in results:
        print(each[0],each[1],each[2],sep=" ")


def admin_login(connection,cursor):
    print()
    print("---------Welcome! You are logged in as Admin!----------")
    print()
    print("Here are the list of choices:")
    print("Choice 1: Add an item",
            "Choice 2: Remove an item",
            "Choice 3: Update item price",
            "Choice 4: See all the items",
            "Choice 5:Exit",
            sep="\n")

    choice = int(input("Enter your choice: "))
    print()
    time.sleep(0.5)

    if choice==1:
        print("What you like to add?")
        product_name = input("Enter product name: ")
        product_price = input("Enter product price: ")

        try:
            query=f'''INSERT INTO Menu(product_name, product_price) VALUES ('{product_name}','{product_price}')'''
            cursor.execute(query)
            connection.commit()
            print("The item has been added to the list!")
        except Exception as e:
            print("Error occured!")
        
        time.sleep(1)
        admin_login(connection,cursor)

    elif choice==2:
        display_items(cursor)
        print("Which item you would like to remove?")
        id = int(input("Enter product id:"))
        try:
            query=f'''DELETE FROM Menu WHERE product_id={id}'''
            cursor.execute(query)
            connection.commit()
            print("The item has been removed from the shop!")
        except Exception as e:
            print("Invalid item!")
        time.sleep(1)
        admin_login(connection,cursor)
       
    elif choice==3:
        display_items(cursor)
        print("Which item price you would like to update?")
        id=int(input("Enter product ID:"))
        price=int(input("Enter the updated price:"))
        try:
            query=f'''UPDATE Menu SET product_price={price} WHERE product_id={id}'''
            cursor.execute(query)
            connection.commit()
            print("The item price has been updated!")
        except Exception as e:
            print("Invalid Product ID!")
        
        time.sleep(1)
        admin_login(connection,cursor)

    elif choice==4:
        display_items(cursor)
        time.sleep(1.5)
        admin_login(connection,cursor)
    elif choice==5:
        main()
    else:
        print("Invalid Choice!")
        time.sleep(1)
        admin_login(connection,cursor)
    

def customer_login(connection,cursor):
    print("-----------Welcome,You are logged in as a biller!-------------")
    print("Here is the list of choices:")
    print("Choice 1: Billing", "Choice 2: Exit",sep="\n")
    choice = int(input("Enter your choice:"))
    if(choice==1):
        name = input("Enter the customer name:")
        print(f"What do you wanna buy {name}?")
        time.sleep(0.5)
        display_items(cursor)

        print()
        total = 0
        items=[]
        while 1:

            id=int(input("Enter the product ID:"))
            quantity =int(input("Enter the quantity:"))
            try:
                query=f'''SELECT * FROM Menu WHERE product_id={id}'''
                cursor.execute(query)
                result=cursor.fetchone()
                
              
                total += result[2]*quantity
                items.append([result[1],quantity])
                i=input("Anything else?Answer Y for Yes and N for No! ")
                if(i=='N'):
                    break
            except Exception as e:
                print("Invalid Entry!")
                print(e)
                break
        
        if(total!=0):
            print()
            print("---------Moon Pie Bakery--------")
            print("-------Billing Details-------")
            print(f"Name:{name}")
            print(f"Items:")
            for each in items:
                print(each[0],each[1],sep=":")
            print(f"Total: {total}")
            print("Thank you! Have a sweet day!")
            print()

        time.sleep(1)
        customer_login(connection,cursor)
    elif choice==2:
        main()
    else:
        print("Invalid Choice!")
        time.sleep(1)
        customer_login(connection,cursor)
            
    

def main():
    inloop = 1
    while inloop:
        print()
        print("---------------------------------------------------------------------")
        print("---------------------Welcome to Moon Pie Bakery!---------------------")
        print("---------------------------------------------------------------------")
        
        print("How you want to Enter?")
        print("Choice 1: Admin Login","Choice 2: Customer Login", "Choice 3: Exit",sep="\n")


        choice = input("Enter your choice:")
       
        
        if choice=='1':
            password = input("Enter the password: ")
            if password=="MoonPie":
                admin_login(connection,cursor)
            else:
                print("Incorrect Password!")
                time.sleep(1)
        
        elif choice=='2':
            customer_login(connection,cursor)
        elif choice=='3':
            exit()
        else:
            print("Invalid Choice!")
        
main()

Thank-You for visiting our website.


Also Read:

Share:

Author: Ayush Jha