以下是一个简单的家庭收支模拟系统的示例代码,实现了以上要求:
``` python
import json
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 Family:
def __init__(self):
self.fixed_income = []
self.non_fixed_income = []
self.fixed_expense = []
self.non_fixed_expense = []
def add_fixed_income(self, time, amount, category):
self.fixed_income.append(Income(time, amount, category))
def add_non_fixed_income(self, time, amount, category):
self.non_fixed_income.append(Income(time, amount, category))
def add_fixed_expense(self, time, amount, category):
self.fixed_expense.append(Expense(time, amount, category))
def add_non_fixed_expense(self, time, amount, category):
self.non_fixed_expense.append(Expense(time, amount, category))
def balance(self):
total_income = sum([income.amount for income in self.fixed_income + self.non_fixed_income])
total_expense = sum([expense.amount for expense in self.fixed_expense + self.non_fixed_expense])
return total_income - total_expense
def query_by_time(self, start_time, end_time):
incomes = [income for income in self.fixed_income + self.non_fixed_income if start_time <= income.time <= end_time]
expenses = [expense for expense in self.fixed_expense + self.non_fixed_expense if start_time <= expense.time <= end_time]
return incomes, expenses
def query_by_amount(self, min_amount, max_amount):
incomes = [income for income in self.fixed_income + self.non_fixed_income if min_amount <= income.amount <= max_amount]
expenses = [expense for expense in self.fixed_expense + self.non_fixed_expense if min_amount <= expense.amount <= max_amount]
return incomes, expenses
def query_by_category(self, category):
incomes = [income for income in self.fixed_income + self.non_fixed_income if income.category == category]
expenses = [expense for expense in self.fixed_expense + self.non_fixed_expense if expense.category == category]
return incomes, expenses
def save_to_file(self, file_path):
data = {
'fixed_income': [(income.time, income.amount, income.category) for income in self.fixed_income],
'non_fixed_income': [(income.time, income.amount, income.category) for income in self.non_fixed_income],
'fixed_expense': [(expense.time, expense.amount, expense.category) for expense in self.fixed_expense],
'non_fixed_expense': [(expense.time, expense.amount, expense.category) for expense in self.non_fixed_expense],
}
with open(file_path, 'w') as f:
json.dump(data, f)
def load_from_file(self, file_path):
with open(file_path, 'r') as f:
data = json.load(f)
self.fixed_income = [Income(*item) for item in data['fixed_income']]
self.non_fixed_income = [Income(*item) for item in data['non_fixed_income']]
self.fixed_expense = [Expense(*item) for item in data['fixed_expense']]
self.non_fixed_expense = [Expense(*item) for item in data['non_fixed_expense']]
# 示例代码
family = Family()
family.add_fixed_income('2022-01-01', 5000, 'salary')
family.add_non_fixed_income('2022-01-02', 1000, 'part-time job')
family.add_fixed_expense('2022-01-03', 2000, 'rent')
family.add_non_fixed_expense('2022-01-04', 500, 'food')
print(f'Balance: {family.balance()}')
incomes, expenses = family.query_by_time('2022-01-01', '2022-01-02')
print(f'Incomes: {[income.amount for income in incomes]}')
print(f'Expenses: {[expense.amount for expense in expenses]}')
incomes, expenses = family.query_by_amount(500, 2000)
print(f'Incomes: {[income.amount for income in incomes]}')
print(f'Expenses: {[expense.amount for expense in expenses]}')
incomes, expenses = family.query_by_category('salary')
print(f'Incomes: {[income.amount for income in incomes]}')
print(f'Expenses: {[expense.amount for expense in expenses]}')
family.save_to_file('family.json')
family.load_from_file('family.json')
```
以上代码中,我们定义了三个类:Income、Expense和Family。Income和Expense分别表示收入和支出,包含时间、金额和类别三个属性。Family类用于管理家庭的收支信息,包含四个列表属性:fixed_income(固定收入)、non_fixed_income(非固定收入)、fixed_expense(固定支出)和non_fixed_expense(非固定支出)。Family类的方法包括:添加收支信息、计算收支平衡、查询收支信息、将信息存储到硬盘上和从硬盘读取信息。
我们可以通过调用Family类的方法,实现对家庭收支信息的管理和查询。同时,我们可以将信息存储到硬盘上,以便下次使用时能够继续读取之前的信息。