QN 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 in excess of 40 hours, commission employees are paid a percentage of their sales and 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 you to write an application that performs its payroll calculations polymorphically. Use abstract class Employee to represent the general concept of an employee. The classes that extend Employee are SalariedEmployee, CommissionEmployee and Hourly-Employee. Class BasePlusCommissionEmployee which extends CommissionEmployee represents the last employee type. The figure below illustrated the hierarchy.
ANSWER
Employee.java
package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qwkisoft.com */ public abstract class Employee { private String firstName; private String laststName; private String socialSecurityNumber; public Employee(String firstName, String laststName, String ssn) { this.firstName = firstName; this.laststName = laststName; this.socialSecurityNumber = ssn; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLaststName() { return laststName; } public void setLaststName(String laststName) { this.laststName = laststName; } public String getSocialSecurityNumber() { return socialSecurityNumber; } public void setSocialSecurityNumber(String ssn) { this.socialSecurityNumber = ssn; } // abstract method overridden by concrete subclasses public abstract double earnings(); // no implementation here // end abstract class Employee @Override public String toString(){ return String.format("%s %s ssn: %s", getFirstName(),getLaststName(),getSocialSecurityNumber()); } }HourlyEmployee .java
package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qwkisoft.com */ public class HourlyEmployee extends Employee{ private double wage; //wage per hour private double hours;// hours worked for week public HourlyEmployee(String firstName, String laststName, String ssn,double hours,double wage) { super(firstName, laststName, ssn); this.setHours(hours); this.setWage(wage); } public double getWage() { return wage; } public void setWage(double hourlyWage) { if(hourlyWage>=0.0){ this.wage = hourlyWage; }else throw new IllegalArgumentException("HourLY Wage must be >=0.0"); } public double getHours() { return hours; } public void setHours(double hourWorked) { if(hourWorked>=0.0){ this.hours = hourWorked; }else throw new IllegalArgumentException("Hour must be >=0.0 and <=168"); //work hour per week 7*24hrs } @Override public double earnings() { if(getHours()<=40){ return getWage()*getHours(); }else{ return getWage()*getHours()+(getHours()-40)*getWage()*1.5; } } @Override public String toString() { return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", super.toString(), "hourly wage" , getWage(), "hours worked", getHours() ); } }
package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qksoftz.com */ // SalariedEmployee concrete class extends abstract class Employee. public class SalariedEmployee extends Employee{ private double weeklySalary; public SalariedEmployee(String firstName, String laststName, String ssn,double salary) { super(firstName, laststName, ssn); this.setWeeklySalary(salary); } public double getWeeklySalary() { return weeklySalary; } public void setWeeklySalary(double salary) { if(salary>=0.0){ this.weeklySalary = salary; }else throw new IllegalArgumentException("Weekly Salary Must be >=0.0"); } @Override public double earnings() { return getWeeklySalary(); } @Override public String toString(){ return String.format("salaried employee: %s\n%s: $%,.2f", super.toString(), "weekly salary" , getWeeklySalary() ); } }
package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qwkisoft.com */ public class CommissionEmployee extends Employee{ private double grossSales; private double commissionRate; public CommissionEmployee(String firstName, String laststName, String ssn,double sales,double rate) { super(firstName, laststName, ssn); this.setCommissionRate(rate); this.setGrossSales(sales); } public double getGrossSales() { return grossSales; } public void setGrossSales(double sales) { if(sales>=0.0){ this.grossSales = sales; }else throw new IllegalArgumentException("gross sales must be >0.0"); } public double getCommissionRate() { return commissionRate; } public void setCommissionRate(double rate) { if(rate>0.0 && rate<1.0){ this.commissionRate = rate; }else throw new IllegalArgumentException("Commission rate must be >0 and <1.0"); } @Override public double earnings() { return getGrossSales()*getCommissionRate(); } @Override public String toString(){ return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f","commission employee", super.toString(), "gross sales", getGrossSales(), "commission rate", getCommissionRate() ); } }
package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qwkisoft.com */ public class BasePlusCommissionEmployee extends CommissionEmployee{ private double baseSalary; public BasePlusCommissionEmployee(String firstName, String laststName, String ssn, double sales, double rate,double salary) { super(firstName, laststName, ssn, sales, rate); this.setBaseSalary(salary); } public double getBaseSalary() { return baseSalary; } public void setBaseSalary(double salary) { if(salary>=0.0){ this.baseSalary = salary; }else throw new IllegalArgumentException("Base salary must be >0.0"); } // calculate earnings; override method earnings in CommissionEmployee @Override public double earnings() { return getBaseSalary() + super.earnings(); } @Override public String toString() { return String.format( "%s %s; %s: $%,.2f", "base-salaried", super.toString(), "base salary", getBaseSalary() ); } }
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package employee; /** * * @author Eng.Mghase * @email mghase.sadick1@gmail.com * @contact 0768454641 * @web http://qwkisoft.com */ public class TestClass { public static SalariedEmployee salariedEmployee; public static HourlyEmployee hourlyEmployee; public static CommissionEmployee commissionEmployee; public static BasePlusCommissionEmployee basePlusCommissionEmployee; public static void main(String[] args) { // create four-element Employee array Employee[] employees = new Employee[4]; // initialize array with Employees employees[0] = new SalariedEmployee("Adam", "Gambo", "T111", 350000.00); employees[1] = new HourlyEmployee("Tumaini", "Shija", "T222", 16.00, 5000); employees[2] = new CommissionEmployee("Scott", "Majanga", "T333", 3800000, 0.05); employees[3] = new BasePlusCommissionEmployee("Rose", "Mahuka", "T444", 2860000, .05, 450000); System.out.println("Employees processed polymorphically:\n"); for (Employee currentEmployee : employees) { System.out.println(currentEmployee); // invokes toString if (currentEmployee instanceof BasePlusCommissionEmployee) { BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee; employee.setBaseSalary(1.10 * employee.getBaseSalary()); System.out.printf( "new base salary with 10%% increase is: $%,.2f\n", employee.getBaseSalary()); } System.out.printf( "earned $%,.2f\n\n", currentEmployee.earnings()); } } }
0 comments:
Post a Comment