Payroll System Using Polymorphism A company pays its employe

Payroll System Using Polymorphism

A company pays its employees on a weekly basis.

The employees are of four types:

Salaried employees are paid a fixed weekly salary regardless of the number of hours worked.

Hourly employees are paid by the hour and receive overtime pay (i.e., 1.5 times their hourly salary rate) for all hours worked more than 40 hours.

Commission employees are paid a percentage of their sales.

Base-salaried commission employees receive a base salary plus a percentage of their sales.

For the current pay period, the company has decided to reward salaried-commission employees by adding 10% to their base salaries.

The company wants to write an application that performs its payroll calculation polymorphically.

Employee

SalariedEmployee

BasePlusCommissionEmployee

HourlyEmployee

CommissionEmployee

Each employee has first name, last name, and SSN

Salaried employee makes $800.00

Hourly employee works 40 hours a week, $16.75/hr

Commission employee made sales worth $10,000, commission rate 6%

Base-salary commission employee earns $300.00 base salary, 4% commission, made sales this week worth $5,000.00

Create an abstract superclass Employee

Three argument constructor (first, last name, and SSN)

Set and get methods for the three data fields

A method that return String representation of Employee object

Create salaried employee concrete class extends abstract class Employee

Four argument constructor (first, last, ssn, and salary)

Set and get methods for the salary

A method that calculates earnings; override abstract method earnings in Employee

A method return String representation of salaried employee object

Create hourly employee class that extends Employee.

Five argument constructor (first, last, ssn, hourly wage, hours worked)

Calculate earnings method; override abstract method earnings in Employee

Get and set methods

A method that returns String representation of hourly employee object

Apply the above technique to the CommissionEmployee, BasePlusCommissionEmployee classes

Create a class to execute the five classes and output the results as it shows in the “Payroll System Test Output” text file attached.

Do not send individual files, but rather zip the entire program and submit by the due date.

Employee

Solution

Code:

package payrollsystem;

public class Hourlyemployee extends Employee {
    double hourlywage;
    double hoursworked;
    private final int fullTime = 40;
    private final double overTime = 1.5;
    public Hourlyemployee(String firstname, String lastname, int SSN,double hourlywage,double hoursworked) {
        super(firstname, lastname, SSN);
        this.hourlywage=hourlywage;
        this.hoursworked=hoursworked;
    }
    public void sethourlyWage(double newWage)
    {
        if(newWage >=0)
            hourlywage = newWage;
        else
            System.err.println(\"Wages cannot be negative\");
    }

    //mutator method for hours.
    public void sethoursworked(int newHours)
    {
        if (newHours >=0)
            hoursworked = newHours;
        else
            System.err.println(\"Hours cannot be negative\");
    }

    //access method for wage.
    public double gethourlyWage()
    {
        return hourlywage;
    }

    //access method for hours.
    public double gethoursworked()
    {
        return hoursworked;
    }

    //overriding toString method.
    public String toString()
    {
        return super.toString() + \"; wages are: \" + hourlywage + \" and hours are: \" + hoursworked;
    }
    @Override
    public double Earnings() {
        double earnings;
        if (hoursworked <= fullTime)
            earnings = hourlywage * hoursworked;
        else
            earnings = (hourlywage * fullTime) + ((hourlywage* overTime) * (hoursworked - fullTime));
        return earnings;
    }
  
}


package payrollsystem;

public class CommissionEmployee extends Employee{
    private double grossSales;
    private double commissionRate;

  
    public CommissionEmployee(String firstname, String lastname, int SSN, double grosssales,double commission) {
        super(firstname, lastname, SSN);
        this.commissionRate=commission;
        this.grossSales=grosssales;
    }

    @Override
    public double Earnings() {
        double earnings = commissionRate * grossSales;
        return earnings;
    }
     public void setGrossSales(double newGrossSales)
    {
        if (newGrossSales >= 0)
            grossSales = newGrossSales;
        else
            System.err.println(\"Gross sales cannot be negative.\");
    }

    //mutator method.
    public void setCommissionRate(double newCommissionRate)
    {
        if (newCommissionRate >= 0)
            commissionRate = newCommissionRate;
        else
            System.err.println(\"Commission rate cannot be negative.\");
    }

    //access method.
    public double getGrossSales()
    {
        return grossSales;
    }

    //access method.
    public double getCommissionRate()
    {
        return commissionRate;
    }

    //overriding toString.
    public String toString()
    {
        return super.toString() + \"; gross sales are: \" + grossSales + \" and commission rate is: \" + commissionRate;
    }
}


package payrollsystem;

public class BasePlusCommissionEmployee extends Employee {
private double commission;
private double grossSales;
salaried s;
    public BasePlusCommissionEmployee(String firstname, String lastname, int SSN,double gross,double commission) {
        super(firstname, lastname, SSN);
        this.commission=commission;
        this.grossSales=gross;
    }
     public void setCommission(double newCommission)
    {
        if (newCommission >= 0)
            commission = newCommission;
        else
            System.err.println(\"Commission rate cannot be negative.\");
    }
     public void setGrossSales(double newGrossSales)
    {
        if (newGrossSales >= 0)
            grossSales = newGrossSales;
        else
            System.err.println(\"Gross sales cannot be negative.\");
    }
     //access method.
    public double getCommissionRate()
    {
        return commission;
    }
    //access method.
    public double getGrossSales()
    {
        return grossSales;
    }

    @Override
    public double Earnings() {
        double earnings;
        earnings=this.s.getSalary()+commission*grossSales;
        return earnings;
    }
     public String toString()
    {
        return super.toString() + \"; gross sales are: \" + grossSales + \" and commission rate is: \" + commission;
    }
}

package payrollsystem;

public class salaried extends Employee {
  
    private double salary; //Annual salary

    public salaried(String firstname, String lastname, int SSN, int salary) {
        super(firstname, lastname, SSN);
        this.salary=salary;
    }
  
    public double getSalary()
    {
    return salary;
    }
    public void setSalary(double newSalary)
    {
    if(newSalary >= 0.0)
    {
    salary = newSalary;
    }
    }
  

    @Override
    public double Earnings() {
        return salary; //To change body of generated methods, choose Tools | Templates.
    }
     public String toString()
    {
        return super.toString() + \"; weekly salary is: \" + salary;
    }
}


package payrollsystem;

public abstract class Employee {
    String firstname;
    String lastname;
    int SSN;
    public abstract double Earnings();
   public Employee(String firstname, String lastname, int SSN)
    {
    System.out.println(\"Constructing an Employee\");
    this.firstname = firstname;
    this.lastname = lastname;
    this.SSN = SSN;
    }
   public String toString()
   {
   return firstname + \" \" + lastname + \" \" + SSN;
   }
   public String getfirstName()
   {
   return firstname;
   }
   public String getlastName()
   {
   return lastname;
   }
   public void setlastName(String n)
   {
   lastname = n;
   }
    public void setfirstName(String n)
   {
   firstname = n;
   }
   public int getSSN()
   {
   return SSN;
   }
   public void setSSN(int n)
   {
   SSN = n;
   }
  
}


package payrollsystem;

public class PayrollSystem {

    public static void main(String[] args) {
     
        salaried s=new salaried(\"Ben\",\"Jhonson\",121,800);
        System.out.println(s.toString());
        System.out.println(\"Earnings :\"+s.Earnings());
        Hourlyemployee e=new Hourlyemployee(\"Mary\",\"John\",122,16.75,40);
        System.out.println(e.toString());
        System.out.println(\"Earnings :\"+e.Earnings());
        CommissionEmployee c=new CommissionEmployee(\"Mathew\",\"Wilson\",123,10000,0.06);
        System.out.println(c.toString());
        System.out.println(\"Earnings :\"+c.Earnings());
        BasePlusCommissionEmployee b=new BasePlusCommissionEmployee(\"Melvin\",\"Tom\",124,300,.04);
        System.out.println(b.toString());
        System.out.println(\"Earnings :\"+b.Earnings());
      
    }
  
  
}

Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa
Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa
Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa
Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa
Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa
Payroll System Using Polymorphism A company pays its employees on a weekly basis. The employees are of four types: Salaried employees are paid a fixed weekly sa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site