python开发记账本课程设计

时间: 2023-10-04 12:12:42 浏览: 71
好的,下面是一个简单的Python记账本的课程设计: ## 1.需求分析 本记账本需要实现以下功能: 1. 记录每天的收入和支出。 2. 可以查询某一天或某一段时间的收支情况。 3. 可以计算某一段时间的总收入和总支出。 4. 可以输出每个月的总结报表。 ## 2.设计思路 本记账本采用文件存储的方式,每一天的收支情况以及月度总结都保存在一个文本文件中。具体设计思路如下: 1. 创建一个`Record`类,用于记录每一笔收支情况,包括日期、类型(收入或支出)、金额和备注。 2. 创建一个`Account`类,用于管理所有的收支记录。这个类包括以下方法: - `add_record(record)`:添加一条新的收支记录。 - `query(date)`:查询某一天的收支情况。 - `query_range(start_date, end_date)`:查询某一段时间的收支情况。 - `total_income(start_date, end_date)`:计算某一段时间的总收入。 - `total_expense(start_date, end_date)`:计算某一段时间的总支出。 - `report_monthly(year, month)`:生成某个月的总结报表。 3. 创建一个`FileManager`类,用于管理所有的文本文件。这个类包括以下方法: - `get_file_name(date)`:根据日期生成对应的文件名。 - `write_record(record)`:将一条收支记录写入对应的文件中。 - `read_records(date)`:从对应的文件中读取所有的收支记录。 ## 3.代码实现 下面是一个简单的Python记账本的代码实现。具体实现过程中,还需要处理一些异常情况和输入参数的格式,这里只展示主要的代码结构。 ```python import os import datetime class Record: def __init__(self, date, type, amount, note): self.date = date self.type = type self.amount = amount self.note = note class Account: def __init__(self): self.records = [] def add_record(self, record): self.records.append(record) def query(self, date): result = [] for record in self.records: if record.date == date: result.append(record) return result def query_range(self, start_date, end_date): result = [] for record in self.records: if start_date <= record.date <= end_date: result.append(record) return result def total_income(self, start_date, end_date): total = 0 for record in self.records: if record.type == 'income' and start_date <= record.date <= end_date: total += record.amount return total def total_expense(self, start_date, end_date): total = 0 for record in self.records: if record.type == 'expense' and start_date <= record.date <= end_date: total += record.amount return total def report_monthly(self, year, month): start_date = datetime.date(year, month, 1) end_date = datetime.date(year, month, 1) + datetime.timedelta(days=30) income = self.total_income(start_date, end_date) expense = self.total_expense(start_date, end_date) balance = income - expense print(f'{year}年{month}月总收入:{income}元') print(f'{year}年{month}月总支出:{expense}元') print(f'{year}年{month}月结余:{balance}元') class FileManager: def __init__(self, data_dir): self.data_dir = data_dir def get_file_name(self, date): return os.path.join(self.data_dir, f'{date}.txt') def write_record(self, record): file_name = self.get_file_name(record.date) with open(file_name, 'a') as f: f.write(f'{record.type},{record.amount},{record.note}\n') def read_records(self, date): file_name = self.get_file_name(date) if not os.path.exists(file_name): return [] records = [] with open(file_name, 'r') as f: for line in f: parts = line.strip().split(',') type = parts[0] amount = float(parts[1]) note = parts[2] record = Record(date, type, amount, note) records.append(record) return records ``` ## 4.使用示例 下面是一个简单的使用示例,展示了如何创建一个记账本、添加记录、查询记录、计算总收支等功能。 ```python data_dir = '/path/to/data' file_manager = FileManager(data_dir) account = Account() # 添加记录 record1 = Record(date=datetime.date(2021, 1, 1), type='income', amount=1000, note='工资') record2 = Record(date=datetime.date(2021, 1, 3), type='expense', amount=200, note='餐饮') record3 = Record(date=datetime.date(2021, 1, 5), type='expense', amount=300, note='购物') file_manager.write_record(record1) file_manager.write_record(record2) file_manager.write_record(record3) account.add_record(record1) account.add_record(record2) account.add_record(record3) # 查询记录 records = account.query(datetime.date(2021, 1, 3)) for record in records: print(record.date, record.type, record.amount, record.note) # 计算总收支 total_income = account.total_income(datetime.date(2021, 1, 1), datetime.date(2021, 1, 31)) total_expense = account.total_expense(datetime.date(2021, 1, 1), datetime.date(2021, 1, 31)) print('总收入:', total_income) print('总支出:', total_expense) # 生成月度总结报表 account.report_monthly(2021, 1) ```

相关推荐

最新推荐

recommend-type

python扫雷游戏设计(课程设计版)

python扫雷游戏,课程设计,一文解决。此报告包含相关代码的解释和源代码,如果有界面要求可以私聊博主。可以帮助部分同学节省一大部分时间,课程设计报告可以直接将这个docx稍微改一下就好。
recommend-type

EduCoder实践课程——Python程序设计入门答案

记:由于疫情暂时返不了校,然后学校大四毕业年级布置了在线实训的任务,我选择了实践课程Python程序设计入门。以前没有学过,可能是之前有过acm经验,感觉Python挺好入门的,把自己学习过程中的代码记录下来,一是...
recommend-type

Python数据处理课程设计-房屋价格预测

此外,无论是对于监管者还是消费者,是房产中介机构还是房地产开发商,只有深入了解房地产交易市场,才能进行合理监管与规划;高效率推广房源,在能满足购房者需求的前提下科学定价,提高市场竞争优势;有效规避风险...
recommend-type

Python综合课程设计贪吃蛇

这是大三的Python通过一年的学习,最后的课程设计作业,虽然不是很美,很多细节处理的还是很用心的
recommend-type

Python restful框架接口开发实现

主要介绍了Python restful框架接口开发实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

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