0% found this document useful (0 votes)
21 views

Untitled 1

The document defines functions for a staff management system using Pandas DataFrames. It includes functions to fetch, write, add, remove, update staff records from a CSV file. It also defines functions for login, resetting password, displaying a menu and welcome message. The login credentials are read and checked against a CSV file. The reset_password function allows updating a user's password in the CSV file.

Uploaded by

evryanimation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Untitled 1

The document defines functions for a staff management system using Pandas DataFrames. It includes functions to fetch, write, add, remove, update staff records from a CSV file. It also defines functions for login, resetting password, displaying a menu and welcome message. The login credentials are read and checked against a CSV file. The reset_password function allows updating a user's password in the CSV file.

Uploaded by

evryanimation
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import pandas as pd

import sys

def fetchcsv(file_path):
try:
df = pd.read_csv(file_path)
print(df)
return df
except FileNotFoundError:
print(f"File {file_path} not found.")
return pd.DataFrame()

def writecsv(df, file_path):


df.to_csv(file_path, na_rep='NULL', index=False)
def welcome_message(username):
print(f"Welcome, {username}! You have successfully logged in.")
def menu(df, file_path):
try:
df = pd.read_csv(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
sys.exit()

print('-------------------')
print(' Menu')
print('-------------------')
print('1. To Add new Staff')
print('2. To Display All the staff')
print('3. To remove a record')
print('4. Update Record')
print('5. Search a staff member')
print('6. View Welcome Message') # New option to view the welcome message
print("7. Logout")
print("8. Reset Password")
print('10. Exit')
x = int(input("Enter your choice:"))

if x == 1:
addrecord(df, file_path)
elif x == 2:
df = fetchcsv(file_path)
elif x==3:
removerecord(df, file_path)
elif x==4:
updaterecord(df, file_path)
elif x==5:
searchrecord(df)
elif x == 6:
view_welcome_message()
elif x == 7:
df = logout()
elif x == 8:
reset_password(df, file_path)
else:
sys.exit()

menu(df, file_path)

def logout():
print("Logging out...")
# Add any additional logout logic if needed
print("Logout successful.")
return pd.DataFrame()
def view_welcome_message():
username = input("Enter your username to view the welcome message: ")
welcome_message(username)
def searchrecord(df):
emp_id_to_search = input("Enter the Emp_ID to search:")

# Check if Emp_ID exists in the DataFrame


if (df['Emp_ID'] == int(emp_id_to_search)).any():
# Display information for the employee with the specified Emp_ID
employee_info = df[df['Emp_ID'] == int(emp_id_to_search)].to_string(index=False)
print(f"\nEmployee Information for Emp_ID {emp_id_to_search}:\n")
print(employee_info)
else:
print(f"\nEmployee with Emp_ID '{emp_id_to_search}' not found.")

def addrecord(df, file_path):


try:
df = pd.read_csv(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
sys.exit()

print(df)
Empid=int(input("Enter your ID:"))
Name = input("Enter your name:")
Age = int(input("Enter your age:"))
Department = input("Enter the name of your Department:")
Salary = int(input("Enter your Salary:"))

# Create a new DataFrame for the new employee


new_employee = pd.DataFrame([[Empid,Name, Age, Department, Salary]],
columns=['Emp_ID','Name', 'Age', 'Department', 'Salary'])

# Concatenate the new employee DataFrame to the existing DataFrame


df = pd.concat([df, new_employee], ignore_index=True)

print(df)
writecsv(df, file_path)
x = input('Press y to continue or n to go back to menu:')
if x == 'y' or x == 'Y':
addrecord(df, file_path)
else:
menu(df, file_path)

def removerecord(df, file_path):


try:
df = pd.read_csv(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
sys.exit()

print(df)
emp_id_to_remove = input("Enter the Emp_ID to remove:")

# Check if Emp_ID exists in the DataFrame


if (df['Emp_ID'] == int(emp_id_to_remove)).any():
# Remove the employee with the specified Emp_ID
df = df[df['Emp_ID'] != int(emp_id_to_remove)]
print("Employee removed successfully.")
print(df)
writecsv(df, file_path)
menu(df, file_path)
else:
print(f"Employee with Emp_ID '{emp_id_to_remove}' not found.")

def updaterecord(df, file_path):


try:
df = pd.read_csv(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
sys.exit()

print(df)
emp_id_to_update = input("Enter the Emp_ID to update:")

# Check if Emp_ID exists in the DataFrame


if (df['Emp_ID'] == int(emp_id_to_update)).any():
# Get the index of the employee with the specified Emp_ID
index_to_update = df.index[df['Emp_ID'] == int(emp_id_to_update)].tolist()[0]

# Allow the user to update information


df.at[index_to_update, 'Name'] = input("Enter updated name:")
df.at[index_to_update, 'Age'] = int(input("Enter updated age:"))
df.at[index_to_update, 'Department'] = input("Enter updated department:")
df.at[index_to_update, 'Salary'] = int(input("Enter updated salary:"))

print("Employee information updated successfully.")


print(df)
writecsv(df, file_path)
menu(df, file_path)
else:
print(f"Employee with Emp_ID '{emp_id_to_update}' not found.")
# Update the login() function
def login(file_path):
un = input("Enter your username:")
pw = int(input("Enter your password:"))

try:
df = pd.read_csv(file_path, encoding='utf-8')
except FileNotFoundError:
print(f"Login file {file_path} not found.")
return pd.DataFrame()

df = df.loc[df["Username"] == un]

if df.empty:
print("Invalid Username")
return pd.DataFrame()
else:
df = df.loc[df["Password"] == pw]
if df.empty:
print("Invalid Password")
return pd.DataFrame()
else:
print("Login Successful")
print("----------------------------------")
print("Unacco School Khongman")
print("----------------------------------")
print("Created by:")
print("Mangaleiba and Rajlakshmi")
return df

# Update the reset_password() function


def reset_password(file_path):
try:
df = pd.read_csv(file_path)
except FileNotFoundError:
print(f"File {file_path} not found.")
sys.exit()

print(df)
username_to_reset = input("Enter the username to reset password: ")

# Check if 'Username' column exists in the DataFrame


if 'Username' not in df.columns:
print("Error: 'Username' column not found in the DataFrame.")
sys.exit()

# Check if the specified username exists in the DataFrame


if (df['Username'] == username_to_reset).any():
# Get the index of the user with the specified username
index_to_reset = df.index[df['Username'] == username_to_reset].tolist()[0]

# Allow the user to reset the password


new_password = input("Enter the new password: ")
df.at[index_to_reset, 'Password'] = new_password

print("Password reset successfully.")


print(df)
writecsv(df, file_path)
login(file_path) # Redirect to login after resetting the password
else:
print(f"User with Username '{username_to_reset}' not found.")
file_path = 'C:/Users/Hi/Login.csv' # Update this with the correct path to 'Login.csv'
existing_df = pd.DataFrame() # Assuming you have an existing DataFrame
reset_password(file_path)

import pandas as pd
data={'Emp_ID':[1,2,3,4,5,],
'Name':['Amy','Raj','Mangal','Zukov','Ray'],
'Age':[21,22,23,24,25],
'Department':['Salesmen','Analyst','Manager','Supervisor','Guard'],
'Salary':[15000,20000,25000,30000,15000]}
df=pd.DataFrame(data)
df.to_csv("Hotel_Staff.csv")

You might also like