题目49学生宿舍水电费信息管理
学生宿舍水电费信息管理
设计程序实现水电费管理系统功能。
设计要求:
(1) 记录每个宿舍的名称、编号、用水量、用电量、水电价格、宿舍学生等信息;
(2) 能够对系统中各宿舍用水,电等信息进行统计;
(3) 对宿舍预存水、电设置最低数量值.当系统中预存值数量低于设定值时报警提醒;
(4) 能够对宿舍水电购买信息进行登记管理功能;
(5) 能够汇总某一段时间内的水电使用情况.统计相关信息。
使用python实现:
import tkinter as tk from tkinter import messagebox import json from tkcalendar import DateEntry import datetime from pylab import * mpl.rcParams['font.sans-serif'] = ['SimHei']#图中的字体 # 水电费管理系统类 class DormitoryUtilitySystem: def __init__(self): self.dormitories = [] # 存储宿舍信息的列表 # 添加宿舍信息 def add_dormitory(self, name, number, water_usage, electricity_usage, water_price, electricity_price, students): dormitory = { #分别为宿舍名,宿舍号,用水量,用电量,水费,电费,学生,水,电设定值,用水剩余量,用电剩余量 'name': name, 'number': number, 'water_usage': water_usage, 'electricity_usage': electricity_usage, 'water_price': water_price, 'electricity_price': electricity_price, 'students': students, 'minimum_water_quantity':0, 'minimum_electricity_quantity':0, 'w':100-water_usage, 'e':100-electricity_usage } self.dormitories.append(dormitory) # 统计系统中各宿舍用水和用电信息 def get_dormitory_statistics(self): statistics = [] for dormitory in self.dormitories: dormitory_info = { '宿舍名': dormitory['name'], '用水量': dormitory['water_usage'], '用电量': dormitory['electricity_usage'] } statistics.append(dormitory_info) return statistics # 显示宿舍剩余水量和电量 def get_dormitory_shop(self): statisticss = [] for dormitory in self.dormitories: dormitory_info = { '宿舍名': dormitory['name'], '剩余水量': dormitory['w'], '剩余电量': dormitory['e'] } statisticss.append(dormitory_info) return statisticss # 设置宿舍预存水电量的最低值,并检查是否低于设定值 def set_minimum_quantity(self, dormitory_name, minimum_water_quantity, minimum_electricity_quantity): for dormitory in self.dormitories: if dormitory['name'] == dormitory_name: dormitory['minimum_water_quantity'] = minimum_water_quantity dormitory['minimum_electricity_quantity'] = minimum_electricity_quantity if int(dormitory['w'] < minimum_water_quantity) & int(dormitory['e'] < minimum_electricity_quantity): print(f"宿舍 {dormitory_name} 的水量电量均低于设定值!") messagebox.showinfo("提示",f"宿舍 {dormitory_name} 的水量低于设定值!,宿舍 {dormitory_name} 的电量低于设定值!") else: if dormitory['w'] < minimum_water_quantity: print(f"宿舍 {dormitory_name} 的水量低于设定值!") messagebox.showinfo("提示", f"宿舍 {dormitory_name} 的水量低于设定值!") if dormitory['e'] < minimum_electricity_quantity: print(f"宿舍 {dormitory_name} 的电量低于设定值!") messagebox.showinfo("提示", f"宿舍 {dormitory_name} 的电量低于设定值!") break # 登记宿舍水电购买信息 def add_purchase_record(self, dormitory_name, water_quantity, electricity_quantity, purchase_date): for dormitory in self.dormitories: if dormitory['name'] == dormitory_name: #水电购买信息 purchase_record = { 'water_quantity': water_quantity, 'electricity_quantity': electricity_quantity, 'purchase_date': purchase_date } dormitory['w'] += water_quantity dormitory['e'] += electricity_quantity sums = electricity_quantity * dormitory['electricity_price']+water_quantity * dormitory['water_price'] if 'purchase_records' not in dormitory: dormitory['purchase_records'] = [] dormitory['purchase_records'].append(purchase_record) messagebox.showinfo("提示", f"已为宿舍{dormitory_name}充值水量{water_quantity},充值电量{electricity_quantity} ,总共花费{sums}元") break # 登记宿舍用电用水当天 def add_purchase_use(self, dormitory_name, water_quantity, electricity_quantity, purchase_date): for dormitory in self.dormitories: if dormitory['name'] == dormitory_name: #水电使用登记 purchase_use = { 'water_quantity_use': water_quantity, 'electricity_quantity_use': electricity_quantity, 'purchase_date': purchase_date } dormitory['w'] -= water_quantity dormitory['e'] -= electricity_quantity dormitory['water_usage'] += water_quantity dormitory['electricity_usage'] += electricity_quantity #'minimum_water_quantity':0,'minimum_electricity_quantity':0, if int(dormitory['w'] < dormitory['minimum_water_quantity']) & int(dormitory['e'] < dormitory['minimum_electricity_quantity']): print(f"宿舍 {dormitory_name} 的水量电量均低于设定值!") messagebox.showinfo("提示",f"宿舍 {dormitory_name} 的水量低于设定值!,宿舍 {dormitory_name} 的电量低于设定值!") else: if dormitory['w'] < dormitory['minimum_water_quantity']: print(f"宿舍 {dormitory_name} 的水量低于设定值!") messagebox.showinfo("提示", f"宿舍 {dormitory_name} 的水量低于设定值!") if dormitory['e'] < dormitory['minimum_electricity_quantity']: print(f"宿舍 {dormitory_name} 的电量低于设定值!") messagebox.showinfo("提示", f"宿舍 {dormitory_name} 的电量低于设定值!") if 'purchase_use' not in dormitory: dormitory['purchase_use'] = [] dormitory['purchase_use'].append(purchase_use) break # 统计某一段时间内的水电使用情况 def get_usage_summary(self, start_date, end_date): summary = { 'water_usage': 0, 'electricity_usage': 0 } for dormitory in self.dormitories: if 'purchase_use' in dormitory: for purchase_record in dormitory['purchase_use']: purchase_date = datetime.datetime.strptime(purchase_record['purchase_date'], '%Y-%m-%d').date() if start_date <= purchase_date <= end_date: summary['water_usage'] += purchase_record['water_quantity_use'] summary['electricity_usage'] += purchase_record['electricity_quantity_use'] return summary # 将系统数据保存到文件 def save_to_file(self, file_name): with open(file_name, 'w') as file: json.dump(self.dormitories, file) # 从文件加载系统数据 def load_from_file(self, file_name): with open(file_name, 'r') as file: self.dormitories = json.load(file) # 创建水电费管理系统对象 dump load system = DormitoryUtilitySystem() # 从文件加载系统数据 system.load_from_file('dormitory_data.json') # 创建主窗口 root = tk.Tk() root.title("主窗口") # 定义按钮点击事件处理函数 def open_add(): # 添加宿舍信息 # system.add_dormitory('Dorm A', 'A101', 300, 400, 6, 0.5, ['John Doe', 'Jane Smith']) # system.add_dormitory('Dorm B', 'B201', 250, 350, 5, 0.45, ['Alice Johnson', 'Bob Wilson']) # 创建主窗口 root = tk.Tk() root.title("学生宿舍水电费信息管理系统") # 定义函数:添加宿舍信息 def add_dormitory(): dormitory_name = dorm_name_entry.get() room_number = room_number_entry.get() water_rate = float(water_rate_entry.get()) electricity_rate = float(electricity_rate_entry.get()) maximum_water = int(max_water_entry.get()) maximum_electricity = int(max_electricity_entry.get()) tenants = tenants_entry.get().split(',') system.add_dormitory(dormitory_name, room_number, water_rate, electricity_rate, maximum_water, maximum_electricity, tenants) messagebox.showinfo("添加成功", "已成功添加宿舍信息!") # 创建宿舍信息输入部件 dorm_name_label = tk.Label(root, text="宿舍名称:") dorm_name_label.pack(pady=5) dorm_name_entry = tk.Entry(root) dorm_name_entry.pack(pady=5) room_number_label = tk.Label(root, text="房间号码:") room_number_label.pack(pady=5) room_number_entry = tk.Entry(root) room_number_entry.pack(pady=5) water_rate_label = tk.Label(root, text="用水量:") water_rate_label.pack(pady=5) water_rate_entry = tk.Entry(root) water_rate_entry.pack(pady=5) electricity_rate_label = tk.Label(root, text="用电量:") electricity_rate_label.pack(pady=5) electricity_rate_entry = tk.Entry(root) electricity_rate_entry.pack(pady=5) max_water_label = tk.Label(root, text="水费单价:") max_water_label.pack(pady=5) max_water_entry = tk.Entry(root) max_water_entry.pack(pady=5) max_electricity_label = tk.Label(root, text="电费单价:") max_electricity_label.pack(pady=5) max_electricity_entry = tk.Entry(root) max_electricity_entry.pack(pady=5) tenants_label = tk.Label(root, text="学生信息(逗号分隔):") tenants_label.pack(pady=5) tenants_entry = tk.Entry(root) tenants_entry.pack(pady=5) add_button = tk.Button(root, text="添加宿舍信息", command=add_dormitory) add_button.pack(pady=10) # 运行主循环 root.mainloop() # 定义按钮点击事件处理函数 def open_shezhi(): # 创建主窗口 root = tk.Tk() root.title("学生宿舍水电费信息管理系统") # 从文件加载系统数据 # system.load_from_file('dormitory_data.json') # 添加宿舍信息 # system.add_dormitory('Dorm A', 'A101', 300, 400, 6, 0.5, ['John Doe', 'Jane Smith']) # system.add_dormitory('Dorm B', 'B201', 250, 350, 5, 0.45, ['Alice Johnson', 'Bob Wilson']) # 定义界面函数 def show_statistics(): statistics = system.get_dormitory_statistics() messagebox.showinfo("统计信息", statistics) def set_minimum_quantity1(): dormitory_name = dorm_name_entry.get() minimum_water_quantity = int(min_water_entry.get()) minimum_electricity_quantity = int(min_electricity_entry.get()) system.set_minimum_quantity(dormitory_name, minimum_water_quantity, minimum_electricity_quantity) messagebox.showinfo("设置成功", "已成功设置最低值!") # 创建GUI部件 statistics_btn = tk.Button(root, text="显示统计信息", command=show_statistics) statistics_btn.pack(pady=10) set_minimum_frame = tk.Frame(root) set_minimum_frame.pack() dorm_name_label = tk.Label(set_minimum_frame, text="宿舍名称:") dorm_name_label.grid(row=0, column=0, padx=5, pady=5) dorm_name_entry = tk.Entry(set_minimum_frame) dorm_name_entry.grid(row=0, column=1, padx=5, pady=5) min_water_label = tk.Label(set_minimum_frame, text="水量最低值:") min_water_label.grid(row=1, column=0, padx=5, pady=5) min_water_entry = tk.Entry(set_minimum_frame) min_water_entry.grid(row=1, column=1, padx=5, pady=5) min_electricity_label = tk.Label(set_minimum_frame, text="电量最低值:") min_electricity_label.grid(row=2, column=0, padx=5, pady=5) min_electricity_entry = tk.Entry(set_minimum_frame) min_electricity_entry.grid(row=2, column=1, padx=5, pady=5) set_minimum_button = tk.Button(set_minimum_frame, text="设置最低值", command=set_minimum_quantity1) set_minimum_button.grid(row=3, columnspan=2, padx=5, pady=10) # 运行主循环 root.mainloop() # 定义按钮点击事件处理函数 def open_shopping(): # 创建主窗口 root = tk.Tk() root.title("学生宿舍水电费购买") # 从文件加载系统数据 # system.load_from_file('dormitory_data.json') # 定义界面函数 def show_statistics(): statistics = system.get_dormitory_shop() messagebox.showinfo("查看宿舍水电剩余信息", statistics) def set_minimum_quantityew(): dormitory_name = dorm_name_entry.get() shop_water_quantity = int(shop_water_entry.get()) shop_electricity_quantity = int(shop_electricity_entry.get()) da1= datetime.date.today().strftime('%Y-%m-%d') system.add_purchase_record(dormitory_name, shop_water_quantity, shop_electricity_quantity,da1) #messagebox.showinfo("提示", "购买成功!") # 创建GUI部件 statistics_btn = tk.Button(root, text="显示宿舍剩余水电情况", command=show_statistics) statistics_btn.pack(pady=10) set_shopping_frame = tk.Frame(root) set_shopping_frame.pack() dorm_name_label = tk.Label(set_shopping_frame, text="宿舍名称:") dorm_name_label.grid(row=0, column=0, padx=5, pady=5) dorm_name_entry = tk.Entry(set_shopping_frame) dorm_name_entry.grid(row=0, column=1, padx=5, pady=5) shop_water_label = tk.Label(set_shopping_frame, text="购买水量:") shop_water_label.grid(row=1, column=0, padx=5, pady=5) shop_water_entry = tk.Entry(set_shopping_frame) shop_water_entry.grid(row=1, column=1, padx=5, pady=5) shop_electricity_label = tk.Label(set_shopping_frame, text="购买电量:") shop_electricity_label.grid(row=2, column=0, padx=5, pady=5) shop_electricity_entry = tk.Entry(set_shopping_frame) shop_electricity_entry.grid(row=2, column=1, padx=5, pady=5) set_minimum_button = tk.Button(set_shopping_frame, text="购买", command=set_minimum_quantityew) set_minimum_button.grid(row=3, columnspan=2, padx=5, pady=10) # 运行主循环 root.mainloop() # 定义按钮点击事件处理函数 def open_us(): # 创建主窗口 root = tk.Tk() root.title("学生宿舍水电使用登记表") # 从文件加载系统数据 # system.load_from_file('dormitory_data.json') # 定义界面函数 def show_statistics(): statistics = system.get_dormitory_shop() messagebox.showinfo("查看宿舍水电剩余信息", statistics) def us_minimum_quantityew(): dormitory_name = dorm_name_entry.get() use_water_quantity = int(us_water_entry.get()) use_electricity_quantity = int(us_electricity_entry.get()) day= datetime.date.today().strftime('%Y-%m-%d') system.add_purchase_use(dormitory_name, use_water_quantity, use_electricity_quantity,day) messagebox.showinfo("提示", "登记完成") # 创建GUI部件 statistics_btn = tk.Button(root, text="显示宿舍剩余水电情况", command=show_statistics) statistics_btn.pack(pady=10) set_use_frame = tk.Frame(root) set_use_frame.pack() dorm_name_label = tk.Label(set_use_frame, text="宿舍名称:") dorm_name_label.grid(row=0, column=0, padx=5, pady=5) dorm_name_entry = tk.Entry(set_use_frame) dorm_name_entry.grid(row=0, column=1, padx=5, pady=5) us_water_label = tk.Label(set_use_frame, text="使用水量:") us_water_label.grid(row=1, column=0, padx=5, pady=5) us_water_entry = tk.Entry(set_use_frame) us_water_entry.grid(row=1, column=1, padx=5, pady=5) us_electricity_label = tk.Label(set_use_frame, text="使用电量:") us_electricity_label.grid(row=2, column=0, padx=5, pady=5) us_electricity_entry = tk.Entry(set_use_frame) us_electricity_entry.grid(row=2, column=1, padx=5, pady=5) set_minimum_button = tk.Button(set_use_frame, text="记录", command=us_minimum_quantityew) set_minimum_button.grid(row=3, columnspan=2, padx=5, pady=10) # 运行主循环 root.mainloop() # 定义按钮点击事件处理函数 def open_usage(): # 创建主窗口 root = tk.Tk() root.title("时间段内的用水量") # 从文件加载系统数据 # system.load_from_file('dormitory_data.json') # 定义界面函数 def show_statistics(): statistics = system.get_dormitory_shop() messagebox.showinfo("查看宿舍水电剩余信息", statistics) def us_minimum_quantityewdate(): date_str = s_entry.get() # 字符串格式为"YYYY-MM-DD","2023-06-23" year, month, day = map(int, date_str.split('-')) start_date = datetime.date(year, month, day) date_str = e_entry.get() # 字符串格式为"YYYY-MM-DD","2023-06-23" year, month, day = map(int, date_str.split('-')) end_date = datetime.date(year, month, day) #day = datetime.date.today().strftime('%Y-%m-%d') mesage=system.get_usage_summary(start_date, end_date) messagebox.showinfo("提示", f"{start_date}___{end_date} 宿舍总用水量 {mesage['water_usage']} ,总用电量{mesage['electricity_usage']}") # 创建GUI部件 statistics_btn = tk.Button(root, text="显示宿舍剩余水电情况", command=show_statistics) statistics_btn.pack(pady=10) set_date_frame = tk.Frame(root) set_date_frame.pack() s_label = tk.Label(set_date_frame, text="起始日期:") s_label.grid(row=1, column=0, padx=5, pady=5) s_entry = DateEntry(set_date_frame, date_pattern='yyyy-mm-dd') s_entry.grid(row=1, column=1, padx=5, pady=5) e_label = tk.Label(set_date_frame, text="终止日期:") e_label.grid(row=2, column=0, padx=5, pady=5) e_entry = DateEntry(set_date_frame, date_pattern='yyyy-mm-dd') e_entry.grid(row=2, column=1, padx=5, pady=5) set_minimum_button = tk.Button(set_date_frame, text="查看", command=us_minimum_quantityewdate) set_minimum_button.grid(row=3, columnspan=2, padx=5, pady=10) # 运行主循环 root.mainloop() def shows(): aw=system.get_dormitory_statistics() e=[] w=[] name=[] for aa in aw: e.append(aa['用电量']) w.append(aa['用水量']) name.append(aa['宿舍名']) # 创建柱状图 plt.bar(name, e, label='用电量') plt.xlabel('宿舍名') plt.ylabel('用电量(kWh)') plt.title('各宿舍用电量统计图') plt.legend() # 显示图表 plt.show() # 创建柱状图 plt.bar(name, w, label='用水量') plt.xlabel('宿舍名') plt.ylabel('用水量') plt.title('各宿舍用水量统计图') plt.legend() # 显示图表 plt.show() # 创建按钮 button = tk.Button(root, text="添加宿舍信息", command=open_add, width=20, height=2) button.pack(padx=100, pady=50)# 设置X轴和Y轴方向的间隔 # 创建按钮 button = tk.Button(root, text="设置每个宿舍的水电阈值", command=open_shezhi, width=20, height=2) button.pack(padx=30, pady=40) # 创建按钮 button = tk.Button(root, text="宿舍水电购买", command=open_shopping, width=20, height=2) button.pack(padx=30, pady=40) # 创建按钮 button = tk.Button(root, text="宿舍水电使用登记", command=open_us, width=20, height=2) button.pack(padx=30, pady=40) # 创建按钮 button = tk.Button(root, text="一段时间内的用电用水使用情况", command=open_usage, width=25, height=2) button.pack(padx=30, pady=40) # 创建按钮 button = tk.Button(root, text="统计图", command=shows, width=25, height=2) button.pack(padx=30, pady=40) # 运行主循环 root.mainloop() # 将系统数据保存到文件 system.save_to_file('dormitory_data.json')