0% found this document useful (0 votes)
6 views9 pages

Ayawqna

The PayrollSystem is a Java application that manages employee data, including login functionality for both admin and employee users. It allows for operations such as adding, editing, searching, and deleting employee records, as well as generating payslips and changing passwords. The system is structured with classes for Employee, User, and an enumeration for user roles, facilitating a clear separation of concerns.

Uploaded by

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

Ayawqna

The PayrollSystem is a Java application that manages employee data, including login functionality for both admin and employee users. It allows for operations such as adding, editing, searching, and deleting employee records, as well as generating payslips and changing passwords. The system is structured with classes for Employee, User, and an enumeration for user roles, facilitating a clear separation of concerns.

Uploaded by

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

import java.util.

HashMap;
import java.util.Map;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PayrollSystem {


private static String adminPassword = "password"; // Default password
private static final int WORK_HOURS_PER_DAY = 8;

private static Map<String, Employee> employees = new HashMap<>();


private static Map<String, User> users = new HashMap<>();

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
Date currentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

System.out.println("\t\t\t\t=============================");
System.out.println("\t\t\t\t WELCOME TO PAYROLL SYSTEM ");
System.out.println("\t\t\t\t=============================");
System.out.println("\t\t\tDate and Time: " +
dateFormat.format(currentDate));
System.out.println();

// Adding admin user


users.put("admin", new User("admin", "password", null, Role.ADMIN));

int attempts = 5;

while (attempts > 0) {


System.out.print("Enter Username: ");
String username = scanner.nextLine();

System.out.print("Enter Password: ");


String password = scanner.nextLine();

User user = authenticate(username, password);

if (user != null) {
System.out.println("Login successful!");

if (user.getRole() == Role.ADMIN) {
displayAdminMenu(scanner);
} else {
displayEmployeeMenu(scanner, user.getUsername());
}
break;
} else {
System.out.println("Invalid username or password. Please try
again.");
attempts--;
System.out.println("You have " + attempts + " attempts left.");
}
}

if (attempts == 0) {
System.out.println("You have exceeded the maximum number of login
attempts. Please try again later.");
}
}

private static User authenticate(String username, String password) {


if (users.containsKey(username) &&
users.get(username).getPassword().equals(password)) {
return users.get(username);
}
return null;
}

private static void displayAdminMenu(Scanner scanner) {


while (true) {
System.out.println("\t\t\t======================");
System.out.println("\t\t\t MAIN MENU ");
System.out.println("\t\t\t======================\n");
System.out.println("1. EMPLOYEE DATA");
System.out.println("2. LOG SYSTEM");
System.out.println("3. GET PAYSLIP");
System.out.println("4. CHANGE PASSWORD");
System.out.println("5. LOG OUT");
System.out.println("6. REGISTER NEW EMPLOYEE");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline character

switch (choice) {
case 1:
employeeDataMenu(scanner);
break;
case 2:
logSystem();
break;
case 3:
getPayslip(scanner);
break;
case 4:
changePassword(scanner);
break;
case 5:
logOut(scanner);
return;
case 6:
registerEmployee(scanner);
break;
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 6.");
}
}
}

private static void displayEmployeeMenu(Scanner scanner, String username) {


while (true) {
System.out.println("\t\t\t======================");
System.out.println("\t\t\t EMPLOYEE MENU ");
System.out.println("\t\t\t======================\n");
System.out.println("1. VIEW YOUR DATA");
System.out.println("2. GET YOUR PAYSLIP");
System.out.println("3. LOG OUT");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline character

switch (choice) {
case 1:
viewEmployeeData(username);
break;
case 2:
getPayslipForEmployee(scanner, username);
break;
case 3:
logOut(scanner);
return;
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 3.");
}
}
}

private static void changePassword(Scanner scanner) {


System.out.print("Enter current password: ");
String currentPassword = scanner.nextLine();

if (!currentPassword.equals(adminPassword)) {
System.out.println("Incorrect password. Password change failed.");
return;
}

System.out.print("Enter new password: ");


String newPassword = scanner.nextLine();

adminPassword = newPassword;
System.out.println("Password changed successfully!");
}

private static void employeeDataMenu(Scanner scanner) {


int choice;

do {
System.out.println("\t\t\t======================");
System.out.println("\t\t\t EMPLOYEE DATA ");
System.out.println("\t\t\t======================\n");
System.out.println("1. Add Data");
System.out.println("2. Edit Data");
System.out.println("3. Search Data");
System.out.println("4. Delete Data");
System.out.println("5. Back to Main Menu");
System.out.print("Enter your choice: ");

choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character

switch (choice) {
case 1:
addEmployeeData(scanner);
break;
case 2:
editEmployeeData(scanner);
break;
case 3:
searchEmployeeData(scanner);
break;
case 4:
deleteEmployeeData(scanner);
break;
case 5:
System.out.println("Returning to Main Menu...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 5);
}

private static void addEmployeeData(Scanner scanner) {


System.out.println("\nAdding Employee Data:");
System.out.print("Enter Full Name: ");
String fullName = scanner.nextLine();
System.out.print("Enter Employee ID: ");
String employeeID = scanner.nextLine();
System.out.print("Enter Address: ");
String address = scanner.nextLine();
System.out.print("Enter Phone Number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter Salary: ");
double salary = scanner.nextDouble();
System.out.print("Enter Total Hours Worked: ");
int totalHoursWorked = scanner.nextInt();
scanner.nextLine(); // Consume newline character

employees.put(employeeID, new Employee(fullName, employeeID, address,


phoneNumber, salary, totalHoursWorked));
System.out.println("Employee data added successfully!");
}

private static void editEmployeeData(Scanner scanner) {


System.out.println("\nEditing Employee Data:");
System.out.print("Enter Employee ID to edit: ");
String employeeID = scanner.nextLine();

if (employees.containsKey(employeeID)) {
Employee employee = employees.get(employeeID);
System.out.println("Editing employee with ID: " + employeeID);
System.out.print("Enter new Full Name: ");
String fullName = scanner.nextLine();
System.out.print("Enter new Address: ");
String address = scanner.nextLine();
System.out.print("Enter new Phone Number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter new Salary: ");
double salary = scanner.nextDouble();
System.out.print("Enter new Total Hours Worked: ");
int totalHoursWorked = scanner.nextInt();
scanner.nextLine(); // Consume newline character

employee.setFullName(fullName);
employee.setAddress(address);
employee.setPhoneNumber(phoneNumber);
employee.setSalary(salary);
employee.setTotalHoursWorked(totalHoursWorked);

System.out.println("Employee data updated successfully!");


} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void searchEmployeeData(Scanner scanner) {


System.out.println("\nSearching Employee Data:");
System.out.print("Enter Employee ID to search: ");
String employeeID = scanner.nextLine();

if (employees.containsKey(employeeID)) {
Employee employee = employees.get(employeeID);
System.out.println("Employee ID: " + employeeID);
System.out.println("Full Name: " + employee.getFullName());
System.out.println("Address: " + employee.getAddress());
System.out.println("Phone Number: " + employee.getPhoneNumber());
System.out.println("Salary: " + employee.getSalary());
System.out.println("Total Hours Worked: " +
employee.getTotalHoursWorked());
} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void deleteEmployeeData(Scanner scanner) {


System.out.println("\nDeleting Employee Data:");
System.out.print("Enter Employee ID to delete: ");
String employeeID = scanner.nextLine();

if (employees.containsKey(employeeID)) {
employees.remove(employeeID);
System.out.println("Employee data deleted successfully!");
} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void logSystem() {


System.out.println("Displaying all employee records:");
for (Map.Entry<String, Employee> entry : employees.entrySet()) {
String employeeID = entry.getKey();
Employee employee = entry.getValue();
System.out.println("Employee ID: " + employeeID);
System.out.println("Full Name: " + employee.getFullName());
System.out.println("Address: " + employee.getAddress());
System.out.println("Phone Number: " + employee.getPhoneNumber());
System.out.println("Salary: " + employee.getSalary());
System.out.println("Total Hours Worked: " +
employee.getTotalHoursWorked());
System.out.println();
}
}

private static void getPayslip(Scanner scanner) {


System.out.print("Enter Employee ID to generate payslip: ");
String employeeID = scanner.nextLine();

if (employees.containsKey(employeeID)) {
Employee employee = employees.get(employeeID);
double basicSalary = employee.getSalary();
double sssDeduction = 180.0; // Fixed amount for SSS
double pagibigPercentage = 0.02; // 2%
double philhealthPercentage = 0.045; // 4.5%
double pagibigDeduction = basicSalary * pagibigPercentage;
double philhealthDeduction = basicSalary * philhealthPercentage;
double totalDeductions = sssDeduction + pagibigDeduction +
philhealthDeduction;
double netPay = basicSalary - totalDeductions;

System.out.println("Employee Payslip");
System.out.println("Name: " + employee.getFullName());
System.out.println("ID No.: " + employee.getEmployeeID());
System.out.println("Basic Salary: " + basicSalary);
System.out.println("Total Hours: " + WORK_HOURS_PER_DAY);
System.out.println("Deductions:");
System.out.println("SSS: " + sssDeduction);
System.out.println("PAG-IBIG Fund: " + pagibigDeduction);
System.out.println("PhilHealth: " + philhealthDeduction);
System.out.println("Net Pay: " + netPay);
} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void getPayslipForEmployee(Scanner scanner, String username) {


User user = users.get(username);
String employeeID = user.getEmployeeID();

if (employees.containsKey(employeeID)) {
Employee employee = employees.get(employeeID);
double basicSalary = employee.getSalary();
double sssDeduction = 180.0; // Fixed amount for SSS
double pagibigPercentage = 0.02; // 2%
double philhealthPercentage = 0.045; // 4.5%
double pagibigDeduction = basicSalary * pagibigPercentage;
double philhealthDeduction = basicSalary * philhealthPercentage;
double totalDeductions = sssDeduction + pagibigDeduction +
philhealthDeduction;
double netPay = basicSalary - totalDeductions;

System.out.println("Employee Payslip");
System.out.println("Name: " + employee.getFullName());
System.out.println("ID No.: " + employee.getEmployeeID());
System.out.println("Basic Salary: " + basicSalary);
System.out.println("Total Hours: " + WORK_HOURS_PER_DAY);
System.out.println("Deductions:");
System.out.println("SSS: " + sssDeduction);
System.out.println("PAG-IBIG Fund: " + pagibigDeduction);
System.out.println("PhilHealth: " + philhealthDeduction);
System.out.println("Net Pay: " + netPay);
} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void registerEmployee(Scanner scanner) {


System.out.println("\nRegister New Employee:");
System.out.print("Enter Username: ");
String username = scanner.nextLine();
System.out.print("Enter Password: ");
String password = scanner.nextLine();
System.out.print("Enter Employee ID: ");
String employeeID = scanner.nextLine();

if (employees.containsKey(employeeID)) {
users.put(username, new User(username, password, employeeID,
Role.EMPLOYEE));
System.out.println("Employee registered successfully!");
// Automatically log in the new employee
displayEmployeeMenu(scanner, username);
} else {
System.out.println("Employee ID not found. Please add the employee data
first.");
}
}

private static void viewEmployeeData(String username) {


User user = users.get(username);
String employeeID = user.getEmployeeID();

if (employees.containsKey(employeeID)) {
Employee employee = employees.get(employeeID);
System.out.println("Employee ID: " + employeeID);
System.out.println("Full Name: " + employee.getFullName());
System.out.println("Address: " + employee.getAddress());
System.out.println("Phone Number: " + employee.getPhoneNumber());
System.out.println("Salary: " + employee.getSalary());
System.out.println("Total Hours Worked: " +
employee.getTotalHoursWorked());
} else {
System.out.println("Employee ID not found. Please try again.");
}
}

private static void logOut(Scanner scanner) {


System.out.println("Logging out...");
}
}

class Employee {
private String fullName;
private String employeeID;
private String address;
private String phoneNumber;
private double salary;
private int totalHoursWorked;

public Employee(String fullName, String employeeID, String address, String


phoneNumber, double salary, int totalHoursWorked) {
this.fullName = fullName;
this.employeeID = employeeID;
this.address = address;
this.phoneNumber = phoneNumber;
this.salary = salary;
this.totalHoursWorked = totalHoursWorked;
}

public String getFullName() {


return fullName;
}

public void setFullName(String fullName) {


this.fullName = fullName;
}

public String getEmployeeID() {


return employeeID;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

public String getPhoneNumber() {


return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {


this.phoneNumber = phoneNumber;
}

public double getSalary() {


return salary;
}

public void setSalary(double salary) {


this.salary = salary;
}

public int getTotalHoursWorked() {


return totalHoursWorked;
}

public void setTotalHoursWorked(int totalHoursWorked) {


this.totalHoursWorked = totalHoursWorked;
}
}

class User {
private String username;
private String password;
private String employeeID;
private Role role;
public User(String username, String password, String employeeID, Role role) {
this.username = username;
this.password = password;
this.employeeID = employeeID;
this.role = role;
}

public String getUsername() {


return username;
}

public String getPassword() {


return password;
}

public String getEmployeeID() {


return employeeID;
}

public Role getRole() {


return role;
}
}

enum Role {
ADMIN,
EMPLOYEE
}

You might also like