-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.py
75 lines (54 loc) · 2.63 KB
/
Account.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from FileManager import FileManager
from HistoryMessages import HistoryMessages
class Account:
def __init__(self, balance = 0):
self.balance = balance
self.file_manager = FileManager()
self.hist_file_path = "hist.json"
def write_to_history(self, hist_dict):
self.file_manager.add_to_json(hist_dict,self.hist_file_path)
def deposit(self, amount):
try:
amount = int(amount) # Convert amount to integer
if amount <= 0:
status = "failure"
print("Invalid amount for deposit!")
else:
self.balance += amount
status = "success"
except ValueError:
print("Invalid input for deposit!")
status = "failure"
history_message = HistoryMessages.deposit(status, amount, self.balance)
self.write_to_history(history_message)
# deposit is a @static method in HistoryMessages.
# so it can be called by HistoryMessages.deposit() without creating instance of History messages class
def debit(self, amount):
try:
amount = int(amount)
if self.balance < amount:
status = "failure"
print("Invalid amount for debit!")
else:
self.balance -= amount
status = "success"
except ValueError:
print("Invalid input for debit!")
status = "failure"
history_message = HistoryMessages.debit(status, amount, self.balance)
self.write_to_history(history_message)
def get_balance(self):
return self.balance
def dict_to_string(self, dict):
if dict["operation_type"] != "exchange":
return f'type: {dict["operation_type"]} status: {dict["status"]} amount: {dict["amount_of_deposit"]} balance: {dict["total_balance"]}'
else:
return f'type: {dict["operation_type"]} status: {dict["status"]} pre exchange amount: {dict["pre_exchange_amount"]} exchange amount: {dict["exchange_amount"]} currency from: {dict["currency_from"]} currency to: {dict["currency_to"]}'
def get_history(self):
history = self.file_manager.read_json(self.hist_file_path)
if history is not None: # Check if history is not None (file exists and data is read successfully)
history_string = "" # Initialize an empty string to store the history as a string
for entry in history:
entry_string = self.dict_to_string(entry)
history_string += entry_string + "\n" # append to
return history_string