import time
import pymysql.cursors
class Pet:
def __init__(self, pet_id, name, species, price, stock):
self.pet_id = pet_id
self.name = name
self.species = species
self.price = price
self.stock = stock
class Order:
def __init__(self, order_id, pet, quantity, purchase_time):
self.order_id = order_id
self.pet = pet
self.quantity = quantity
self.purchase_time = purchase_time
class Return:
def __init__(self, return_id, pet_id, reason, quantity, return_time):
self.return_id = return_id
self.pet_id = pet_id
self.reason = reason
self.quantity = quantity
self.return_time = return_time
class PetStore:
def __init__(self):
# 数据库连接
self.connection = pymysql.connect(
host="localhost",
port=3306,
user="root",
passwd="123456",
db="pet_store",
charset="utf8",
cursorclass=pymysql.cursors.DictCursor
)
self.logged_in_user = None
def register(self):
"""
注册新用户
"""
username = input("请输入用户名:")
password = input("请输入密码:")
try:
with self.connection.cursor() as cursor:
# 执行注册操作
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
cursor.execute(sql, (username, password))
self.connection.commit()
print("注册成功!")
except Exception as e:
print(f"注册失败:{e}")
def login(self):
"""
用户登录
"""
username = input("请输入用户名:")
password = input("请输入密码:")
try:
with self.connection.cursor() as cursor:
# 执行登录查询
sql = "SELECT user_id FROM users WHERE username=%s AND password=%s"
cursor.execute(sql, (username, password))
result = cursor.fetchone()
if result:
self.logged_in_user = result['user_id']
print("登录成功!")
else:
print("用户名或密码错误!")
except Exception as e:
print(f"登录失败:{e}")
def show_pet_list(self):
"""
显示宠物列表
"""
try:
with self.connection.cursor() as cursor:
# 查询宠物信息
sql = "SELECT * FROM pets"
cursor.execute(sql)
pets = cursor.fetchall()
print("宠物ID\t姓名\t种类\t价格\t库存")
for pet in pets:
print(f"{pet['pet_id']}\t{pet['name']}\t{pet['species']}\t{pet['price']}\t{pet['stock']}")
except Exception as e:
print(f"查询宠物列表失败:{e}")
def buy_pet(self):
"""
购买宠物
"""
if not self.logged_in_user:
print("请先登录!")
return
self.show_pet_list()
pet_id = int(input("输入要购买的宠物ID:"))
try:
with self.connection.cursor() as cursor:
# 查询宠物信息
sql = "SELECT * FROM pets WHERE pet_id=%s"
cursor.execute(sql, (pet_id,))
pet_info = cursor.fetchone()
if pet_info:
quantity = int(input("输入购买数量:"))
if 0 < quantity <= pet_info['stock']:
try:
# 减少库存
sql_update_stock = "UPDATE pets SET stock=stock-%s WHERE pet_id=%s"
cursor.execute(sql_update_stock, (quantity, pet_id))
# 生成订单
purchase_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
sql_insert_order = "INSERT INTO orders (pet_id, user_id, quantity, purchase_time) VALUES (%s, %s, %s, %s)"
cursor.execute(sql_insert_order, (pet_id, self.logged_in_user, quantity, purchase_time))
self.connection.commit()
print(f"您成功购买了{quantity}只{pet_info['species']}!")
except Exception as e:
print(f"购买失败:{e}")
else:
print("无效的数量或库存不足。")
else:
print("无效的宠物ID。")
except Exception as e:
print(f"购买宠物失败:{e}")
def show_order_list(self):
"""
显示订单列表
"""
if not self.logged_in_user:
print("请先登录!")
return
try:
with self.connection.cursor() as cursor:
# 查询订单信息
sql = "SELECT orders.order_id, pets.species, pets.name, orders.quantity, orders.purchase_time FROM orders JOIN pets ON orders.pet_id = pets.pet_id WHERE orders.user_id=%s"
cursor.execute(sql, (self.logged_in_user,))
orders = cursor.fetchall()
print("订单ID\t宠物\t数量\t购买时间")
for order in orders:
print(
f"{order['order_id']}\t{order['species']}({order['name']})\t{order['quantity']}\t{order['purchase_time']}")
except Exception as e:
print(f"查询订单列表失败:{e}")
def show_user_list(self):
"""
显示用户列表
"""
try:
with self.connection.cursor() as cursor:
# 查询用户信息
sql = "SELECT * FROM users"
cursor.execute(sql)
users = cursor.fetchall()
print("用户ID\t用户名")
for user in users:
print(f"{user['user_id']}\t{user['username']}")
except Exception as e:
print(f"查询用户列表失败:{e}")
def close_connection(self):
"""
关闭数据库连接
"""
self.connection.close()
print("数据库连接已关闭。")
# 主程序
if __name__ == "__main__":
pet_store = PetStore()
while True:
print("1. 注册")
print("2. 登录")
print("3. 显示宠物列表")
print("4. 购买宠物")
print("5. 显示订单列表")
print("6. 查询用户表")
print("7. 退出")
choice = input("请选择操作:")
if choice == "1":
pet_store.register()
elif choice == "2":
pet_store.login()
elif choice == "3":
pet_store.show_pet_list()
elif choice == "4":
pet_store.buy_pet()
elif choice == "5":
pet_store.show_order_list()
elif choice == "6":
pet_store.show_user_list() # Assuming you have a method for querying the user table
elif choice == "7":
pet_store.close_connection()
print("退出宠物商店。再见!")
break
else:
print("无效的选择。请重试。")
没有合适的资源?快使用搜索试试~ 我知道了~
Python宠物商店管理系统 课程设计
共4个文件
zip:1个
sql:1个
py:1个
需积分: 1 1 下载量 192 浏览量
2024-01-06
21:34:27
上传
评论 2
收藏 132KB ZIP 举报
温馨提示
内容概要: 这段Python代码实现了一个宠物商店管理系统,可以进行用户注册、登录、查看宠物列表、购买宠物、查看订单列表和查询用户表等操作。系统使用MySQL数据库进行数据存储和管理,通过命令行界面与用户交互。用户可以通过选择不同的操作来管理宠物商店的宠物和订单信息。
资源推荐
资源详情
资源评论
收起资源包目录
027.Python宠物管理系统.zip (4个子文件)
作业.zip 5KB
task35
main.py 7KB
C2_{M)I7`S3@JU%CVZJV~SM.jpg 128KB
pet_store.sql 3KB
共 4 条
- 1
资源评论
海洋之心
- 粉丝: 9w+
- 资源: 109
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- multisim简易密码锁设计秒表电路抢答器简易测频仪multisim数字电路仿真电路合集(4个).zip
- 基于yolov10实现5类水果蔬菜检测源码+数据集+模型.zip
- 第一章+数学基础.pdf
- 安卓与STM32硬件开发项目,实现安卓端控制家庭灯,窗帘,门.zip
- java-leetcode题解之第972题最接近原点的K个点.zip
- java-leetcode题解之第347题前K个高频元素.zip
- java-leetcode题解之第215题数组中的第K个最大元素.zip
- java-leetcode题解之第641题设计循环双端队列.zip
- java-leetcode题解之第1284题转化为全零矩阵的最少反转次数.zip
- java-leetcode题解之第1311获取已观看视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功