python面向对象课程设计: Python实现个人收支管理系统

本文介绍了如何使用Python编程语言创建一个简单的个人收支管理系统,该系统利用pickle模块存储收支数据,包括记录收入和支出、查看余额、历史记录查询等功能,并提供了一个交互式命令行界面供用户操作。
摘要由CSDN通过智能技术生成

Python实现个人收支管理系统

概述

在本篇文章中,我们将探讨如何使用Python编程语言创建一个简单但功能齐全的个人收支管理系统。该系统将允许我们记录收入和支出,查看当前的余额,存储历史记录以及查询某个时间段的收支明细。我们将使用Python的pickle模块将收支数据持久化到文件中。

实现

首先,我们需要定义两个类:TransactionFinanceManagementSystem

Transaction 类表示单个收支事项,包含类型(收入或支出)、金额和日期三个属性。__str__() 方法用于返回收支事项的字符串表示。

FinanceManagementSystem 类负责管理收支数据。它包含一系列方法,包括加载和保存收支数据、记录收入和支出、查看余额、查看历史记录和查询某个时间段的收支明细。

具体的代码如下:

import os
import pickle
from datetime import datetime

class Transaction:
    def __init__(self, type, amount, date):
        self.type = type
        self.amount = amount
        self.date = date

    def __str__(self):
        return f"{self.date}: {self.type}, {self.amount}"

class FinanceManagementSystem:
    def __init__(self, filename="finance.txt"):
        self.filename = filename
        self.transactions = self.load_transactions()

    def load_transactions(self):
        if os.path.exists(self.filename):
            with open(self.filename, "rb") as file:
                return pickle.load(file)
        else:
            return []

    def save_transactions(self):
        with open(self.filename, "wb") as file:
            pickle.dump(self.transactions, file)

    def record_transaction(self, type, amount):
        new_transaction = Transaction(type, amount, datetime.now())
        self.transactions.append(new_transaction)
        self.save_transactions()

    def get_balance(self):
        balance = 0
        for transaction in self.transactions:
            if transaction.type == "收入":
                balance += transaction.amount
            elif transaction.type == "支出":
                balance -= transaction.amount
        return balance

    def display_transactions(self):
        for transaction in self.transactions:
            print(transaction)

    def find_transactions(self, start_date, end_date):
        result = []
        for transaction in self.transactions:
            if start_date <= transaction.date <= end_date:
                result.append(transaction)
        return result

交互界面

接下来,我们为系统添加一个交互式命令行界面,以方便用户使用。我们将提供一个菜单,用户可以通过输入对应的数字来选择想要执行的操作。

if __name__ == "__main__":
    system = FinanceManagementSystem()

    while True:
        print("\n个人收支管理系统菜单:")
        print("1. 记录收入")
        print("2. 记录支出")
        print("3. 查看余额")
        print("4. 查看所有交易")
        print("5. 查询某个时间段的交易")
        print("6. 退出")

        choice = input("请选择功能(输入数字):")

        if choice == "1":
            amount = float(input("请输入收入金额:"))
            system.record_transaction("收入", amount)
            print("收入已成功记录!")
        elif choice == "2":
            amount = float(input("请输入支出金额:"))
            system.record_transaction("支出", amount)
            print("支出已成功记录!")
        elif choice == "3":
            balance = system.get_balance()
            print(f"当前余额:{balance}")
        elif choice == "4":
            print("所有交易记录:")
            system.display_transactions()
        elif choice == "5":
            start_date_str = input("请输入开始日期(格式:YYYY-MM-DD):")
            end_date_str = input("请输入结束日期(格式:YYYY-MM-DD):")
            start_date = datetime.strptime(start_date_str, "%Y-%m-%d")
            end_date = datetime.strptime(end_date_str, "%Y-%m-%d")
            transactions = system.find_transactions(start_date, end_date)
            for transaction in transactions:
                print(transaction)
        elif choice == "6":
            print("谢谢使用,再见!")
            break
        else:
            print("无效的输入,请重新选择。")

在线预览

🔗在线预览

总结

在这篇文章中,我们使用Python创建了一个简单的个人收支管理系统。虽然该系统的功能相对基础,但它提供了一个良好的起点,你可以根据自己的需求对其进行扩展和优化。例如,你可以添加对个人财务状况的统计分析功能,或者添加一个图形用户界面(GUI)以提升用户体验。

Python的灵活性和易读性使其成为实现这类系统的理想选择。希望你能从中学到一些有用的知识,并将其应用到你自己的项目中。

  • 5
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值