Problem Super Employer Inc. has two different types of employees with some basic
ID: 3546342 • Letter: P
Question
Problem
Super Employer Inc. has two different types of employees with some basic features in common but different methods of calculating pay. It is also likely that other employee types may be added in the future. Write an interface that specifies constants and functions that all the employees must contain. Then write classes that implement the interface class. Finally, write a test class. Details follow:
Interface: Company
Constants
Company Name: Super Employer Inc.
Company Address: Rochester, NY
Weeks paid: 52
Methods
setPay
Explanation / Answer
#########################Company.java##################
public interface Company {
String COMPANY_NAME = "Super Employer Inc",
COMPANY_ADDRESS = "Rochester, NY";
int WEEKS_PAID = 52;
public void setPay(double pay);
public double calcAnnualPay();
public double calcWeeklyPay();
}
#########################Salaried.java##############
public class Salaried implements Company {
double HEALTH_INSURANCE=125.50,pay;
public double getPay() {
return pay;
}
String name,ssn;
@Override
public void setPay(double pay) {
this.pay=pay;
}
@Override
public double calcAnnualPay() {
return WEEKS_PAID*HEALTH_INSURANCE;
}
@Override
public double calcWeeklyPay() {
return (calcAnnualPay()/WEEKS_PAID)-HEALTH_INSURANCE;
}
public String toString(){
return "Name : "+getName()+" Social Security Number : "+getSsn();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
}
########################Hourly.java###############
public class Hourly implements Company {
int OVERTIME_HOURS = 40, AVG_HOURS_PER_WEEK;
double OVERTIME_RATE = 1.5, HOURLY_RATE,pay;
public double getPay() {
return pay;
}
public int getOVERTIME_HOURS() {
return OVERTIME_HOURS;
}
public void setOVERTIME_HOURS(int oVERTIME_HOURS) {
OVERTIME_HOURS = oVERTIME_HOURS;
}
public int getAVG_HOURS_PER_WEEK() {
return AVG_HOURS_PER_WEEK;
}
public void setAVG_HOURS_PER_WEEK(int aVG_HOURS_PER_WEEK) {
AVG_HOURS_PER_WEEK = aVG_HOURS_PER_WEEK;
}
public double getOVERTIME_RATE() {
return OVERTIME_RATE;
}
public void setOVERTIME_RATE(double oVERTIME_RATE) {
OVERTIME_RATE = oVERTIME_RATE;
}
public double getHOURLY_RATE() {
return HOURLY_RATE;
}
public void setHOURLY_RATE(double hOURLY_RATE) {
HOURLY_RATE = hOURLY_RATE;
}
String name, ssn;
@Override
public void setPay(double pay) {
this.pay=pay;
}
@Override
public double calcAnnualPay() {
return calcWeeklyPay()*WEEKS_PAID;
}
@Override
public double calcWeeklyPay() {
if(AVG_HOURS_PER_WEEK>OVERTIME_HOURS){
return (OVERTIME_HOURS*HOURLY_RATE)+((AVG_HOURS_PER_WEEK-OVERTIME_HOURS)*HOURLY_RATE*OVERTIME_RATE);
}else{
return HOURLY_RATE*8*5;
}
}
public String toString(){
return "Name : "+getName()+" Social Security Number : "+getSsn()+" Average Hours worked: "+AVG_HOURS_PER_WEEK+" Hourly rate: "+HOURLY_RATE+" Weekly Pay: $"+calcWeeklyPay()+" Annual Pay: $"+calcAnnualPay();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
}
######################Payroll.java#######################
import java.util.Scanner;
public class Payroll {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String menu = "What do you want to do? 1 Create a new employee 2 Set pay 3 Show weekly pay 4 Show annual pay 5 Show summary 6 Quit ==>";
// System.out.print(menu);
int empCount = 0;
Salaried s1 = new Salaried();
Hourly h1 = new Hourly();
while (true) {
System.out.print(menu);
int option = input.nextInt();
if (option == 1) {
while (true) {
System.out
.print("What type of employee would you like to create (Salaried or Hourly)? ");
String op = input.next();
if (op.equalsIgnoreCase("salaried")) {
System.out.print("Please enter the name:");
String name = input.next();
System.out
.println("Please enter the social security number:");
String ssn = input.next();
s1.setName(name);
s1.setSsn(ssn);
empCount = empCount + 1;
System.out.println("==> Employee " + empCount
+ " has been created.");
} else if (op.equalsIgnoreCase("hourly")) {
System.out.print("Please enter the name:");
String name = input.next();
System.out
.println("Please enter the social security number:");
String ssn = input.next();
double hRate;
while (true) {
System.out
.print("Please enter the hourly rate of pay:");
hRate = input.nextDouble();
if (hRate <= 0) {
continue;
} else {
break;
}
}
int avg;
while (true) {
System.out
.print("Please enter the average number of hours worked per week:");
avg = input.nextInt();
if (avg <= 0) {
continue;
} else {
break;
}
}
h1.setName(name);
h1.setSsn(ssn);
h1.setHOURLY_RATE(hRate);
h1.setAVG_HOURS_PER_WEEK(avg);
empCount = empCount + 1;
System.out.println("==> Employee " + empCount
+ " has been created.");
} else {
System.out
.print("***Please enter either 'salaried' or 'hourly'.");
}
break;
}
} else if (option == 2) {
System.out
.print("Which employee do you wish to use (enter 1 to "
+ empCount + ")? ");
while (true) {
int emp = input.nextInt();
if (emp > empCount || emp <= 0) {
System.out
.print("***ERROR Please enter an employee number from 1 to "
+ empCount + ": ");
continue;
} else {
System.out
.print("How much would you like to set the annual pay to?");
double pay = input.nextDouble();
while (pay <= 0) {
System.out
.println("***ERROR Please enter an amount > 0.");
System.out
.print("How much would you like to set the annual pay to?");
pay = input.nextDouble();
if (option == 1) {
s1.setPay(pay);
} else {
h1.setPay(pay);
}
}
}
break;
}
} else if (option == 3) {
System.out
.print("Which employee do you wish to use (enter 1 to "
+ empCount + ")? ");
while (true) {
int emp = input.nextInt();
if (emp > empCount || emp <= 0) {
System.out
.print("***ERROR Please enter an employee number from 1 to "
+ empCount + ": ");
continue;
} else {
if (option == 1) {
System.out.println("The weekly pay is "+s1.calcWeeklyPay());
} else {
System.out.println("The weekly pay is "+h1.calcWeeklyPay());
}
}
break;
}
} else if (option == 4) {
System.out
.print("Which employee do you wish to use (enter 1 to "
+ empCount + ")? ");
while (true) {
int emp = input.nextInt();
if (emp > empCount || emp <= 0) {
System.out
.print("***ERROR Please enter an employee number from 1 to "
+ empCount + ": ");
continue;
} else {
if (option == 1) {
System.out.println("The annual pay is "+s1.calcAnnualPay());
} else {
System.out.println("The annual pay is "+h1.calcAnnualPay());
}
}
break;
}
}else if (option == 5) {
System.out
.print("Which employee do you wish to use (enter 1 to "
+ empCount + ")? ");
while (true) {
int emp = input.nextInt();
if (emp > empCount || emp <= 0) {
System.out
.print("***ERROR Please enter an employee number from 1 to "
+ empCount + ": ");
continue;
} else {
if (option == 1) {
System.out.println(s1);
} else {
System.out.println(h1);
}
}
break;
}
}else if (option == 6) {
System.out.println("Bye!");
System.exit(0);
} else {
System.out.println("ERROR Bad option, please try again.");
System.out.print(menu);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.