-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank.py
More file actions
140 lines (115 loc) · 4.33 KB
/
Copy pathbank.py
File metadata and controls
140 lines (115 loc) · 4.33 KB
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
def save_data(users):
with open('registrations.txt', 'w') as file:
for user_id, (name, age, balance) in users.items():
file.write(f"{user_id},{name},{age}, {balance}\n")
def load_data():
users = {}
try:
with open('registrations.txt', 'r') as file:
for line in file:
user_id, name, age, balance = line.strip().split(',')
users[int(user_id)] = (name, int(age), float(balance))
except FileNotFoundError:
pass
return users
users = load_data()
user_id = max(users.keys(), default=0) + 1
def register():
name = input('Name: ')
age = int(input('Age: '))
balance = float(input('Initial balance: '))
return name, age, balance
def search():
user = int(input('Enter the ID you want to seacrh: '))
if users:
if user in users:
name, age, balance = users[user]
print(f'ID: {user}, name: {name}, age: {age}, balance: {balance:.2f}')
else:
print('ID not found.')
else:
print('No data found.')
def exclude():
user = int(input('Insira o ID que deseja exclude: '))
if user in users:
del users[user]
save_data(users)
print(f'User with the ID {user} excluded succesfully.')
else:
print('User not found')
def transfer():
source = int(input('Enter the ID of the source user: '))
if source not in users:
print("Source ID not found.")
return
target = int(input('Enter the ID of the target user: '))
if target not in users:
print("Target ID not found.")
return
valor = float(input('Enter the amount you want to transfer: '))
balance_source = users[source][2]
balance_target = users[target][2]
if valor > balance_source:
print('Insufficient funds!')
else:
users[source] = (users[source][0], users[source][1], balance_source - valor)
users[target] = (users[target][0], users[target][1], balance_target + valor)
print(f"Transfer completed: From ID {source} to ID {target} with amount US${valor:.2f}")
with open('transfers.txt', 'a') as file:
file.write(f"Source: {source}, target: {target}, amount: {valor:.2f}\n")
save_data(users)
def extract():
user = int(input('Enter the ID of an user to see its extract: '))
if user in users:
try:
with open('transfers.txt', 'r') as file:
print(f"ID {user} transfer extract:")
print("Source, target, amount")
found_transactions = False
for line in file:
source, target, quantia = line.strip().split(", ")
if int(source.split(": ")[1]) == user or int(target.split(": ")[1]) == user:
print(line.strip())
found_transactions = True
if not found_transactions:
print("No transactions found.")
except FileNotFoundError:
print("No transactions found.")
else:
print(f"User with id {user} not found.")
def default():
print('Error! Please choose a valid option.\n')
return menu()
switcher = {
1: register,
2: search,
3: exclude,
4: transfer,
5: extract
}
def switch(case):
func = switcher.get(case, default)
return func()
def menu():
option = int(input("(1) Register \n(2) Search \n(3) Exclude \n(4) Transfer\n(5) Extract\nOption: "))
return switch(option)
def again():
while True:
opc = int(input('Would you like to execute the program again?\n(1) Yes\t(2) No\nOption: '))
if opc == 1:
return True # Continues the program
elif opc == 2:
print('Thank you for using our app!\nClosing app...')
return False # Exits the loop and program
else:
print('Error! Please choose a valid option.')
def main():
while True:
result = menu()
if isinstance(result, tuple):
name, age, balance = result
users[user_id] = (name, age, balance)
save_data(users)
if not again():
break
main()