-
Notifications
You must be signed in to change notification settings - Fork 1
/
main1.py
200 lines (167 loc) · 6.27 KB
/
main1.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Function to read user data from the users.txt file
def read_user_data():
user_data = {}
with open("users.txt", "r") as file:
for line in file:
user_id, username, password, user_type = line.strip().split(',')
user_data[username] = {
'user_id': user_id,
'password': password,
'user_type': user_type,
}
return user_data
# Function to write user data back to the users.txt file
def write_user_data(user_data):
with open("users.txt", "w") as file:
for username, details in user_data.items():
user_id = details['user_id']
password = details['password']
user_type = details['user_type']
file.write(f"{user_id},{username},{password},{user_type}\n")
# Function to authenticate a user
def authenticate_user(user_data, username, password):
if username in user_data and user_data[username]['password'] == password:
return user_data[username]
return None
# Function for admin to create a new user
def create_user(user_data):
user_id = str(len(user_data) + 1)
username = input("Enter the new username: ")
password = input("Enter the new password: ")
user_type = input("Enter the user type (admin or staff): ")
if user_type not in ['admin', 'staff']:
print("Invalid user type. User not created.")
return
user_data[username] = {
'user_id': user_id,
'password': password,
'user_type': user_type,
}
print("User created successfully!")
# Function for admin to modify a user's details
def modify_user(user_data):
username = input("Enter the username to modify: ")
if username in user_data:
user = user_data[username]
password = input("Enter the new password (leave blank to keep the current password): ")
user_type = input("Enter the new user type (admin or staff): ")
new_username = input("Enter the new username (leave blank to keep the current username): ")
if user_type not in ['admin', 'staff']:
print("Invalid user type. User not modified.")
return
if password:
user['password'] = password
if user_type:
user['user_type'] = user_type
if new_username and new_username != username:
user_data[new_username] = user
del user_data[username]
write_user_data(user_data)
print(f"User details modified successfully.")
else:
print("User not found.")
# Function for admin to search for a user
def search_user(user_data):
username = input("Enter the username to search: ")
if username in user_data:
print(f"User ID: {user_data[username]['user_id']}")
print(f"User Type: {user_data[username]['user_type']}")
else:
print("User not found.")
# Function to delete a user
def delete_user(user_data, username):
if username in user_data:
del user_data[username]
write_user_data(user_data)
print(f"{username} has been deleted.")
else:
print("User not found.")
# Function to display all users
def display_users():
with open("users.txt", "r") as file:
lines = file.readlines()
for line in lines:
user_id, username, _, user_type = line.strip().split(',')
print(f"User ID: {user_id}, Username: {username}, User Type: {user_type}")
# Function for user login
def login(user_data):
while True:
print("\nLogin System")
username = input("Enter your username: ")
password = input("Enter your password: ")
user = authenticate_user(user_data, username, password)
if user:
print(f"Welcome, {username} ({user['user_type']})!")
if user['user_type'] == 'admin':
admin_menu(user_data)
else:
staff_menu(user_data)
user_continue = input("Do you want to continue (Y/N)? ").strip().lower()
if user_continue != 'y':
break
else:
print("Invalid username or password. Please try again.")
# Function for admin menu
def admin_menu(user_data):
while True:
print("\nUser Management Menu:")
print("1. Create User")
print("2. Modify User")
print("3. Search User")
print("4. Delete User")
print("5. Display All Users")
print("6. Back to Main Menu")
choice = input("Enter your choice: ")
if choice == "1":
create_user(user_data)
elif choice == "2":
modify_user(user_data)
elif choice == "3":
search_user(user_data)
elif choice == "4":
username = input("Enter the username to delete: ")
delete_user(user_data, username)
elif choice == "5":
display_users()
elif choice == "6":
print("Returning to the main menu.")
break
else:
print("Invalid choice. Please try again.")
# Function for staff menu
def staff_menu(user_data):
while True:
print("\nStaff Menu:")
print("1. Search User")
print("2. Display All Users")
print("3. Logout")
choice = input("Enter your choice: ")
if choice == "1":
search_user(user_data)
elif choice == "2":
display_users()
elif choice == "3":
write_user_data(user_data)
print("Logged out.")
break
else:
print("Invalid choice. Please try again.")
# User management system
if __name__ == "__main__":
user_data = read_user_data()
while True: # This outer loop keeps the program running
print("\nLogin System")
username = input("Enter your username: ")
password = input("Enter your password: ")
user = authenticate_user(user_data, username, password)
if user:
print(f"Welcome, {username} ({user['user_type']})!")
if user['user_type'] == 'admin':
admin_menu(user_data)
else:
staff_menu(user_data)
user_continue = input("Do you want to continue (Y/N)? ").strip().lower()
if user_continue != 'y':
break
else:
print("Invalid username or password. Please try again.")