-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_demo.py
More file actions
92 lines (80 loc) · 2.87 KB
/
Copy pathjson_demo.py
File metadata and controls
92 lines (80 loc) · 2.87 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
import json
import os
class User:
def __init__(self, username, password, email):
self.username = username
self.password = password
self.email = email
class UserRepository:
def __init__(self):
self.users = []
self.isLoggedIn = False
self.currentUser = {}
# load from .json file
self.loadUsers()
def loadUsers(self):
if os.path.exists("users.json"):
with open("users.json", "r", encoding="utf-8") as file:
users = json.load(file)
for user in users:
user = json.loads(user)
newUser = User(username=user["username"], password=user["password"], email=user["email"])
self.users.append(newUser)
def register(self, user: User):
self.users.append(user)
self.savetoFile()
print("User has been created.")
def login(self, username, password):
for user in self.users:
if user.username == username and user.password == password:
self.isLoggedIn = True
self.currentUser = user
else:
self.isLoggedIn = False
if self.isLoggedIn == True:
print("Succesfully Log in!")
else:
print("Failed to Log in!")
def logout(self):
self.isLoggedIn = False
self.currentUser = {}
print("Successfully Log out!")
def identity(self):
if self.isLoggedIn == True:
print(
f"1-) Username:{self.currentUser.username} \n2-)Password: {self.currentUser.password}\n3-)Email: {self.currentUser.email}")
else:
print("Not Logged In!")
def savetoFile(self):
list = []
for user in self.users:
list.append(json.dumps(user.__dict__))
with open("users.json", "w") as file:
json.dump(list, file)
repository = UserRepository()
while True:
print('Menu'.center(50, '*'))
choose = input("1- Register\n2- Login\n 3- Logout\n4- Identity \n5- Exit\nPlease choose 1-5: ")
if choose == "5":
print("Have a good day".center(50, "*"))
break
elif choose == "1":
# Register
username = input("Username: ")
password = input("Password: ")
email = input("Email: ")
user = User(username=username, password=password, email=email)
repository.register(user)
elif choose == "2":
username = input("Username: ")
password = input("Password: ")
repository.login(username=username, password=password)
pass
elif choose == "3":
repository.logout()
elif choose == "4":
# Identity
print("Current User".center(50, "*"))
repository.identity()
else:
print("Wrong choose.")