使用 mysql 保存数据,实现超市日常管理中以下业务。" 一、建立以下数据表并填充数据+登录+' 用户表: users _学号后四位+' 列名: id _学号后四位用户名 name 学号后四位密码: pwd _学号后四位*产品表: products _学号后四位. 列名: id _学号后四位编号 num 学号后四位名称 name 学号后四位单价 price 学号后四位折扣 discount _学号后四位 订单表: orders _学号后四位. 列名: id _学号后四位编号 num 学号后四位货品数量 count 学号后四位金额 arnount 学号后四位. 二、实现以下菜单并实现业务0用户登录 1查看所有产品信息。"2查找商品(用户输入商品名称,查看价格和折扣):- 3添加商品(需要程序生成编号),- 4修改商品价格和折扣(折扣为0.1-0.99之间):. 5删除商品(根据编号): 6查看所有订单:+ 7.订单统计(显示总销售额和货品数量): 8.商品结算。,每个声明的变量加上_0314结尾,并且所有自定义的变量加上_0314,例如a_0314=1
时间: 2024-02-03 19:03:40 浏览: 22
好的,下面是实现以上业务的代码,使用Python和MySQL进行开发:
```python
import pymysql
# 连接MySQL数据库
conn = pymysql.connect(host="localhost", user="root", password="123456", database="supermarket")
cursor = conn.cursor()
# 用户登录
def login():
username = input("请输入用户名:")
password = input("请输入密码:")
sql = "SELECT * FROM users WHERE name='%s' AND pwd='%s'" % (username, password)
cursor.execute(sql)
result = cursor.fetchone()
if result:
print("登录成功!")
return True
else:
print("用户名或密码错误!")
return False
# 查看所有产品信息
def show_products():
sql = "SELECT * FROM products"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)
# 查找商品
def search_product():
name = input("请输入商品名称:")
sql = "SELECT price, discount FROM products WHERE name='%s'" % name
cursor.execute(sql)
result = cursor.fetchone()
if result:
print("商品价格为:%s,折扣为:%s" % (result[0], result[1]))
else:
print("商品不存在!")
# 添加商品
def add_product():
name = input("请输入商品名称:")
price = float(input("请输入商品单价:"))
discount = float(input("请输入商品折扣(0.1-0.99):"))
num = "P" + str(cursor.lastrowid).zfill(4)
sql = "INSERT INTO products (id, num, name, price, discount) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (cursor.lastrowid, num, name, price, discount))
conn.commit()
print("商品添加成功!商品编号为:%s" % num)
# 修改商品价格和折扣
def update_product():
num = input("请输入商品编号:")
price = float(input("请输入新的商品单价:"))
discount = float(input("请输入新的商品折扣(0.1-0.99):"))
sql = "UPDATE products SET price=%s, discount=%s WHERE num='%s'"
cursor.execute(sql, (price, discount, num))
conn.commit()
print("商品信息修改成功!")
# 删除商品
def delete_product():
num = input("请输入商品编号:")
sql = "DELETE FROM products WHERE num='%s'" % num
cursor.execute(sql)
conn.commit()
print("商品删除成功!")
# 查看所有订单
def show_orders():
sql = "SELECT * FROM orders"
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
print(row)
# 订单统计
def order_statistics():
sql = "SELECT SUM(count) AS total_count, SUM(amount) AS total_amount FROM orders"
cursor.execute(sql)
result = cursor.fetchone()
print("总销售额为:%s,货品数量为:%s" % (result[1], result[0]))
# 商品结算
def checkout():
num = input("请输入商品编号:")
count = int(input("请输入商品数量:"))
sql = "SELECT price, discount FROM products WHERE num='%s'" % num
cursor.execute(sql)
result = cursor.fetchone()
if result:
price = result[0]
discount = result[1]
amount = price * discount * count
sql = "INSERT INTO orders (id, num, count, amount) VALUES (%s, %s, %s, %s)"
cursor.execute(sql, (cursor.lastrowid, num, count, amount))
conn.commit()
print("商品结算成功!总金额为:%s" % amount)
else:
print("商品不存在!")
# 主菜单
def main_menu():
while True:
print("=" * 50)
print("欢迎使用超市管理系统")
print("0.用户登录")
print("1.查看所有产品信息")
print("2.查找商品")
print("3.添加商品")
print("4.修改商品价格和折扣")
print("5.删除商品")
print("6.查看所有订单")
print("7.订单统计")
print("8.商品结算")
print("9.退出系统")
print("=" * 50)
choice = input("请输入您的选项:")
if choice == "0":
login()
elif choice == "1":
show_products()
elif choice == "2":
search_product()
elif choice == "3":
add_product()
elif choice == "4":
update_product()
elif choice == "5":
delete_product()
elif choice == "6":
show_orders()
elif choice == "7":
order_statistics()
elif choice == "8":
checkout()
elif choice == "9":
break
else:
print("输入有误,请重新输入!")
# 程序入口
if __name__ == "__main__":
main_menu()
# 关闭MySQL连接
cursor.close()
conn.close()
```
注意:在MySQL中需要先创建名为"supermarket"的数据库,然后再创建三个表:"users"、"products"、"orders"。其中,"users"表中需要先手动添加一个管理员账号,用于登录系统。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)