-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.py
156 lines (121 loc) · 4.77 KB
/
bank.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import hashlib
class User:
def __init__(self, name, password, initial_deposit):
self.name = name
self.hashed_password = self.hash_password(password)
self.account = Account(initial_deposit)
def get_name(self):
return self.name
def get_account(self):
return self.account
def verify_password(self, password):
return self.hashed_password == self.hash_password(password)
@staticmethod
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
class Account:
def __init__(self, initial_deposit):
self.balance = initial_deposit
def get_balance(self):
return self.balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
else:
print("Deposit amount must be positive.")
def withdraw(self, amount):
if amount > 0 and self.balance >= amount:
self.balance -= amount
return True
else:
print("Insufficient funds or invalid amount.")
return False
class Bank:
def __init__(self):
self.users = {}
def create_user(self, name, password, initial_deposit):
if name not in self.users:
self.users[name] = User(name, password, initial_deposit)
print(f"User {name} created with initial deposit of {initial_deposit}")
else:
print(f"User {name} already exists.")
def get_user(self, name):
return self.users.get(name)
def deposit(self, name, amount):
user = self.get_user(name)
if user:
user.get_account().deposit(amount)
print(f"Deposited {amount} to {name}'s account. New balance: {user.get_account().get_balance()}")
else:
print(f"User {name} not found.")
def transfer(self, from_user, to_user, amount):
user_from = self.get_user(from_user)
user_to = self.get_user(to_user)
if user_from and user_to:
if user_from.get_account().withdraw(amount):
user_to.get_account().deposit(amount)
print(f"Transferred {amount} from {from_user} to {to_user}")
print(f"{from_user}'s new balance: {user_from.get_account().get_balance()}")
print(f"{to_user}'s new balance: {user_to.get_account().get_balance()}")
else:
print(f"Transfer failed. Insufficient funds in {from_user}'s account.")
else:
print("One or both users not found.")
def main():
bank = Bank()
while True:
print("\nWelcome to the Bank")
print("1. Create new account")
print("2. Login to existing account")
print("3. Exit")
choice = input("Choose an option: ")
if choice == '1':
create_new_account(bank)
elif choice == '2':
login_to_account(bank)
elif choice == '3':
print("Thank you for using the Bank. Goodbye!")
break
else:
print("Invalid choice. Please try again.")
def create_new_account(bank):
name = input("Enter your name: ")
password = input("Enter your password: ")
initial_deposit = float(input("Enter initial deposit: "))
bank.create_user(name, password, initial_deposit)
def login_to_account(bank):
name = input("Enter your name: ")
password = input("Enter your password: ")
user = bank.get_user(name)
if user and user.verify_password(password):
while True:
print(f"\nHello, {user.get_name()}")
print("1. Deposit money")
print("2. Transfer money")
print("3. Check balance")
print("4. Logout")
choice = input("Choose an option: ")
if choice == '1':
deposit_money(bank, user)
elif choice == '2':
transfer_money(bank, user)
elif choice == '3':
check_balance(user)
elif choice == '4':
print("Logging out...")
break
else:
print("Invalid choice. Please try again.")
else:
print("Invalid username or password. Please try again.")
def deposit_money(bank, user):
amount = float(input("Enter amount to deposit: "))
bank.deposit(user.get_name(), amount)
def transfer_money(bank, from_user):
to_user = input("Enter recipient's name: ")
amount = float(input("Enter amount to transfer: "))
bank.transfer(from_user.get_name(), to_user, amount)
def check_balance(user):
print(f"Your current balance is: {user.get_account().get_balance()}")
if __name__ == "__main__":
main()