-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
177 lines (149 loc) · 5.67 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import certifi
from pymongo import MongoClient
from pymongo.server_api import ServerApi
# TODO Connect to the database and get the users collection
client = MongoClient("mongodb+srv://user:pzcPKYCyH3iTJl1y@cluster0.bvru9z5.mongodb.net/?retryWrites=true&w=majority", tlsCAFile=certifi.where())
db = client["demo"]
collection = db["users"]
current_user = None
def register_user(username, password):
"""
Register the user with <username> and <password> into the database
:param username: the user's username
:param password: the user's password
:return: None
"""
# TODO implement this function
collection.insert_one({"username":username, "password": password})
def does_user_exist(username) -> bool:
"""
Checks if the <username> already exists in the database
:param username: the username to check
:return: True if it does exist, otherwise False
"""
# TODO implement this function
return bool(collection.find_one({"username": username}))
def signup():
"""
Allows a user to register an account if the username does not already exist
:return: None
"""
username = input("Please enter your username:")
while does_user_exist(username):
username = input("Sorry! This user already exists\nPlease enter your "
"username:")
password = input("Please enter your password:")
register_user(username, password)
print("User registered successfully!")
def fetch_user(username):
"""
Get the user with <username> fromt the database
:param username: username to fetch
:return: the document with the <username>
"""
# TODO fetch the user from the database
return collection.find_one({"username": username})
def signin():
"""
Signs in to the user's bank account, confirms whether their username and
password are valid
:return: None
"""
username = input("Please enter your username: ")
user = fetch_user(username)
while not user:
username = input("Sorry! This user does not exist\nPlease enter your "
"username: ")
user = fetch_user(username)
password = input(f"Please enter the password for user {username}: ")
while password != user["password"]:
password = input(f"Password incorrect! Please enter the password for "
f"user {username}: ")
global current_user
current_user = user
print(f"Welcome {username}!")
def get_valid_int_input(request: str):
"""
A helper function to get an input from a user and check if it is numeric
:param request: a string message for the requested input
:return: the valid integer input
"""
inp = input(request)
while not inp.isnumeric():
inp = input(f'Please enter a valid non-negative int!\n{request}: ')
return int(inp)
def deposit():
"""
Prompt the user to enter an amount they want to deposit to their account,
then update the user's balance in the database
:return: none
"""
global current_user
amount = get_valid_int_input("Please enter deposit amount: ")
# TODO update database
collection.update_one({"username":current_user["username"]}, {"$inc": {"balance": amount}})
current_user = fetch_user(current_user["username"])
def withdraw():
"""
Prompt the user to enter an amount they want to withdraw to their account,
then update the user's balance in the database
:return: none
"""
global current_user
amount = get_valid_int_input("Please enter withdraw amount: ")
# TODO update database
collection.update_one({"username": current_user["username"]}, {"$inc": {"balance": -amount}})
current_user = fetch_user(current_user["username"])
def signout():
"""
Signs out of the user's account
:return: none
"""
global current_user
current_user = None
user_functions = {
"deposit": deposit,
"de": deposit,
"withdraw": withdraw,
"wd": withdraw,
"signout": signout,
"so": signout
}
if __name__ == '__main__':
print("""
__ __ _____ _ ____ _
| \/ | / ____| | | | _ \ | |
| \ / |_ _ | | ___ ___ | | | |_) | __ _ _ __ | | __
| |\/| | | | | | | / _ \ / _ \| | | _ < / _` | '_ \| |/ /
| | | | |_| | | |___| (_) | (_) | | | |_) | (_| | | | | <
|_| |_|\__, | \_____\___/ \___/|_| |____/ \__,_|_| |_|_|\_\
__/ |
|___/
""")
print("Welcome my cool bank, you can make your account and manage your "
"money!\nTo exit this application at any time just type \"exit\"")
user_inp = ""
while user_inp != 'exit':
user_inp = ''
if not current_user:
user_inp = input("Would you like to sign-in (SI) or sign-up (SU)?: "
"")
if user_inp.lower() == 'si':
signin()
elif user_inp.lower() == 'su':
signup()
elif user_inp.lower() == 'exit':
break
else:
print('Please enter a valid option')
else:
print(f"Hi {current_user['username']}, your balance is "
f"${current_user['balance'] if 'balance' in current_user else 0}")
user_inp = input("Please either withdraw (WD), deposit (DE), "
"signout (SO) or exit: ")
if user_inp.lower() in user_functions:
user_functions[user_inp.lower()]()
elif user_inp.lower() == 'exit':
break
else:
print('Please enter a valid option')