@@ -58,11 +58,22 @@ class BankApp:
58
58
''' )
59
59
self .conn.commit()
60
60
61
- def create_account (self , username , password ):
61
+ def user_already_exist (self ):
62
+ username = input (" Enter a username: " )
63
+ self .cursor.execute(" SELECT username FROM accounts WHERE username = ?" , (username))
64
+ account = self .cursor.fetchone()
65
+ if account:
66
+ print (" Username already in use, use another new one" )
67
+ self .user_already_exist()
68
+ else :
69
+ return username
70
+
71
+ def create_account (self ,username ,password ):
72
+
62
73
hashed_password = hashlib.sha256(password.encode()).hexdigest()
63
- self .cursor.execute(" INSERT INTO accounts (username, password, balance) VALUES (?, ?, ?)" ,
64
- (username, hashed_password, 0.0 ))
74
+ self .cursor.execute(" INSERT INTO accounts (username, password, balance) VALUES (?,?,?)" ,(username,hashed_password,0.0 ))
65
75
self .conn.commit()
76
+ print (" Account created successfully." )
66
77
67
78
def login (self , username , password ):
68
79
hashed_password = hashlib.sha256(password.encode()).hexdigest()
@@ -119,10 +130,9 @@ def main():
119
130
choice = input (" Select an operation: " )
120
131
121
132
if choice == " 1" :
122
- username = input ( " Enter a username: " )
133
+ username = bank_app.user_already_exist( )
123
134
password = input (" Enter a password: " )
124
135
bank_app.create_account(username, password)
125
- print (" Account created successfully." )
126
136
127
137
elif choice == " 2" :
128
138
username = input (" Enter your username: " )
0 commit comments