用python语言编写一个物品租赁管理系统
时间: 2023-07-18 15:09:33 浏览: 103
Python版自行车租赁系统源代码,含详细项目设计报告,基于Django+MySQL
5星 · 资源好评率100%
好的,我可以为您提供一个Python语言编写的物品租赁管理系统的示例代码,以下是一个基本的模板:
```python
import sqlite3
import datetime
conn = sqlite3.connect('items.db')
c = conn.cursor()
# 创建用户表
def create_user_table():
c.execute('''CREATE TABLE IF NOT EXISTS user
(id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL)''')
conn.commit()
# 创建物品表
def create_item_table():
c.execute('''CREATE TABLE IF NOT EXISTS item
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT NOT NULL,
is_rented INTEGER)''')
conn.commit()
# 创建租赁表
def create_rental_table():
c.execute('''CREATE TABLE IF NOT EXISTS rental
(id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
rental_time TEXT NOT NULL,
return_time TEXT,
FOREIGN KEY (item_id) REFERENCES item(id),
FOREIGN KEY (user_id) REFERENCES user(id))''')
conn.commit()
# 用户注册
def register():
username = input('请输入用户名:')
password = input('请输入密码:')
c.execute("INSERT INTO user (username, password) VALUES (?, ?)", (username, password))
conn.commit()
print('注册成功!')
# 用户登录
def login():
username = input('请输入用户名:')
password = input('请输入密码:')
c.execute("SELECT id FROM user WHERE username = ? AND password = ?", (username, password))
user_id = c.fetchone()
if user_id:
print('登录成功!')
return user_id[0]
else:
print('用户名或密码错误!')
return None
# 添加物品
def add_item():
name = input('请输入物品名称:')
description = input('请输入物品描述:')
c.execute("INSERT INTO item (name, description, is_rented) VALUES (?, ?, ?)", (name, description, 0))
conn.commit()
print('添加成功!')
# 查询物品
def query_item():
name = input('请输入要查询的物品名称:')
c.execute("SELECT id, name, description, is_rented FROM item WHERE name = ?", (name,))
item = c.fetchone()
if item:
print('物品ID:', item[0])
print('物品名称:', item[1])
print('物品描述:', item[2])
if item[3] == 0:
print('可租借')
else:
print('已租借')
else:
print('未查询到该物品!')
# 租借物品
def rent_item(user_id):
item_id = input('请输入要租借的物品ID:')
c.execute("SELECT is_rented FROM item WHERE id = ?", (item_id,))
is_rented = c.fetchone()
if is_rented and is_rented[0] == 0:
rental_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
c.execute("UPDATE item SET is_rented = 1 WHERE id = ?", (item_id,))
c.execute("INSERT INTO rental (item_id, user_id, rental_time) VALUES (?, ?, ?)", (item_id, user_id, rental_time))
conn.commit()
print('租借成功!')
else:
print('该物品已被租借或不存在!')
# 归还物品
def return_item():
rental_id = input('请输入租赁记录ID:')
return_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
c.execute("UPDATE rental SET return_time = ? WHERE id = ?", (return_time, rental_id))
c.execute("SELECT item_id FROM rental WHERE id = ?", (rental_id,))
item_id = c.fetchone()
if item_id:
c.execute("UPDATE item SET is_rented = 0 WHERE id = ?", (item_id[0],))
conn.commit()
print('归还成功!')
else:
print('未查询到该租赁记录!')
# 程序入口
if __name__ == '__main__':
create_user_table()
create_item_table()
create_rental_table()
while True:
print('1. 注册')
print('2. 登录')
print('3. 添加物品')
print('4. 查询物品')
print('5. 租借物品')
print('6. 归还物品')
print('7. 退出')
choice = input('请输入操作编号:')
if choice == '1':
register()
elif choice == '2':
user_id = login()
if user_id:
while True:
print('1. 添加物品')
print('2. 查询物品')
print('3. 租借物品')
print('4. 归还物品')
print('5. 返回上级菜单')
choice = input('请输入操作编号:')
if choice == '1':
add_item()
elif choice == '2':
query_item()
elif choice == '3':
rent_item(user_id)
elif choice == '4':
return_item()
elif choice == '5':
break
elif choice == '3':
add_item()
elif choice == '4':
query_item()
elif choice == '5':
print('请先登录!')
elif choice == '6':
print('请先登录!')
elif choice == '7':
break
else:
print('无效操作,请重新输入!')
conn.close()
```
以上代码仅供参考,具体的实现需要根据您的需求进行修改和完善。
阅读全文