利用python编写一个水果店信息管理系统,要求能够登录,能够记录各种水果的入库、库存信息,查询、修改、增加、删除水果的相关数据,以及水果保质期的提醒

时间: 2024-01-12 11:04:31 浏览: 26
好的,这是一个基于Python和SQLite的简单水果店信息管理系统,包括登录、水果信息管理、库存管理和保质期提醒功能。请先确保已经安装了最新版本的Python和SQLite。 首先,创建一个名为“fruit_shop.db”的SQLite数据库,其中包括两个表格:“users”和“fruits”: ```sql CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, password TEXT NOT NULL ); CREATE TABLE fruits ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, price REAL NOT NULL, quantity INTEGER NOT NULL, shelf_life INTEGER NOT NULL, expiration_date TEXT NOT NULL ); ``` 然后,编写Python代码实现水果店信息管理系统的各项功能。以下是完整代码: ```python import sqlite3 import getpass import datetime # 数据库连接和游标 conn = sqlite3.connect('fruit_shop.db') cursor = conn.cursor() # 登录功能 def login(): print('*** 登录 ***') username = input('请输入用户名:') password = getpass.getpass('请输入密码:') cursor.execute('SELECT * FROM users WHERE username=? AND password=?', (username, password)) user = cursor.fetchone() if user: print('登录成功!') return user[0] else: print('用户名或密码错误!') return None # 注册功能 def register(): print('*** 注册 ***') username = input('请输入用户名:') password1 = getpass.getpass('请输入密码:') password2 = getpass.getpass('请再次输入密码:') if password1 != password2: print('两次输入的密码不一致!') return False cursor.execute('SELECT * FROM users WHERE username=?', (username,)) user = cursor.fetchone() if user: print('该用户名已存在!') return False cursor.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, password1)) conn.commit() print('注册成功!') return True # 显示水果列表 def show_fruit_list(): print('*** 水果列表 ***') cursor.execute('SELECT * FROM fruits') fruits = cursor.fetchall() if fruits: print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)')) for fruit in fruits: print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4])) else: print('暂无水果信息!') # 添加水果信息 def add_fruit(): print('*** 添加水果信息 ***') name = input('请输入水果名称:') price = float(input('请输入水果价格(元):')) quantity = int(input('请输入水果库存数量:')) shelf_life = int(input('请输入水果保质期(天):')) expiration_date = (datetime.date.today() + datetime.timedelta(days=shelf_life)).strftime('%Y-%m-%d') cursor.execute('INSERT INTO fruits (name, price, quantity, shelf_life, expiration_date) VALUES (?, ?, ?, ?, ?)', (name, price, quantity, shelf_life, expiration_date)) conn.commit() print('添加成功!') # 查询水果信息 def search_fruit(): print('*** 查询水果信息 ***') keyword = input('请输入水果名称或ID:') cursor.execute('SELECT * FROM fruits WHERE name=? OR id=?', (keyword, keyword)) fruit = cursor.fetchone() if fruit: print('查询结果:') print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)')) print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4])) else: print('无匹配结果!') # 修改水果信息 def update_fruit(): print('*** 修改水果信息 ***') keyword = input('请输入要修改的水果名称或ID:') cursor.execute('SELECT * FROM fruits WHERE name=? OR id=?', (keyword, keyword)) fruit = cursor.fetchone() if fruit: print('原信息:') print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)')) print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4])) name = input('请输入新的水果名称(留空表示不修改):') price = input('请输入新的水果价格(留空表示不修改):') quantity = input('请输入新的水果库存数量(留空表示不修改):') shelf_life = input('请输入新的水果保质期(留空表示不修改):') if name: cursor.execute('UPDATE fruits SET name=? WHERE id=?', (name, fruit[0])) if price: cursor.execute('UPDATE fruits SET price=? WHERE id=?', (price, fruit[0])) if quantity: cursor.execute('UPDATE fruits SET quantity=? WHERE id=?', (quantity, fruit[0])) if shelf_life: cursor.execute('UPDATE fruits SET shelf_life=?, expiration_date=? WHERE id=?', (shelf_life, (datetime.date.today() + datetime.timedelta(days=int(shelf_life))).strftime('%Y-%m-%d'), fruit[0])) conn.commit() print('修改成功!') else: print('无匹配结果!') # 删除水果信息 def delete_fruit(): print('*** 删除水果信息 ***') keyword = input('请输入要删除的水果名称或ID:') cursor.execute('SELECT * FROM fruits WHERE name=? OR id=?', (keyword, keyword)) fruit = cursor.fetchone() if fruit: print('删除信息:') print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)')) print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4])) confirm = input('是否确认删除?(Y/N):') if confirm.lower() == 'y': cursor.execute('DELETE FROM fruits WHERE id=?', (fruit[0],)) conn.commit() print('删除成功!') else: print('无匹配结果!') # 库存管理 def inventory_management(): print('*** 库存管理 ***') cursor.execute('SELECT * FROM fruits') fruits = cursor.fetchall() if fruits: print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)')) for fruit in fruits: print('{:<10}{:<10}{:<10}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4])) fruit_id = int(input('请输入要更新库存的水果ID:')) cursor.execute('SELECT * FROM fruits WHERE id=?', (fruit_id,)) fruit = cursor.fetchone() if fruit: quantity = int(input('请输入要更新的库存数量:')) if fruit[3] + quantity >= 0: cursor.execute('UPDATE fruits SET quantity=? WHERE id=?', (fruit[3] + quantity, fruit[0])) conn.commit() print('库存更新成功!') else: print('库存不足,无法更新!') else: print('无匹配结果!') else: print('暂无水果信息!') # 保质期提醒 def expiration_reminder(): print('*** 保质期提醒 ***') cursor.execute('SELECT * FROM fruits') fruits = cursor.fetchall() if fruits: today = datetime.date.today() print('{:<10}{:<10}{:<10}{:<15}{:<15}{:<15}'.format('ID', '名称', '价格', '库存数量', '保质期(天)', '到期日期')) for fruit in fruits: expiration_date = datetime.datetime.strptime(fruit[5], '%Y-%m-%d').date() days_left = (expiration_date - today).days if days_left <= 3: print('{:<10}{:<10}{:<10}{:<15}{:<15}{:<15}'.format(fruit[0], fruit[1], fruit[2], fruit[3], fruit[4], fruit[5])) print('该水果剩余保质期不足3天,请及时处理!') else: print('暂无水果信息!') # 主程序 def main(): print('欢迎使用水果店信息管理系统!') user_id = None while not user_id: choice = input('请选择功能:\n1. 登录\n2. 注册\n') if choice == '1': user_id = login() elif choice == '2': register() while True: print('请选择功能:\n1. 显示水果列表\n2. 添加水果信息\n3. 查询水果信息\n4. 修改水果信息\n5. 删除水果信息\n6. 库存管理\n7. 保质期提醒\n8. 退出') choice = input() if choice == '1': show_fruit_list() elif choice == '2': add_fruit() elif choice == '3': search_fruit() elif choice == '4': update_fruit() elif choice == '5': delete_fruit() elif choice == '6': inventory_management() elif choice == '7': expiration_reminder() elif choice == '8': break else: print('输入错误,请重新选择!') conn.close() print('谢谢使用水果店信息管理系统!') if __name__ == '__main__': main() ``` 运行程序后,根据提示进行登录或注册,然后选择需要的功能进行操作即可。其中,“保质期提醒”功能会列出所有剩余保质期不足3天的水果,并提醒用户及时处理。

相关推荐

最新推荐

recommend-type

python学生信息管理系统实现代码

主要介绍了python学生信息管理系统的实现代码,代码简单,复制即可使用,需要的朋友可以参考下
recommend-type

python+mysql实现学生信息查询系统

主要为大家详细介绍了python+mysql实现学生信息查询系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python实现外卖信息管理系统

主要为大家详细介绍了python实现外卖信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Python实现GUI学生信息管理系统

主要介绍了Python实现GUI学生信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Python实现的查询mysql数据库并通过邮件发送信息功能

主要介绍了Python实现的查询mysql数据库并通过邮件发送信息功能,可实现Python针对mysql的查询与宕机信息通过邮件发送的相关操作技巧,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

用Spring boot和vue写一个登录注册界面

好的,这是一个比较常见的技术问题,我可以为您提供一些参考资料和思路。首先,您可以使用Spring Boot作为后端框架来处理用户认证和注册的逻辑,比如使用Spring Security实现用户登录认证。同时,您还需要设计相应的数据模型和数据库表结构来存储用户信息。在前端方面,您可以使用Vue.js作为框架来构建登录注册页面,使用Axios来发起API请求并和后端进行交互。当然,在实现过程中,还需要考虑一些具体细节,比如数据校验、安全性和用户体验等方面。希望这些信息能够帮助到您。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。