In: Computer Science
Exercise 2: A company runs a small factory. You need to create a pay slip for the employees of the company. The details of the Java application to be implemented is provided below.
A. The factory workers are paid one of the three hourly rates depending on their skill level.
Write a method to that takes the skill level as a parameter and returns the hourly rate. Use the switch construct in Java.
B. Each factory worker might work any number of hours per week; any hour over 40 are paid at double rate.
Write a method that takes the hours worked and calculates the gross pay depending on if there is overtime or not. Return the gross pay from the method.
Skill Level | Hourly Rate (dirham) |
A | 50 |
B | 40 |
C | 30 |
C. Factory workers have extra benefits. Only workers in skill level A can choose to participate in the Retirement Plan at 5% of their gross pay. Also, The workers in all skill levels can choose to participate in the following insurance options:
Option | Explanation | Weekly Cost to Employee (dirham) |
1 | Medical Insurance | 73 |
2 | Dental Insurance | 35 |
3 | Disability Insurance | 20 |
Write a method that takes the worker’s skill level and gross pay as parameter and asks the user the necessary questions to decide on benefits (Insurance and/or Retirement). The user must be given the choice to choose all three types of Insurances. The method must return the net pay after deducting the benefits if the worker chooses to opt for these benefits.
Ask the user to enter the details of all employees. The details include: Employee ID, Name, Skill level, and Hourly Pay should entered in the main method. The Retirement and Insurance benefits should be asked from the User in the above method.
Generate a pay slip for each employee i.e. if the user enters 10 employees, then 10 pay slips are to be generated. Each pay slip is written to a separate file with the name given as the employee id. For example: if the employee id is A11P9, then the file “A11P9.txt” will contain the pay slip of the employee. The pay slip for each worker must contain the following details in a neat and readable format.
The ID, Name, and skill level of the Factory Worker
The total number of working hours of the factory worker
The overtime hours
The hourly rate
The gross pay of a factory worker i.e. before the deductions
The total deductions i.e. the retirement and insurance benefits
The net pay i.e. Gross pay – Total deductions
Code
import java.io.FileWriter;
import java.util.Scanner;
public class EmployeePayslip
{
private String id,name,skillLevel;
private int hours,rate;
private double netPay,deduction,grossPay;
public EmployeePayslip() {
}
public void setNetPay(double netPay) {
this.netPay = netPay;
}
public void setDeduction(double deduction) {
this.deduction = deduction;
}
public void setGrossPay(double grossPay) {
this.grossPay = grossPay;
}
public double getNetPay() {
return netPay;
}
public double getDeduction() {
return deduction;
}
public double getGrossPay() {
return grossPay;
}
public EmployeePayslip(String id, String name, String skillLevel,
int hours) {
this.id = id;
this.name = name;
this.skillLevel = skillLevel;
this.hours = hours;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getSkillLevel() {
return skillLevel;
}
public int getHours() {
return hours;
}
public static void main(String[] args)
{
int totalEmployee,hr;
String name,id;
String lvl;
Scanner sc=new Scanner(System.in);
EmployeePayslip []emp;
System.out.print("Enter the total number of emplyee worked:
");
totalEmployee=sc.nextInt();
emp=new EmployeePayslip[totalEmployee];
for(int i=0;i
System.out.print("Enter the Employee id: ");
id=sc.next();
System.out.print("Enter the Employee name: ");
name=sc.next();
System.out.print("Enter the Employee skill Level(A,B or C):
");
lvl=sc.next();
System.out.print("Enter the Employee's number of hours per week:
");
hr=sc.nextInt();
emp[i]=new EmployeePayslip(id, name, lvl, hr);
emp[i].setGrossPay(calculateGrosspay(emp[i]));
double
netPay=calculateNetPay(emp[i].getSkillLevel(),emp[i].getGrossPay());
emp[i].setNetPay(netPay);
emp[i].setDeduction(emp[i].getGrossPay()-emp[i].getNetPay());
System.out.println("");
}
printPaySlip(emp);
}
private static void printPaySlip(EmployeePayslip[] emp)
{
for(int i=0;i
try
{
FileWriter fw=new FileWriter(emp[i].getId()+".txt");
fw.write(emp[i].getId()+" "+emp[i].getName()+"
"+emp[i].getSkillLevel()+"\r\n");
fw.write(emp[i].getHours()+"\r\n");
if(emp[i].getHours()>40)
fw.write((emp[i].getHours()-40)+"\r\n");
else
fw.write(0+"\r\n");
fw.write( emp[i].rate+"\r\n");
fw.write(emp[i].getGrossPay()+"\r\n");
fw.write(emp[i].getDeduction()+"\r\n");
fw.write(emp[i].getNetPay()+"\r\n");
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
System.out.println("\nAll payslip are genrated.......");
}
private static double calculateGrosspay(EmployeePayslip emp)
{
int hourlyPay=getHourRate(emp.getSkillLevel());
emp.rate=hourlyPay;
double grosspay;
if(emp.hours>40)
{
grosspay=40*hourlyPay+((emp.hours-40)*2*hourlyPay);
}
grosspay=emp.getHours()*hourlyPay;
return grosspay;
}
private static int getHourRate(String skillLevel)
{
int payRate=0;
switch(skillLevel)
{
case "A":
payRate= 50;
break;
case "B":
payRate= 40;
break;
case "C":
payRate= 30;
break;
}
return payRate;
}
private static double calculateNetPay(String lvl,double
grossPay)
{
double netPay=grossPay;
double totalDeduction=0;
String choice;
Scanner sc=new Scanner(System.in);
if(lvl.equalsIgnoreCase("A"))
{
System.out.print("Do you want retirement Plan that is 5% of your
gross pay?(y/n): ");
choice=sc.next();
if(choice.equalsIgnoreCase("y"))
totalDeduction+=grossPay*0.05;
}
System.out.print("Do you want Medical Insurancet Plan that is cost
73 per week?(y/n): ");
choice=sc.next();
if(choice.equalsIgnoreCase("y"))
totalDeduction+=73;
System.out.print("Do you want Dental Insurancet Plan that is cost
35 per week?(y/n): ");
choice=sc.next();
if(choice.equalsIgnoreCase("y"))
totalDeduction+=35;
System.out.print("Do you want Disability Insurancet Plan that is
cost 20 per week?(y/n): ");
choice=sc.next();
if(choice.equalsIgnoreCase("y"))
totalDeduction+=20;
return grossPay-totalDeduction;
}
}
output
txt file genrated