Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an employee payroll program that uses polymorphism to calculate and print

ID: 3538691 • Letter: W

Question

Write an employee payroll program that uses polymorphism to calculate and print the weekly payroll for your company. There are three types of employees %u2500 hourly, salaried, and salaried plus commission. Each type of employee gets paid using a different formula. But for all employee types, if the calculated paycheck exceeds $1000, the actual paycheck must be decreased to $1000.


Use this class hierarchy:


Employee

/

Hourly Salaried

|

Salaried Plus Commission


Employee class:


Instance variables:

name

social security number

birthday month

birthday week.


load method :

Prompts the user for instance-variable values and loads the entries.


toString method:

Returns a string that shows the employee%u2019s name, social security number, and paycheck.. Use the String.format method (See Java API documentation to help you format the string as shown in the sample session%u2019s paycheck report.) Here%u2019s an example from the sample session:

employee: Biff Sanchez

social security number: 111-11-1111

paycheck: $800.00


getBonus method:

Generates a $100 employee birthday bonus. Compare the employee%u2019s birthday with the current date found on your computer system. Use the Calendar class to generate the current date. (See Sun%u2019s Java API documentation.) If the employee%u2019s birthday month and birthday week match your computer system%u2019s current month and current week, then increment the employee%u2019s paycheck by $100. The birthdayMonth holds the month (1%u250012) in which the employee was born. The birthdayWeek holds the week (1%u25004) that the employee chooses to get paid his/her bonus.


Hourly class:


Instance variables:

hourly pay

hours worked during the past week


load method:

Prompts the user for instance-variable values and loads the entries.


Include a getEarnings method that calculates earnings for an hourly employee. Hourly employees are paid by the hour. If they work more than 40 hours in a week, then they receive overtime pay for their overtime work. Overtime pay equals one and a half times their normal hourly pay.


Salaried class:


Instance variables:

weekly salary


load method:

Prompts the user for instance-variable values and loads the entries.


Include a getEarnings method that calculates earnings for a salaried employee. Salaried employees are paid their fixed weekly salary regardless of the number of hours they work.


SalariedPlusCommission class:


Instance variables:

sales during the past week

commission rate


load method:

Prompts the user for instance-variable values and loads the entries.


Include a getEarnings method that calculates earnings for a SalariedPlusCommission employee. SalariedPlusCommission employees are paid a base salary plus a percentage of their sales. %u201CPercentage of their sales%u201D equates to the product of their sales times their commission rate.


Use initially declared named constants instead of hardcoded %u201Cmagic numbers%u201D embedded in the program. In the Employee class, include a getEarnings method that is abstract.


Use the public access modifier for the toString method in the Employee class

and the load method in the Employee, Hourly, Salaried, and SalariedPlusCommission classes.


Provide a driver that generates the following queries and outputs.


Sample session (assuming current month = 10 and current week = 2):


Number of employees: 3


PROFILE FOR EMPLOYEE #1:

type Hourly(1), Salaried(2), Salaried plus Commission(3)

Enter 1, 2, or 3 ==> 1

Name ==> Biff Sanchez

Social security number ==> 111-11-1111

Birthday month (1-12) ==> 2

Birthday bonus week (1-4) ==> 3

Hourly pay ==> 20

Hours worked this past week ==> 30


PROFILE FOR EMPLOYEE #2:

type Hourly(1), Salaried(2), Salaried plus Commission(3)

Enter 1, 2, or 3 ==> 2

Name ==> Dirk Jones

Social security number ==> 222-22-2222

Birthday month (1-12) ==> 10

Birthday bonus week (1-4) ==> 2

Salary ==> 700


PROFILE FOR EMPLOYEE #3:

type Hourly(1), Salaried(2), Salaried plus Commission(3)

Enter 1, 2, or 3 ==> 3

Name ==> Suzie Que

Social security number ==> 333-33-3333

Birthday month (1-12) ==> 7

Birthday bonus week (1-4) ==> 3

Salary ==> 400

Sales for this past week ==> 2000

Sales commission rate (fraction paid to employee) ==> .25


PAYCHECK REPORT:


employee: Biff Sanchez

social security number: 111-11-1111

paycheck: $600.00


employee: Dirk Jones

social security number: 222-22-2222

paycheck: $800.00


employee: Suzie Que

social security number: 333-33-3333

paycheck: $900.00

Explanation / Answer

Employee class :


import java.util.Calendar;
import java.util.Scanner;

public abstract class Employee {
    String name;
    String socialSecurityNumber;
    int birthdayMonth;
    int birthdayWeek;
   
    public void load(Scanner scanner){
        if(scanner == null){
            scanner = new Scanner(System.in);
        }
        System.out.print("Employee Name ==> ");
        this.name = scanner.nextLine();
       
        System.out.print("Social Security Number ==>");
        this.socialSecurityNumber = scanner.nextLine();
       
        System.out.print("Date of Birth Month(1-12) ==> ");
        this.birthdayMonth = scanner.nextInt();
        scanner.skip(" ");
       
        System.out.print("Date of Birth Month(1-4) ==> ");
        this.birthdayWeek = scanner.nextInt();
        scanner.skip(" ");
       
       
    }
   
    public String toString(){
        String content = "";
        content = "Employee : "+name+" "+
                "Social Security Number : "+socialSecurityNumber+" ";
       
        return content;
    }
   
    public int getBonus(){
        int bonus = 0;
        Calendar c = Calendar.getInstance();
        int week = c.get(Calendar.WEEK_OF_MONTH);
        int month = c.get(Calendar.MONTH);
       
        if(week == birthdayWeek && month+1 == birthdayMonth){
            bonus = 100;
        }
        return bonus;
    }
   
    public abstract double getEarnings();
}


Hourly class :


import java.util.Scanner;

public class Hourly extends Employee{
    double hourlyPay;
    int lastWeekHoursWorked;
   
    public void load(Scanner scanner){
        if(scanner == null){
            scanner = new Scanner(System.in);
        }
        super.load(scanner);
       
        System.out.print("Hourly Pay ==> ");
        hourlyPay = scanner.nextDouble();
        scanner.skip(" ");
       
        System.out.print("Last week hours worked ==> ");
        lastWeekHoursWorked = scanner.nextInt();
        scanner.skip(" ");
    }
   
    public double getEarnings(){
        double earnings = 0;
        if(lastWeekHoursWorked > 40){
            earnings = 40 * hourlyPay;
            earnings = (lastWeekHoursWorked - 40)*(hourlyPay * 1.5);
        }else{
            earnings = lastWeekHoursWorked * hourlyPay;
        }
        return earnings + getBonus();
    }
   
    public String toString(){
        String content = super.toString()+" paycheck : $"+getEarnings();
       
        return content;
    }
}


Salaried class :


import java.util.Scanner;

public class Salaried extends Employee{
    double weeklySalary;
   
    public void load(Scanner scanner){
        if(scanner == null){
            scanner = new Scanner(System.in);
        }
        super.load(scanner);
       
        System.out.print("Weekly Salary ==> ");
        weeklySalary = scanner.nextDouble();
        scanner.skip(" ");
    }
   
    public double getEarnings(){
        return weeklySalary + getBonus();
    }
   
    public String toString(){
        String content = "";
        if(this instanceof Salaried){
            content = super.toString()+" paycheck : $"+getEarnings();
        }else{
            content = super.toString();
        }
       
        return content;
    }
}


SalariedPlusCommission class :


public class SalariedPlusCommission extends Salaried{
    double salesPastWeek;
    double commisionRate;
   
    public void load(Scanner scanner){
        if(scanner == null){
            scanner = new Scanner(System.in);
        }
        super.load(scanner);
       
        System.out.print("Past Week sales ==> ");
        salesPastWeek = scanner.nextDouble();
        scanner.skip(" ");
       
        System.out.print("Commission Rate ==> ");
        commisionRate = scanner.nextDouble();
        scanner.skip(" ");
    }
   
    public double getEarnings(){
        return super.getEarnings() + (salesPastWeek/100) * commisionRate;
    }
   
    public String toString(){
        String content = super.toString()+" paycheck : $"+getEarnings();
       
        return content;
    }
}


I have writtent the test class for you to validate the code.


TestEmployee class :


import java.util.ArrayList;
import java.util.Scanner;

public class TestEmployee {
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        int choice = 0;
        ArrayList<Employee> employees = new ArrayList<Employee>();
        while(true){
            System.out.println("type Hourly(1), Salaried(2), Salaried plus Commission(3) ");
           
            while(true){
                System.out.print("Enter 1, 2, or 3 ==> ");
                try{
                    choice = scanner.nextInt();
                    scanner.skip(" ");
                    if(choice < 1 && choice > 3){
                        System.out.println("Wrong input .... Please try again...");
                        continue;
                    }
                }catch(Exception e){
                    System.out.println("Wrong input .... Please try again...");
                    continue;
                }
                break;
            }
            switch(choice){
            case 1 : {
                Hourly hourly = new Hourly();
                hourly.load(scanner);
                employees.add(hourly);
                break;
            }
            case 2 : {
                Salaried salaried = new Salaried();
                salaried.load(scanner);
                employees.add(salaried);
                break;
            }
            case 3 :{
                SalariedPlusCommission comm = new SalariedPlusCommission();
                comm.load(scanner);
                employees.add(comm);
                break;
            }
            }
           
            System.out.print("Do you want to add more employees (y) ==> ");
            if(!scanner.nextLine().equalsIgnoreCase("y")){
                break;
            }
        }
        System.out.println("Display Report......");
        displayReport(employees);
    }
   
    public static void displayReport(ArrayList<Employee> employees){
        if(employees != null){
            for(Employee emp : employees){
                if(emp instanceof Hourly){
                    System.out.println(((Hourly)emp).toString());
                }else if(emp instanceof SalariedPlusCommission){
                    System.out.println(((SalariedPlusCommission)emp).toString());
                }else if(emp instanceof Salaried){
                    System.out.println(((Salaried)emp).toString());
                }
               
                System.out.println("==================================");
            }
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote