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

Instructions: (This is the same asmt we did in class, the only difference is tha

ID: 3904948 • Letter: I

Question

Instructions: (This is the same asmt we did in class, the only difference is that the data file does not tell us how many employees we have. You need to figure out how to handle this situation. However, you may assume the number of employee will never exceed 20000.) ----------------- Write a program to model three different kinds of employees in a company using inheritance and File I/O. Your program will consist of three classes that extend the class Employee and a testing class Payroll.java that reads in payroll information from a file then prints it to the console window. The Classes Manager, HourlyWorker, and CommissionEmployee: ----------------------------------------------------------------------------------------- To model different types of employees, extend the provided abstract class Employee by writing three new classes: Manager, HourlyWorker, and CommissionEmployee. Implement the abstract methods as required. Managers get paid a fixed weekly salary, commission employees get paid based on base salary and commission (commission rate x sales), hourly workers get paid an hourly wage with time-and-a-half: 1.5 times the hourly wage for hours worked over 40 hours. Do not create additional fields, and only write methods that override methods from either Employee or Object. The Class Payroll: Create a class called Payroll with a main method to test your classes. This method should create an array of type Employee (think about what size you use?) Then, populate the array using data from the text file payroll.txt using appropriate Java I/O techniques. Each line of the file will look similar to one of these: #Steve, Davis,2000 *John, Kanet, 800, 7000, 0.10 @Thomas, Hill, 20, 50 *Lisa, Green,800,6000,0.10 In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and print out each employee in the following format: Manager: Steve Davis $2000 Commission Employee: John Kanet $1500 (Base salary: $800, sales: $7000, Commission rate: 10%) Hourly Worker: Thomas Hill $1100 (Hourly wage: $20, hours worked: 50)

Employee class given:

public abstract class Employee {

private String firstName;
private String lastName;
private static int employeeID=100;

public Employee(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
employeeID++;
}

public void setFirstName(String firstName){
this.firstName=firstName;
}
public String getFirstName(){
return firstName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public String getLastName(){
return lastName;
}

public int getEmplyeeID(){
return employeeID;
}
public String toString(){
return String.format("%20s%20s", firstName, lastName);
}
public abstract double earnings();
}

Explanation / Answer

here is your classes : -------->>>>>>>>

Manager.java : ----->>>

public class Manager extends Employee{
private double weeklySalary;

public Manager(String fn,String ln){
  super(fn,ln);
}

public Manager(String fn,String ln,double wSal){
  super(fn,ln);
  weeklySalary = wSal;
}

public void setWeeklySalary(double wSal){
  weeklySalary = wSal;
}

public double earnings(){
  return weeklySalary;
}

public String toString(){
  return " Manager : "+super.toString()+" $"+weeklySalary;
}

}

HourlyWorker.java : --------->>>

public class HourlyWorker extends Employee{
private double hourlyWage;
private int hourWorked;

public HourlyWorker(String fn,String ln){
  super(fn,ln);
}

public HourlyWorker(String fn,String ln,double hWage,int hW){
  super(fn,ln);
  hourWorked = hW;
  hourlyWage = hWage;
}

public void setHourlyWorked(int hW){
  hourWorked = hW;
}

public void setHourlyWage(double hWage){
  hourlyWage = hWage;
}

public double getHourlyWage(){
  return hourlyWage;
}

public double getHourlyWorked(){
  return hourWorked;
}

public double earnings(){
  if(hourWorked > 40){
   return 40*hourlyWage + (hourlyWage*1.5*(hourWorked-40));
  }else{
   return hourWorked*hourlyWage;
  }
}

public String toString(){
  return " Hourly Worked : "+super.toString()+" $"+earnings()+" ( Hourly Wage : $"+getHourlyWage()+", Hour Worked : "+getHourlyWorked()+" )";
}
}

CommissionEmployee.java : ----------->>>>>>>>>

public class CommissionEmployee extends Employee{
private double baseSal;
private double sales;
private double rate;

public CommissionEmployee(String fn,String ln){
  super(fn,ln);
}

public CommissionEmployee(String fn,String ln,double bSal,double sale,double rate){
  super(fn,ln);
  baseSal = bSal;
  this.sales = sale;
  this.rate = rate;
}

public void setRate(double rate){
  this.rate = rate;
}

public void setBaseSalary(double bsal){
  baseSal = bsal;
}

public void setSales(double sale){
  sales = sale;
}

public double getRate(){
  return rate;
}

public double getSales(){
  return sales;
}

public double getBaseSalary(){
  return baseSal;
}

public double earnings(){
  return baseSal + (sales*rate/100);
}


public String toString(){
  String s1 = " Commission Employee : ";
  s1 += super.toString();
  s1 += " $"+earnings()+" (";
  s1 += "Base Salary : $"+getBaseSalary()+", ";
  s1 += "Sales : $"+getSales()+", ";
  s1 += "Commission Rate : "+getRate()+" % )";

  return s1;
}
}

PayRoll.java : --------->>>>>>

import java.io.File;
import java.util.Scanner;

public class PayRoll{
public static void main(String[] args) {
  Employee[] employees = new Employee[20];
  int n = 0;

  try{
   Scanner sc = new Scanner(new File("payroll.txt"));
   while(sc.hasNextLine()){
    if(n == 20){
     break;
    }

    String line = sc.nextLine();
    if(line.charAt(0) == '#'){
     String[] str = line.split(",");
     if(str.length == 3){
      employees[n] = new Manager(str[0].substring(1,str[0].length()),str[1],Double.parseDouble(str[2]));
      n++;
     }
    }else if(line.charAt(0) == '@'){
     String[] str = line.split(",");
     if(str.length == 4){
      employees[n] = new HourlyWorker(str[0].substring(1,str[0].length()),str[1],Double.parseDouble(str[2]),Integer.parseInt(str[3]));
      n++;
     }
    }else if(line.charAt(0) == '*'){
     String[] str = line.split(",");
     if(str.length == 5){
      employees[n] = new CommissionEmployee(str[0].substring(1,str[0].length()),str[1],Double.parseDouble(str[2]),Double.parseDouble(str[3]),Double.parseDouble(str[4]));
      n++;
     }
    }
   }
  }catch(Exception e){
   System.out.println("Error in file opening");
  }

  for(int i = 0;i<n;i++){
   System.out.println(employees[i]);
  }
}
}