finale bvm
finale bvm
M PUBLIC SCHOOL
2023-2024
1
B.V.M PUBLIC
----SCHOOL-----
Examiner signature
2
Aknowledgement
3
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root@localhost',
password='pragya',
database='customer'
)
mycursor = mydb.cursor()
def select(table_name):
query = f"SELECT * FROM {table_name}"
mycursor.execute(query)
return mycursor.fetchall()
4
print(value, end=" ")
print()
def menu1():
print("\n\nTo display list of all customers press 1:")
print("To display list of all items press 2:")
print("For customer-related query press 3:")
print("For items-related query press 4:")
print("To buy an item press 5:")
print("To exit press 6:")
a = get_user_input([1, 2, 3, 4, 5, 6])
return a
def menu2():
print("To display info of a particular item press 1:")
print("To update info of a particular item press 2:")
print("To enter a new item press 3:")
print("To delete a particular item press 4:")
print("To get items of a particular type press 5:")
print("To get items in sorted order press 6:")
print("To update the quantity of an item press 7:")
print("To go back to the main menu press 8:")
g = get_user_input([1, 2, 3, 4, 5, 6, 7, 8])
return g
def menu3():
print("To display info of a particular customer press 1:")
print("To update info of a particular customer press 2:")
print("To enter a new Customer press 3:")
5
print("To delete a particular Customer press 4:")
print("To go back to the main menu press 5:")
h = get_user_input([1, 2, 3, 4, 5])
return h
def menu4():
print("To get items in increasing order of price press 1:")
print("To get items in alphabetical order of name press 2:")
print("To get items in increasing order of quantity press
3:")
print("To go back to the main menu press 4:")
i = get_user_input([1, 2, 3, 4])
if i == 1:
data = select_items_by_price()
message(data, "item")
elif i == 2:
data = select_items_by_name()
message(data, "item")
elif i == 3:
data = select_items_by_quantity()
message(data, "item")
return i
def get_user_input(valid_choices):
while True:
try:
user_input = int(input("Enter your choice: "))
if user_input in valid_choices:
6
return user_input
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter a number.")
def dis_c():
data = select("customerinformation")
message(data, "customerinformation")
def dis_p():
data = select("item")
message(data, "item")
while True:
g = menu2()
if g == 1:
pid = int(input("Enter pid of item: "))
data = select_item_by_pid(pid)
message(data, "item")
elif g == 2:
pid = int(input("Enter pid of item: "))
update_item_info(pid)
elif g == 3:
enter_new_item()
elif g == 4:
delete_item(int(input("Enter pid of item to
delete:")))
elif g == 5:
7
item_type = input("Enter the type of item: ")
data = select_items_by_type(item_type)
message(data, "item")
elif g == 6:
data = select_items_in_sorted_order()
message(data, "item")
elif g == 7:
pid = int(input("Enter pid of item: "))
new_quantity = int(input("Enter the new quantity: "))
update_item_quantity(pid, new_quantity)
elif g == 8:
break
def select_item_by_pid(pid):
query = "SELECT * FROM item WHERE pid = %s"
mycursor.execute(query, (pid,))
return mycursor.fetchall()
def update_customer_info(cid):
current_data = select_customer_by_cid(cid)
if not current_data:
print(f"Customer with CID {cid} not found.")
return
print("Current Customer Information:")
message(current_data, "customerinformation")
new_name = input("Enter the new name: ")
new_age = int(input("Enter the new age: "))
new_address = input("Enter the new address: ")
8
update_query = "UPDATE customerinformation SET Name = %s, Age
= %s, Address = %s WHERE cid = %s"
mycursor.execute(update_query, (new_name, new_age,
new_address, cid))
mydb.commit()
updated_data = select_customer_by_cid(cid)
print("\nUpdated Customer Information:")
message(updated_data, "customerinformation")
print(f"Customer with CID {cid} updated successfully.")
def enter_new_customer():
print("Enter details for the new customer:")
new_cid = int(input("Enter the new CID: "))
new_name = input("Enter the name: ")
new_age = int(input("Enter the age: "))
new_address = input("Enter the address: ")
insert_query = "INSERT INTO customerinformation (cid, Name,
Age, Address) VALUES (%s, %s, %s, %s)"
mycursor.execute(insert_query, (new_cid, new_name, new_age,
new_address))
mydb.commit()
print("New customer added successfully!")
def delete_customer(cid):
delete_query = "DELETE FROM customerinformation WHERE cid =
%s"
mycursor.execute(delete_query, (cid,))
mydb.commit()
print(f"Customer with CID {cid} deleted successfully.")
9
def select_customer_by_cid(cid):
query = "SELECT * FROM customerinformation WHERE cid = %s"
mycursor.execute(query, (cid,))
return mycursor.fetchall()
def update_item_info(pid):
current_data = select_item_by_pid(pid)
if not current_data:
print(f"Item with PID {pid} not found.")
return
print("Current Item Information:")
message(current_data, "item")
new_name = input("Enter the new name: ")
new_quantity = int(input("Enter the new quantity: "))
new_price = float(input("Enter the new price: "))
new_type = input("Enter the new type: ")
update_query = "UPDATE item SET Name = %s, Quantity = %s,
Price = %s, Type = %s WHERE pid = %s"
mycursor.execute(update_query, (new_name, new_quantity,
new_price, new_type, pid))
mydb.commit()
updated_data = select_item_by_pid(pid)
print("\nUpdated Item Information:")
message(updated_data, "item")
print(f"Item with PID {pid} updated successfully.")
10
print(f"Item with PID {pid} not found.")
return
print("Current Item Information:")
message(current_data, "item")
update_query = "UPDATE item SET Quantity = %s WHERE pid = %s"
mycursor.execute(update_query, (new_quantity, pid))
mydb.commit()
updated_data = select_item_by_pid(pid)
print("\nUpdated Item Information:")
message(updated_data, "item")
print(f"Quantity of Item with PID {pid} updated
successfully.")
def enter_new_item():
print("Enter details for the new item:")
new_pid = int(input("Enter the new PID: "))
new_name = input("Enter the name: ")
new_quantity = int(input("Enter the quantity: "))
new_price = float(input("Enter the price: "))
new_type = input("Enter the type: ")
insert_query = "INSERT INTO item (pid, Name, Quantity, Price,
Type) VALUES (%s, %s, %s, %s, %s)"
mycursor.execute(insert_query, (new_pid, new_name,
new_quantity, new_price, new_type))
mydb.commit()
print("New item added successfully!")
def delete_item(pid):
delete_query = "DELETE FROM item WHERE pid = %s"
mycursor.execute(delete_query, (pid,))
11
mydb.commit()
print(f"Item with PID {pid} deleted successfully.")
def select_items_by_type(item_type):
query = "SELECT * FROM item WHERE Type = %s"
mycursor.execute(query, (item_type,))
return mycursor.fetchall()
def select_items_in_sorted_order():
query = "SELECT * FROM item ORDER BY Name ASC"
mycursor.execute(query)
return mycursor.fetchall()
def select_items_by_price():
query = "SELECT * FROM item ORDER BY Price ASC"
mycursor.execute(query)
return mycursor.fetchall()
def select_items_by_name():
query = "SELECT * FROM item ORDER BY Name ASC"
mycursor.execute(query)
return mycursor.fetchall()
def select_items_by_quantity():
query = "SELECT * FROM item ORDER BY Quantity ASC"
mycursor.execute(query)
return mycursor.fetchall()
def buy_item():
12
pid = int(input("Enter the PID of the item you want to buy:
"))
quantity = int(input("Enter the quantity you want to buy: "))
cid = int(input("Enter your customer ID (CID): "))
item_data = select_item_by_pid(pid)
customer_data = select_customer_by_cid(cid)
if not item_data:
print(f"Item with PID {pid} not found.")
elif not customer_data:
print(f"Customer with CID {cid} not found.")
elif item_data[0][2] < quantity:
print(f"Insufficient quantity available for {item_data[0]
[1]}")
else:
total_price = quantity * item_data[0][3]
print("Purchase successful!")
print(f"Customer Name: {customer_data[0][1]}")
print(f"Customer Age: {customer_data[0][2]}")
print(f"Customer Address: {customer_data[0][3]}")
print(f"Item Name: {item_data[0][1]}")
print(f"Item Price: {item_data[0][3]}")
print(f"Quantity: {quantity}")
print(f"Total Price: {total_price}")
while True:
choice = menu1()
if choice == 1:
dis_c()
13
elif choice == 2:
dis_p()
elif choice == 3:
while True:
h = menu3()
if h == 1:
cid = int(input("Enter cid of customer: "))
data = select_customer_by_cid(cid)
message(data, "customerinformation")
elif h == 2:
cid = int(input("Enter cid of customer: "))
update_customer_info(cid)
elif h == 3:
enter_new_customer()
elif h == 4:
cid = int(input("Enter cid of customer: "))
delete_customer(cid)
elif h == 5:
break
elif choice == 4:
while True:
i = menu4()
if i == 4:
break
elif choice == 5:
buy_item()
mydb.close()
OUTPUT:-
14
15
16