某个家庭每月有固定收入(如工资)和非固定收入(如兼职收入),家庭每月有固定支出(如电话费)和非固定支出。请编程实现一个家庭的收支的模拟系统,要求实现以下功能: 1.每月固定的收入和非固定收入信息的录入,包括时间、金额、类别等。 2. 每月固定的支出和非固定支出信息的录入,包括消费信息,包括消费时间、消费金额、消费类别等。 3.信息查询与统计:判断家庭是否收支平衡;根据时间、金额大小、类别进行查询并汇总。 4.信息存储:程序退出时将信息存储到硬盘上,开启时从硬盘读取信息。 5.使用合理的方式进行类的封装以完成上述功能。 6.系统以菜单界面方式(至少采用文本菜单界面,如能采用图形菜单界面更好)工作,运行界面友好,演示程序以用户和计算机的对话方式进行。
时间: 2023-06-16 08:03:34 浏览: 72
管理家庭财务收入和支出的家庭收支管理系统
4星 · 用户满意度95%
好的,这个任务可以用面向对象的方式来实现。我们可以定义以下几个类:
1. Income类,用于记录家庭每月的收入信息;
2. Expense类,用于记录家庭每月的支出信息;
3. Balance类,用于判断家庭是否收支平衡;
4. Query类,用于根据时间、金额大小、类别进行查询并汇总;
5. Storage类,用于信息的存储和读取;
6. Menu类,用于显示菜单和用户交互。
下面是一个简单的实现示例:
```python
import json
import os
class Income:
def __init__(self, time, amount, category):
self.time = time
self.amount = amount
self.category = category
class Expense:
def __init__(self, time, amount, category):
self.time = time
self.amount = amount
self.category = category
class Balance:
def __init__(self, incomes, expenses):
self.total_income = sum([income.amount for income in incomes])
self.total_expense = sum([expense.amount for expense in expenses])
def is_balanced(self):
return self.total_income == self.total_expense
class Query:
def __init__(self, incomes, expenses):
self.incomes = incomes
self.expenses = expenses
def query_by_time(self, start_time, end_time):
income_result = [income for income in self.incomes if start_time <= income.time <= end_time]
expense_result = [expense for expense in self.expenses if start_time <= expense.time <= end_time]
return income_result, expense_result
def query_by_amount(self, low_amount, high_amount):
income_result = [income for income in self.incomes if low_amount <= income.amount <= high_amount]
expense_result = [expense for expense in self.expenses if low_amount <= expense.amount <= high_amount]
return income_result, expense_result
def query_by_category(self, category):
income_result = [income for income in self.incomes if income.category == category]
expense_result = [expense for expense in self.expenses if expense.category == category]
return income_result, expense_result
def query_summary(self):
income_total = sum([income.amount for income in self.incomes])
expense_total = sum([expense.amount for expense in self.expenses])
return income_total, expense_total
class Storage:
def __init__(self, file_name):
self.file_name = file_name
def save_data(self, data):
with open(self.file_name, 'w') as f:
json.dump(data, f)
def load_data(self):
if os.path.exists(self.file_name):
with open(self.file_name, 'r') as f:
data = json.load(f)
incomes = [Income(income['time'], income['amount'], income['category']) for income in data['incomes']]
expenses = [Expense(expense['time'], expense['amount'], expense['category']) for expense in data['expenses']]
return incomes, expenses
else:
return [], []
class Menu:
def __init__(self):
self.incomes = []
self.expenses = []
self.balance = None
self.query = None
self.storage = Storage('data.json')
def load_data(self):
self.incomes, self.expenses = self.storage.load_data()
self.balance = Balance(self.incomes, self.expenses)
self.query = Query(self.incomes, self.expenses)
def save_data(self):
data = {'incomes': [{'time': income.time, 'amount': income.amount, 'category': income.category} for income in self.incomes],
'expenses': [{'time': expense.time, 'amount': expense.amount, 'category': expense.category} for expense in self.expenses]}
self.storage.save_data(data)
def show_menu(self):
print('1. 录入收入信息')
print('2. 录入支出信息')
print('3. 查询收入支出信息')
print('4. 判断家庭是否收支平衡')
print('5. 退出程序')
def input_income(self):
time = input('请输入收入时间(格式如2022-01-01):')
amount = float(input('请输入收入金额:'))
category = input('请输入收入类别:')
income = Income(time, amount, category)
self.incomes.append(income)
self.balance = Balance(self.incomes, self.expenses)
self.query = Query(self.incomes, self.expenses)
self.save_data()
def input_expense(self):
time = input('请输入支出时间(格式如2022-01-01):')
amount = float(input('请输入支出金额:'))
category = input('请输入支出类别:')
expense = Expense(time, amount, category)
self.expenses.append(expense)
self.balance = Balance(self.incomes, self.expenses)
self.query = Query(self.incomes, self.expenses)
self.save_data()
def query_by_time(self):
start_time = input('请输入起始时间(格式如2022-01-01):')
end_time = input('请输入结束时间(格式如2022-01-01):')
income_result, expense_result = self.query.query_by_time(start_time, end_time)
print('收入信息:')
for income in income_result:
print('时间:{},金额:{},类别:{}'.format(income.time, income.amount, income.category))
print('支出信息:')
for expense in expense_result:
print('时间:{},金额:{},类别:{}'.format(expense.time, expense.amount, expense.category))
def query_by_amount(self):
low_amount = float(input('请输入最低金额:'))
high_amount = float(input('请输入最高金额:'))
income_result, expense_result = self.query.query_by_amount(low_amount, high_amount)
print('收入信息:')
for income in income_result:
print('时间:{},金额:{},类别:{}'.format(income.time, income.amount, income.category))
print('支出信息:')
for expense in expense_result:
print('时间:{},金额:{},类别:{}'.format(expense.time, expense.amount, expense.category))
def query_by_category(self):
category = input('请输入类别:')
income_result, expense_result = self.query.query_by_category(category)
print('收入信息:')
for income in income_result:
print('时间:{},金额:{},类别:{}'.format(income.time, income.amount, income.category))
print('支出信息:')
for expense in expense_result:
print('时间:{},金额:{},类别:{}'.format(expense.time, expense.amount, expense.category))
def query_summary(self):
income_total, expense_total = self.query.query_summary()
print('总收入:{},总支出:{}'.format(income_total, expense_total))
def start(self):
self.load_data()
while True:
self.show_menu()
choice = input('请输入您的选择:')
if choice == '1':
self.input_income()
elif choice == '2':
self.input_expense()
elif choice == '3':
print('1. 按时间查询')
print('2. 按金额查询')
print('3. 按类别查询')
print('4. 查询收支汇总')
sub_choice = input('请输入您的选择:')
if sub_choice == '1':
self.query_by_time()
elif sub_choice == '2':
self.query_by_amount()
elif sub_choice == '3':
self.query_by_category()
elif sub_choice == '4':
self.query_summary()
else:
print('无效的选择!')
elif choice == '4':
if self.balance.is_balanced():
print('家庭收支平衡!')
else:
print('家庭收支不平衡!')
elif choice == '5':
print('谢谢使用!')
break
else:
print('无效的选择!')
if __name__ == '__main__':
menu = Menu()
menu.start()
```
阅读全文