超市会员管理系统代码
设计一个完整的超市会员管理系统需要考虑多个方面,包括会员注册、积分管理、优惠活动、购物记录等功能。由于系统的复杂性,下面是一个简化的示例,使用Python语言和SQLite数据库。
pythonimport sqlite3
from datetime import datetime
# 连接数据库
conn = sqlite3.connect('supermarket.db')
cursor = conn.cursor()
# 创建会员表
cursor.execute('''
CREATE TABLE IF NOT EXISTS members (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
points INTEGER DEFAULT 0
)
''')
conn.commit()
# 创建购物记录表
cursor.execute('''
CREATE TABLE IF NOT EXISTS purchases (
id INTEGER PRIMARY KEY AUTOINCREMENT,
member_id INTEGER,
amount REAL NOT NULL,
purchase_date TEXT NOT NULL,
FOREIGN KEY (member_id) REFERENCES members(id)
)
''')
conn.commit()
def register_member(name, email):
cursor.execute('INSERT INTO members (name, email) VALUES (?, ?)', (name, email))
conn.commit()
print(f'会员 {name} 注册成功!')
def make_purchase(member_id, amount):
purchase_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('INSERT INTO purchases (member_id, amount, purchase_date) VALUES (?, ?, ?)',
(member_id, amount, purchase_date))
conn.commit()
update_points(member_id, int(amount))
def update_points(member_id, points):
cursor.execute('UPDATE members SET points = points + ? WHERE id = ?', (points, member_id))
conn.commit()
def view_member_points(email):
cursor.execute('SELECT points FROM members WHERE email = ?', (email,))
result = cursor.fetchone()
if result:
return result[0]
else:
return None
# 示例用法
register_member('张三', 'zhangsan@example.com')
register_member('李四', 'lisi@example.com')
make_purchase(1, 100.0)
make_purchase(2, 150.0)
print(f'张三的积分: {view_member_points("zhangsan@example.com")}')
print(f'李四的积分: {view_member_points("lisi@example.com")}')
# 关闭数据库连接
conn.close()
当设计一个完整的超市会员管理系统时,你可能还希望包括其他功能,例如会员等级制度、优惠活动、交易历史记录等。
python# 在 members 表中添加等级字段
cursor.execute('''
ALTER TABLE members
ADD COLUMN level INTEGER DEFAULT 1
''')
conn.commit()
# 在 members 表中添加注册日期字段
cursor.execute('''
ALTER TABLE members
ADD COLUMN registration_date TEXT
''')
conn.commit()
# 在 purchases 表中添加商品信息字段
cursor.execute('''
ALTER TABLE purchases
ADD COLUMN products TEXT
''')
conn.commit()
def register_member(name, email):
registration_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('INSERT INTO members (name, email, registration_date) VALUES (?, ?, ?)',
(name, email, registration_date))
conn.commit()
print(f'会员 {name} 注册成功!')
def make_purchase(member_id, amount, products):
purchase_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('INSERT INTO purchases (member_id, amount, purchase_date, products) VALUES (?, ?, ?, ?)',
(member_id, amount, purchase_date, products))
conn.commit()
update_points(member_id, int(amount))
def update_level(member_id, level):
cursor.execute('UPDATE members SET level = ? WHERE id = ?', (level, member_id))
conn.commit()
def view_transaction_history(member_id):
cursor.execute('SELECT * FROM purchases WHERE member_id = ?', (member_id,))
transactions = cursor.fetchall()
return transactions
# 示例用法
register_member('王五', 'wangwu@example.com')
make_purchase(3, 200.0, '商品A, 商品B')
print(f'王五的交易历史: {view_transaction_history(3)}')
# 关闭数据库连接
conn.close()
这个示例中,我们添加了会员等级和注册日期字段。还在购物记录表中添加了商品信息字段。在实际应用中,你可能还需要更多的字段和复杂的逻辑,具体取决于项目的需求。