-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance_manager.py
34 lines (25 loc) · 953 Bytes
/
balance_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from data_manager import DataManager
import inject
from singleton_metaclass import SingletonMeta
class BalanceManager(metaclass=SingletonMeta):
INITIAL_BALANCE = 100.0
@inject.autoparams()
def __init__(self, data_manager: DataManager):
self.data_manager = data_manager
self.balance = self.load_balance()
def load_balance(self) -> float:
balance: float = self.data_manager.get_value('balance')
if not balance:
return self.INITIAL_BALANCE
return balance
def add_funds(self, amount: float) -> None:
self.balance += amount
def deduct_funds(self, amount: float) -> None:
if amount <= self.balance:
self.balance -= amount
else:
raise ValueError("Insufficient balance")
def get_balance(self) -> float:
return self.balance
def reset_balance(self) -> None:
self.balance = self.INITIAL_BALANCE