-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
148 lines (118 loc) · 4.01 KB
/
main.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
from database import DatabaseManager
from database import userHandler
import dotenv
import os
connection = dotenv.get_key(".env", "MONGODB_ADDRESS")
client = DatabaseManager.initialize(connection)
MENU_OPTIONS = {
"menu": ["Login", "Register"],
"dashboard": ["Transfer", "Deposit", "Withdraw", "Change PIN", "Exit"],
}
def menu():
os.system("cls")
print("\nVeonise Minibank\n")
for i, option in enumerate(MENU_OPTIONS["menu"], start=1):
print(f"{i}. {option}")
choice = int(input("Enter your choice (1/2): "))
choiceHandler(choice, "menu")
def choiceHandler(choice: int, loc: str, user=None):
match loc:
case "dashboard":
match choice:
case 1:
userTransfer(user)
case 2:
userDeposit(user)
case 3:
userWithdraw(user)
case 4:
userChangePIN(user)
case 5:
print("Exiting dashboard...")
return
case "menu":
match choice:
case 1:
user = loginPrompt()
if user:
userDashboard(user)
case 2:
user = register_prompt()
if user:
userDashboard(user)
def loginPrompt():
while True:
os.system("cls")
print("Login\n")
user = input("Enter your name: ")
pin = int(input("Enter your pin: "))
if userHandler.validateUser(user, pin) == 1:
print(f"Welcome, {user}!")
return user
else:
print("Wrong username or PIN!")
def register_prompt():
while True:
os.system("cls")
print("\nRegister")
user = input("Enter your name: ")
pin = int(input("Enter your pin: "))
confirm_pin = int(input("Confirm your pin: "))
if pin != confirm_pin:
print("PINs do not match! Please try again.")
elif pin < 1000 or pin > 9999:
print("PINs should be 4 digits")
else:
userHandler.create_user(user, pin)
print("Registration successful!")
return user
def userDashboard(user):
choice = 0
while choice != 5:
os.system("cls")
print("\nDashboard Menu")
print("Current Balance:", userHandler.get_user(user)["money"])
for i, option in enumerate(MENU_OPTIONS["dashboard"], start=1):
print(f"{i}. {option}")
choice = int(input("Menu (1/2/3/4/5): "))
choiceHandler(choice, "dashboard", user)
def userDeposit(user):
os.system("cls")
print("\nDeposit\n")
amount = int(input("Amount to deposit: Rp. "))
print(f"Successfully deposited Rp. {amount}.")
def userWithdraw(user):
os.system("cls")
print("\nWithdraw\n")
amount = int(input("Amount to withdraw: Rp. "))
if DatabaseManager.withdraw(user, amount):
print(f"Successfully withdrew Rp. {amount}.")
else:
print("Withdrawal failed.")
def userChangePIN(user):
os.system("cls")
print("\nChange PIN\n")
current_pin = userHandler.get_user(user)["pin"]
print(
"Current PIN:", current_pin
) # Do not display the current PIN for security reasons
while True:
newPIN = int(input("New PIN: "))
confirmation = int(input("Confirm New PIN: "))
if newPIN != confirmation:
print("PINs do not match! Please try again.")
else:
DatabaseManager.changePIN(user, newPIN)
print("PIN changed successfully!")
break
def userTransfer(user):
os.system("cls")
print("\nTransfer")
transferTo = input("Enter recipient's name (must match exactly): ")
amount = int(input("Amount to transfer: Rp. "))
DatabaseManager.transfer(user, transferTo, amount)
if __name__ == "__main__":
try:
menu()
except KeyboardInterrupt:
print("\nUh oh! You seem intended to stop the program. Goodbye!")