# Employee attendance management
import datetime
import hashlib
import os
def hash_password(password):
"""رمز را هش میکند"""
return hashlib.sha256(password.encode()).hexdigest()
while True:
print("1.admin 2.employee 3.exit\n")
choice = input("your choice: ")
if choice == "1":
username = input("Username: ")
password = input("Password: ")
if username == "admin" and password == "12345":
print("\nadmin login")
while True:
print("\n1. Add employee 2. Exit")
admin_choice = input("your choice: ")
if admin_choice == "1":
employee_name = input("Name: ")
employee_username = input("Username: ")
employee_password = input("Password: ")
username_exists = False
if os.path.exists("employees.txt"):
with open("employees.txt", "r") as e:
for line in e:
parts = line.strip().split("|")
if len(parts) == 3 and parts[1] == employee_username:
username_exists = True
break
if username_exists:
print("This username already exists!")
else:
hashed_pass = hash_password(employee_password)
with open("employees.txt", "a") as e:
e.write(f"{employee_name}|{employee_username}|{hashed_pass}\n")
print("Employee added successfully")
elif admin_choice == "2":
print("Exiting admin panel...")
break
else:
print("Invalid choice")
else:
print("Wrong username or password, try again.")
elif choice == "2":
username = input("Username: ")
password = input("Password: ")
found = False
if os.path.exists("employees.txt"):
with open("employees.txt", "r") as e:
for line in e:
parts = line.strip().split("|")
if len(parts) == 3:
name, user, hashed_pass = parts
if username == user and hash_password(password) == hashed_pass:
found = True
break
if found:
now = datetime.datetime.now()
time_str = now.strftime("%Y-%m-%d %H:%M:%S")
with open("attendance.txt", "a") as a:
a.write(f"{username}|{time_str}\n")
print("Attendance was recorded.")
else:
print("Wrong username or password")
elif choice == "3":
print("You have exited the program.")
break
else:
print("Invalid entered value.")