Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
7.3 kB
3
Indexable
Never
import java.util.ArrayList;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

class Employee {
    int id;
    String name;
    double hourlyRate;

    public Employee(int id, String name, double hourlyRate) {
        this.id = id;
        this.name = name;
        this.hourlyRate = hourlyRate;
    }
}

public class Main {
    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        // Add employees to the list
        addEmployees(employees);

        // Validate employee ID
        int employeeId = validateEmployeeId(scanner, employees);

        // Display employee information
        displayEmployeeInformation(employeeId, employees);

        // Display Employee Timesheet
        double totalHoursWorked = calculateTotalHoursWorked(scanner);
        
        // Calculate and display salary details
        calculateAndDisplaySalary(employeeId, employees, totalHoursWorked);
    }

    // Function to add employees to the list
    private static void addEmployees(ArrayList<Employee> employees) {
        // Add employees here
    }

    // Function to validate employee ID
    private static int validateEmployeeId(Scanner scanner, ArrayList<Employee> employees) {
        int employeeId;
        boolean validEmployeeId;
        do {
            System.out.println("Enter your employee ID:");
            employeeId = scanner.nextInt();
            validEmployeeId = employees.stream().anyMatch(employee -> employee.getId() == employeeId);
            if (!validEmployeeId) {
                System.out.println("Invalid employee ID. Please try again.");
            }
        } while (!validEmployeeId);
        return employeeId;
    }

    // Function to display employee information
    private static void displayEmployeeInformation(int employeeId, ArrayList<Employee> employees) {
        for (Employee employee : employees) {
            if (employee.getId() == employeeId) {
                System.out.println("\n--------------------------------------");
                System.out.println("EMPLOYEE INFORMATION:");
                System.out.println("--------------------------------------");
                System.out.println("Employee ID: " + employee.getId());
                System.out.println("Name: " + employee.getName());
                System.out.println("Hourly Rate: Php" + employee.getHourlyRate());
                break;
            }
        }
    }

    // Function to calculate total hours worked
    private static double calculateTotalHoursWorked(Scanner scanner) {
        double totalHoursWorked = 0;
        for (int i = 1; i <= 5; i++) {
            System.out.println("\nEnter date for day " + i + " (mm/dd/yyyy):");
            String dateString = scanner.next();
            // Parse the date with the specified format
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
            LocalDate date = LocalDate.parse(dateString, formatter);
            System.out.println("Enter time in for " + date + " (in 24-hour format, e.g., 9.30 for 9:30 AM):");
            double timeIn = scanner.nextDouble();
            System.out.println("Enter time out for " + date + " (in 24-hour format):");
            double timeOut = scanner.nextDouble();
            // Calculate hours worked for the day
            double hoursWorked = (timeOut - timeIn) - 1; // subtracting 1 hour for lunch break from 12PM to 1PM
            totalHoursWorked += hoursWorked;
        }
        return totalHoursWorked;
    }

    // Function to calculate and display salary details
    private static void calculateAndDisplaySalary(int employeeId, ArrayList<Employee> employees, double totalHoursWorked) {
        Employee selectedEmployee = employees.stream().filter(employee -> employee.getId() == employeeId).findFirst().orElse(null);
        if (selectedEmployee != null) {
            double weeklySalary = selectedEmployee.getHourlyRate() * totalHoursWorked;
            double monthlySalary = weeklySalary * 4; // Assuming 4 weeks in a month
            double philHealthDeduction = monthlySalary * 0.03;
            double pagIbigDeduction = monthlySalary * 0.02;
            double sssDeduction = calculateSSSDeduction(monthlySalary);
            double withholdingTax = calculateWithholdingTax(monthlySalary);
            double totalDeductions = philHealthDeduction + pagIbigDeduction + sssDeduction + withholdingTax;
            double netSalary = monthlySalary - totalDeductions;
            // Display salary details
            System.out.println("\n--------------------------------------");
            System.out.println("EMPLOYEE PAYSLIP");
            System.out.println("--------------------------------------");
            System.out.println("\nTotal Hours Worked: " + totalHoursWorked);
            System.out.println("Weekly Salary: Php" + weeklySalary);
            System.out.println("--------------------------------------");
            System.out.println("GROSS INCOME: Php" + monthlySalary);
            System.out.println("--------------------------------------");
            System.out.println("\nPhilHealth: Php" + philHealthDeduction);
            System.out.println("Pag-IBIG: Php" + pagIbigDeduction);
            System.out.println("Social Security System: Php" + sssDeduction);
            System.out.println("Withholding Tax: Php" + withholdingTax);
            System.out.println("--------------------------------------");
            System.out.println("TOTAL DEDUCTIONS: Php" + totalDeductions);
            System.out.println("--------------------------------------");
            System.out.println("\n*****TAKE HOME PAY: Php" + netSalary + "*****");
        } else {
            System.out.println("Employee not found.");
        }
    }

    // Function to calculate SSS deduction based on monthly salary
    private static double calculateSSSDeduction(double monthlySalary) {
        if (monthlySalary >= 22250 && monthlySalary < 22750) {
            return 1012.50;
        } else if (monthlySalary >= 22750 && monthlySalary < 23250) {
            return 1035;
        } else if (monthlySalary >= 23250 && monthlySalary < 23750) {
            return 1057.50;
        } else if (monthlySalary >= 23750 && monthlySalary < 24250) {
            return 1080;
        } else if (monthlySalary >= 24250 && monthlySalary < 24750) {
            return 1102.50;
        } else if (monthlySalary >= 24750) {
            return 1125;
        }
        return 0;
    }

    // Function to calculate withholding tax based on monthly salary
    private static double calculateWithholdingTax(double monthlySalary) {
        double taxableIncome = monthlySalary - (monthlySalary * 0.05); // Assuming 5% for other deductions
        if (taxableIncome >= 20833 && taxableIncome < 33333) {
            return (taxableIncome - 20833) * 0.20;
        } else if (taxableIncome >= 33333 && taxableIncome < 66667) {
            return (taxableIncome - 33333) * 0.25;
        } else if (taxableIncome >= 66667 && taxableIncome < 166667) {
            return (taxableIncome - 66667) * 0.30;
        }
        return 0;
    }
}
Leave a Comment