Калькулятор бюджета (50/30/20)
Разделите доход по правилу 50/30/20: нужды, желания и сбережения.
About the 50/30/20 Rule
A simple budgeting framework: allocate 50% of after-tax income to needs, 30% to wants, and 20% to savings or debt repayment.
Часто задаваемые вопросы
Реализация кода
from dataclasses import dataclass, field
from typing import List
@dataclass
class BudgetItem:
name: str
amount: float
category: str # 'need' | 'want' | 'savings'
@dataclass
class Budget:
income: float
items: List[BudgetItem] = field(default_factory=list)
def add_item(self, name: str, amount: float, category: str):
self.items.append(BudgetItem(name, amount, category))
def total_expenses(self) -> float:
return sum(i.amount for i in self.items)
def surplus(self) -> float:
return self.income - self.total_expenses()
def by_category(self) -> dict:
totals = {'need': 0.0, 'want': 0.0, 'savings': 0.0}
for item in self.items:
totals[item.category] += item.amount
return totals
def rule_50_30_20(self) -> dict:
"""Check against the 50/30/20 guideline."""
cats = self.by_category()
return {
'needs_pct': cats['need'] / self.income * 100,
'wants_pct': cats['want'] / self.income * 100,
'savings_pct': cats['savings'] / self.income * 100,
}
# Example
budget = Budget(income=5000)
budget.add_item("Rent", 1200, "need")
budget.add_item("Groceries", 400, "need")
budget.add_item("Utilities", 150, "need")
budget.add_item("Transport", 300, "need")
budget.add_item("Dining out", 250, "want")
budget.add_item("Streaming", 50, "want")
budget.add_item("Entertainment", 200, "want")
budget.add_item("Emergency fund", 500, "savings")
budget.add_item("Investments", 500, "savings")
cats = budget.by_category()
rule = budget.rule_50_30_20()
print(f"Income: ${budget.income:,.2f}")
print(f"Expenses: ${budget.total_expenses():,.2f}")
print(f"Surplus: ${budget.surplus():,.2f}")
print(f"Needs: ${cats['need']:,.2f} ({rule['needs_pct']:.1f}%, target 50%)")
print(f"Wants: ${cats['want']:,.2f} ({rule['wants_pct']:.1f}%, target 30%)")
print(f"Savings: ${cats['savings']:,.2f} ({rule['savings_pct']:.1f}%, target 20%)")
Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.