Create a class Budget
that maintains private attributes for different spending categories. Include methods to allocate and spend money, ensuring that overspending is not allowed.
Example 1:
Input: Allocate: 1000, Spend: 500 Output: Remaining: 500
Example 2:
Input: Allocate: 1000, Spend: 1500 Output: "Insufficient budget"
Check the remaining budget before allowing any spending.
class Budget: def __init__(self): self._amount = 0 def allocate(self, amount): self._amount += amount def spend(self, amount): if self._amount >= amount: self._amount -= amount else: return "Insufficient budget" def get_remaining(self): return self._amount # Test the class budget = Budget() budget.allocate(1000) budget.spend(500) print(budget.get_remaining()) # Output: 500 budget.spend(1500) print(budget.get_remaining()) # Output: Insufficient budget
Unlock AI & Data Science treasures. Log in!