مدیر سیستم باید بتواند پس از وارد کردن نام کاربری admin و پسورد 12345، وارد شود و کارمند جدید اضافه کند و برای هر کارمند نام کاربری و رمزعبور تعریف کند. برنامه باید این اطلاعات را درون یک فایل به اسم دلخواه ذخیره کند.
کارمندان هم باید بتوانند با وارد کردن نام کاربری و رمز خود، وارد شوند و حضور خود را ثبت کنند. زمان دقیقی که کارمندان حضور خود را ثبت کرده اند باید در فایلی ذخیره شود.
این تمرین یه پروژه ست! یعنی پتانسیل اینو داره که خیلی بیشتر روش کار بشه بنابراین ایده اصلی رو از اینجا
بردار و خودت بیشتر روش کار کن تا به یه تمرین بزرگتر تبدیل یشه
👨💻 2 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد
reply 21
visibility
import datetime
# تابع برای ذخیره اطلاعات کارمندان در فایل
def save_employee_data(user_data):
with open('employees.txt', 'a') as file:
for username, password in user_data.items():
file.write(f"{username},{password}\n")
# تابع برای ثبت حضور کارمند
def record_attendance(username):
with open('attendance.txt', 'a') as file:
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file.write(f"{username} - {timestamp}\n")
# تابع برای ورود مدیر
def admin_login():
username = input("نام کاربری: ")
password = input("رمز عبور: ")
return username == "admin" and password == "12345"
# تابع برای افزودن کارمند جدید
def add_employee(user_data):
username = input("نام کاربری کارمند جدید: ")
password = input("رمز عبور کارمند جدید: ")
user_data[username] = password
save_employee_data(user_data)
print("کارمند جدید با موفقیت اضافه شد.")
# تابع برای ورود کارمند
def employee_login(user_data):
username = input("نام کاربری: ")
password = input("رمز عبور: ")
if username in user_data and user_data[username] == password:
print("ورود با موفقیت انجام شد.")
record_attendance(username)
print("حضور شما ثبت شد.")
else:
print("نام کاربری یا رمز عبور نادرست است.")
# تابع اصلی
def main():
user_data = {}
# بارگذاری اطلاعات کارمندان از فایل
try:
with open('employees.txt', 'r') as file:
for line in file:
username, password = line.strip().split(',')
user_data[username] = password
except FileNotFoundError:
print("فایل کارمندان پیدا نشد. یک کارمند جدید اضافه کنید.")
# ورود مدیر یا کارمند
while True:
role = input("آیا شما مدیر (m) هستید یا کارمند (e)؟ (خروج (q) برای پایان): ").lower()
if role == 'm':
if admin_login():
while True:
add_employee(user_data)
continue_adding = input("آیا کارمند دیگری اضافه کنید؟ (بله: y / خیر: n): ").lower()
if continue_adding != 'y':
break
else:
print("نام کاربری یا رمز عبور مدیر نادرست است.")
elif role == 'e':
employee_login(user_data)
elif role == 'q':
print("خروج از برنامه.")
break
else:
print("ورودی نامعتبر است. لطفاً دوباره تلاش کنید.")
# اجرای برنامه
if __name__ == "__main__":
main()
from datetime import datetime
import os
employee_file = 'employee.txt'
attendance_file = 'attendance.txt'
# ساخت فایلها در صورت نبودن
if not os.path.exists(employee_file):
open(employee_file, 'w').close()
if not os.path.exists(attendance_file):
open(attendance_file, 'w').close()
print('Welcome!')
username = input('Enter username: ')
password = input('Enter password: ')
if username == 'admin' and password == '12345':
print("\n Welcome Admin!")
print("1. Add new employee")
print("2. View attendance list")
print("3. Remove employee")
choice = input("Enter your choice (1, 2 or 3): ")
if choice == '1':
new_user_name = input("Enter employee's username: ")
new_pass_word = input("Enter employee's password: ")
with open(employee_file, 'a') as f:
f.write(f"{new_user_name},{new_pass_word}\n")
print(f" Employee '{new_user_name}' added successfully.")
elif choice == '2':
with open(attendance_file, 'r') as f:
data = f.read()
if data.strip() == "":
print("No attendance records found.")
else:
print("\n--- Attendance List ---")
print(data)
elif choice == '3':
remove_name = input("Enter employee username to remove: ")
with open(employee_file, 'r') as f:
employees = f.readlines()
found = False
with open(employee_file, 'w') as f:
for emp in employees:
emp_username, emp_password = emp.strip().split(',')
if emp_username != remove_name:
f.write(emp)
else:
found = True
if found:
print(" Employee removed successfully.")
else:
print(" Employee not found.")
else:
print(" Invalid choice.")
else:
found = False
with open(employee_file, 'r') as f:
employees = f.readlines()
for emp in employees:
emp_username, emp_password = emp.strip().split(',')
if username == emp_username and password == emp_password:
found = True
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(attendance_file, 'a') as f:
f.write(f"{username} - {now}\n")
print(f" Attendance recorded at {now}")
break
if not found:
print(" Invalid username or password.")
import os
from datetime import datetime
import pickle
fail_karmandan='nam karmandan.pcl'
fail_huzur='huzur.pkl'
if not os.path.exists(fail_karmandan):
with open(fail_karmandan,'wb') as f:
pickle.dump({},f)
if not os.path.exists(fail_huzur):
with open(fail_huzur,'wb') as f:
pickle.dump({},f)
class Modir:
def __init__(self,nam,pasword):
self.nam=nam
self.pasword=pasword
def afzudane_karmand(self,nam_karmand,pasword_karmand):
with open(fail_karmandan,'rb')as f:
karmandan=pickle.load(f)
if nam_karmand in karmandan:
print('gablan sabt shode')
else:
karmandan[nam_karmand]=pasword_karmand
with open(fail_karmandan,'wb')as f:
pickle.dump(karmandan,f)
print(f'karbar{nam_karmand}be anjam resid.')
class Karmand:
def __init__(self,nam_karbari) :
self.nam_karbari=nam_karbari
def sabte_huzur(self):
with open(fail_huzur,'rb')as f:
huzur=pickle.load(f)
zamane_huzur=datetime.now().strftime('%Y-%m-%d%h:%M:%S')
if self.nam_karbari not in huzur:
huzur[self.nam_karbari]=[]
huzur[self.nam_karbari].append(zamane_huzur)
with open(fail_huzur,'wb') as f:
pickle.dump(huzur,f)
print(f'huzure shoma dar tarikhe {zamane_huzur} sabt shod.')
def vurud():
nam_karbari=input('name karbari ra vared konid:')
ramze_ubur=input('ramze ubur ra vared konid:')
if nam_karbari=='admin'and ramze_ubur=='12345':
return Modir(nam_karbari,ramze_ubur)
else:
with open( fail_karmandan,'rb') as f:
karmandan=pickle.load(f)
if nam_karbari in karmandan and karmandan[nam_karbari]==ramze_ubur:
return Karmand(nam_karbari)
else:
print('name karbari ya ramze ubur eshtebah ast.')
return None
def meno():
karbar=vurud()
if isinstance(karbar,Modir):
print('khos amadid modir!')
while 1:
print('1.ezafe kardane karmand')
print('2.khuruj')
gozine=input('entekhab konid:')
if gozine=='1':
name_karmand=input('name karbari karmand:')
karmand_pasword=input('pasworde karmand:')
karbar.afzudane_karmand(name_karmand,karmand_pasword)
elif isinstance(karbar,Karmand):
print(f'khosh amadid {karbar.nam_karbari}!')
karbar.sabte_huzur()
elif gozine=='2':
break
else:
print('entekhab na motabar ast')
if __name__=='__main__':
meno()
import pickle
import datetime import datetime
def admin_fun():
admin_username = "admin"
admin_password = "12345"
# Admin login
username = input("Enter admin username: ")
password = input("Enter admin password: ")
if username == admin_username and password == admin_password:
print("Access granted. You can add employees.")
else:
print("Access denied.")
return None
# Initialize an empty dictionary to store employee data
employee_data = {}
# Loop to collect usernames and passwords
while True:
username = input("Enter a new username for employee (or type 'exit' to finish): ")
if username.lower() == 'exit':
break
password = input(f'Enter a password for username {username}: ')
employee_data[username] = password
# Serialize the dictionary using pickle
b_employee_data = pickle.dumps(employee_data)
database = "employee_data.pkl"
# Write the serialized dictionary to a file
with open(database, "wb") as file:
file.write(b_employee_data)
print("Employee data saved successfully.")
return database
def employee_fun(database):
# Read the serialized dictionary from the file
try:
with open(database, "rb") as file:
b_employee_data = file.read()
employee_data = pickle.loads(b_employee_data) # Deserialize the data
except FileNotFoundError:
print("No employee data found, please contact the admin.")
return
# Input for username and password
username = input("Enter your username: ")
password = input("Enter your password: ")
# Check if the username exists in the dictionary
if username in employee_data:
# Compare the given password with the stored password
if employee_data[username] == password:
print("Login successful.")
# Record attendance with timestamp
attendance_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with open("attendance_log.txt", "a") as log_file:
log_file.write(f"{username} logged in at {attendance_time}\n")
print("Attendance recorded.")
else:
print("Incorrect password.")
else:
print("Username not found.")