用Python写一个账单信息管理系统,里面有收入支出,修改账单,查询账单,显示余额,退出系统
时间: 2024-11-13 22:18:03 浏览: 15
python基于Django的在线账单记录系统源码(课程设计).zip
在Python中编写一个简单的账单信息管理系统可以采用类结构来组织,下面是一个基本的设计框架:
```python
class AccountManager:
def __init__(self):
self.incomes = []
self.expenses = []
# 添加收入
def add_income(self, amount, description=""):
self.incomes.append({"amount": amount, "description": description})
# 添加支出
def add_expense(self, amount, description=""):
self.expenses.append({"amount": -amount, "description": description})
# 修改账单(这里假设我们有唯一的标识符)
def update_bill(self, identifier, new_amount=None, new_description=None):
for i, bill in enumerate(self.incomes if amount > 0 else self.expenses):
if bill["identifier"] == identifier:
if new_amount is not None:
bill["amount"] = new_amount
if new_description is not None:
bill["description"] = new_description
break
# 查询账单
def query_bill(self, identifier):
incomes = [bill for bill in self.incomes if bill["identifier"] == identifier]
expenses = [bill for bill in self.expenses if bill["identifier"] == identifier]
return {"incomes": incomes, "expenses": expenses}
# 显示余额
def show_balance(self):
total = sum(income["amount"] for income in self.incomes) - sum(expense["amount"] for expense in self.expenses)
return f"当前余额: {total}元"
# 退出系统
def exit_system(self):
print("感谢使用账单管理器,再见!")
return True
# 示例用法
manager = AccountManager()
manager.add_income(5000, "工资")
manager.add_expense(2000, "购物")
print(manager.show_balance()) # 输出当前余额
identifier = "工资"
query_result = manager.query_bill(identifier)
# 更改账单描述
manager.update_bill(identifier, new_description="本月奖金")
```
阅读全文