-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.py
145 lines (114 loc) · 4.66 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from datetime import datetime
time_right_now = datetime.now() # time object
current_time = time_right_now.strftime("%Y-%m-%d %H-%M")
class Account:
def __init__(self, accNum, accType, balance):
# account_type = ('debit account', 'credit account', 'savings account')
self.account_number = int(accNum)
self.account_type = accType
self.account_balance = float(balance)
self.transaction_history_list = []
self.load_transactions_from_txt()
def update_transaction_db(self, transaction):
text_file = open("transactions_list.txt", 'a')
text_file.write(transaction)
text_file.write('\n')
text_file.close()
def load_transactions_from_txt(self):
raw_data_list = open("transactions_list.txt").readlines()
slask_list = []
for rows in raw_data_list:
a = rows.replace('\n', '').split('%')
if len(a) > 1:
if int(a[0]) == self.account_number:
slask_list.append(a)
if len(slask_list) > 0: #om det finns transaktioner loggade
self.transaction_history_list = slask_list
def remove_transaction_list(self):
txt = open("transactions_list.txt", "r+")
raw_data_list = txt.readlines()
txt.seek(0)
txt.truncate()
for row in raw_data_list:
acc_num_key = int(row[0:4])
if acc_num_key != self.account_number:
txt.write(str(row))
elif acc_num_key == self.account_number:
txt.write('')
txt.close()
self.load_transactions_from_txt()
def add_to_trans_hist(self, transaction):
line = str(f"{self.account_number}%{current_time}%{transaction}%{self.account_balance}")
self.transaction_history_list.append(line)
self.update_transaction_db(line)
self.load_transactions_from_txt()
# Meny
def account_menu(self):
a = -1
self.print_account_info()
while a != 0:
print('')
print('1. Withdraw')
print('2. Deposit')
print('3. Print account info')
print('4. Change account')
print('5. Transaction history')
print('0. Exit to main menu')
a = int(input('Choice: '))
if a == 1:
self.withdraw()
elif a == 2:
self.deposit()
elif a == 3:
self.print_account_info()
elif a == 4:
break
elif a == 5:
self.print_transaction_history()
# transaction history
def print_transaction_history(self):
if len(self.transaction_history_list) > 0:
for rows in self.transaction_history_list:
if float(rows[2]) > 0:
operator = '+'
else:
operator = ''
line = f"Date: {rows[1]} Transaction: {operator}{rows[2]} Balance:{rows[3]}"
print(line)
if len(self.transaction_history_list ) > 10:
input("Press Enter to go back to account menu")
else:
print("No transactions has been made with this account yet")
# withdraw function
def deposit(self):
amount = input("How much to deposit?: ")
try:
if float(amount) > 0:
self.account_balance += float(amount)
print(f"New balance: {self.account_balance}")
self.add_to_trans_hist(float(amount))
else:
print("Error, you must enter a positive amount")
except ValueError:
print("Error, only enter numbers and decimal points")
def withdraw(self):
while True:
amount = float(input("How much would you like to withdraw?: "))
if amount > self.account_balance:
print(f"{amount}kr is more than you have in your account: {self.account_balance}kr")
a = input("Would you like to withdraw a lesser amount? y/n: ")
if a == 'y' or a == 'Y':
continue
else:
print('Good bye!')
return False
else:
self.account_balance -= amount
print(f'\nYou withdrew {amount}kr\nYour current balance is now {self.account_balance}kr')
self.add_to_trans_hist(0 - amount)
return False
# presentera kontot; visa kontonummer, saldo, kontotyp
def print_account_info(self):
print(f"Account number: {self.account_number}")
print(f"Account type: \t{self.account_type}")
print(f"Balance:\t{self.account_balance} kr")