CS 5 BookStoreManagementSystem
CS 5 BookStoreManagementSystem
2024-2025
Project Topic
Submitted by:
Name:
Class:
Roll Number:
CONTENTS
S.No. TOPIC PAGE NO
1 Certificate 3
2 Acknowledgement 4
3 Hardware’s and 5
Software’s required
4 Introduction 6
6 MySQL Database 18
7 Outputs 20
8 References 27
CERTIFICATE
Principal
ACKNOWLEDGEMENT
Hardware:
Desktop / Laptop
Software:
PyCharm IDE
Python
MySQL
Python-MySQL connector module
INTRODUCTION
DB = s.connect(host="localhost",user="root", password="pwd_pwd",
database="book_store")
C = DB.cursor() # ADMIN FUNCTIONS
logged_in = False
def add_book():
book = input("Enter Book Name: ")
genre = input("Genre:")
quantity = int(input("Enter quantity:"))
author = input("Enter author name:")
price = int(input("Enter the price:"))
C.execute("INSERT INTO available_books values('{}','{}',{},'{}',{})"\
.format(book, genre, quantity, author,price))
DB.commit()
print(" ++++ SUCCESSFULLY ADDED +++++++++++++ ")
def view_books():
C.execute("SELECT * FROM available_books" )
books = C.fetchall()
if books:
columns = []
for i in C.description:
columns.append(i[0])
print(tabulate(books, headers=columns, tablefmt="fancy_grid"))
else:
print("No books present")
def update_book():
name = input("Enter the name of book to be updated:")
C.execute("SELECT * FROM available_books WHERE book ='{}'".format(name))
book = C.fetchone()
if book:
choice = int(input("1 ---> Update genre 2---> Update quantity 3--->Update price 4--
>Exit : "))
if choice == 1:
new_genre = input("Enter new genre : ")
C.execute("UPDATE available_books SET genre = '{}' WHERE
book='{}'".format(new_genre,name))
DB.commit()
print("Successfully updated genre ")
elif choice == 2:
new_quantity = input("Enter new quantity : ")
C.execute("UPDATE available_books SET quantity = {} WHERE
book='{}'".format(new_quantity,name))
DB.commit()
print("Successfully updated quantity ")
elif choice == 3:
new_price = input("Enter new price : ")
C.execute("UPDATE available_books SET price = {} WHERE
book='{}'".format(new_price,name))
DB.commit()
print("Successfully updated price ")
else:
print("Enter a valid choice")
else:
print("No book with this name")
def delete_book():
name = input("Enter the book to be deleted: ")
C.execute("SELECT * FROM available_books WHERE book ='{}'".format(name))
book = C.fetchone()
if book:
C.execute("DELETE FROM available_books WHERE book='{}'".format(name))
DB.commit()
print("Successfully deleted book")
else:
print("No book with this name")
def search_by_name():
book = input("Enter the book name to be searched: ")
C.execute("SELECT * FROM available_books WHERE book ='{}'".format(book))
books = C.fetchall()
if books:
columns = []
for i in C.description:
columns.append(i[0])
print(tabulate(books, headers=columns, tablefmt="fancy_grid"))
else:
print("No book with this name")
def search_by_genre():
genre = input("Enter the genre to be searched: ")
C.execute("SELECT * FROM available_books WHERE genre ='{}'".format(genre))
books = C.fetchall()
if books:
columns = []
for i in C.description:
columns.append(i[0])
print(tabulate(books, headers=columns, tablefmt="fancy_grid"))
else:
print("No book with this genre")
def purchase_book():
book_name = input("Enter the name of the book : ")
C.execute("SELECT * FROM available_books WHERE book ='{}'".format(book_name))
book = C.fetchone()
if book:
quantity = int(input("How many books you want to buy : "))
if book[2] >= quantity:
C.execute("UPDATE available_books SET quantity={}-{} WHERE book
='{}'".format(book[2], quantity, book_name))
DB.commit()
print(" ################# ")
print(" Purchase successful ")
print("Receipt: ")
print("Book purchased :",book_name)
print("Total Bill : ", book[4]*quantity)
print(" ################# ")
else:
print("There are only {} copies of '{}' available".format(book[2],book_name))
else:
print("No book with this name")
def user_register():
while True:
user = input("Enter username: ")
C.execute("SELECT * FROM users WHERE user ='{}'".format(user))
data = C.fetchone()
if data:
print("User already exists. Try different username ")
else:
password = input("Enter password: ")
C.execute("INSERT INTO users VALUES('{}','{}')".format(user,password))
DB.commit()
print("User registered successfully")
break
def user_login():
global logged_in
user = input("Enter username: ")
password = input("Enter password: ")
C.execute("SELECT * FROM users WHERE user='{}'".format(user))
data = C.fetchone()
if data:
C.execute("SELECT password FROM users WHERE user='{}'".format(user))
pwd = C.fetchone()
if pwd[0] == password :
if not logged_in:
logged_in = True
print("Successfully logged in")
else:
print("Already logged in")
else:
print("Username/Password not correct ")
else:
print("Username/Password not correct ")
def user_logout():
global logged_in
logged_in = False
print("Successfully logged out")
def admin_panel():
while True:
print(" ############################# ")
print("Enter 1 for Adding books")
print("Enter 2 for Checking inventory")
print("Enter 3 for Updating inventory")
print("Enter 4 for deleting a book")
print("Enter 5 to exit")
print(" ############################# ")
option = int(input("Enter a option:"))
if option == 1:
add_book()
elif option == 2:
view_books()
elif option == 3:
update_book()
elif option == 4:
delete_book()
elif option == 5:
print("Exiting...")
break
else:
print("Enter a valid option")
return
def user_panel():
while True:
print(" ############################# ")
print("Enter 1 for registering")
print("Enter 2 for login")
print("Enter 3 for searching books by name ")
print("Enter 4 for searching books by genre ")
print("Enter 5 for purchase books")
print("Enter 6 for logout")
print("Enter 7 to exit")
print(" ############################# ")
ch = int(input("Enter user's choice: "))
if ch == 1:
user_register()
elif ch == 2:
user_login()
elif ch == 3:
if logged_in :
search_by_name()
else:
print("Login first")
user_login()
elif ch == 4:
if logged_in :
search_by_genre()
else:
print("Login first")
user_login()
elif ch == 5:
if logged_in:
purchase_book()
else:
print("Login first")
user_login()
elif ch == 6:
if logged_in:
user_logout()
else:
print("You are already logged out")
elif ch == 7:
print("Exiting...")
break
else:
print("Enter a valid option")
while True:
print("Enter 1 for Store Manager")
print("Enter 2 for Customer")
print("Enter 3 for Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
admin_panel()
elif choice == 2:
user_panel()
elif choice == 3:
print("Exiting...")
break
else:
print("Enter a valid option")
Database and Table creation in MySQL
Table available_books
Table users
OUTPUTS
Admin panel:
Adding a book
Checking inventory
Updating price
Deleting a book
User panel
User login
Searching books by name
Final inventory
References :
Python
https://www.python.org/
MySQL
https://www.mysql.com/
Grade 11th and 12th Computer Science
books (Sumita Arora)