-2

I'm working on the following program.

Design a class named Employee. The class should keep the following information in fields:

a. Employee name. b. Employee number in the format XXX-L, where each X is a digit within the range 0-9 and the L is a letter within the range A-M. c. Hire date.

Write one or more constructors and the appropriate accessor and mutator methods for the class.

Next, write a class named ProductionWorker that extends the Employee class. The ProductionWorker class should have fields to hold the following information:

a. Shift (an integer). b. Hourly pay rate (a double).

The workday is divided into two shifts: day and night. The shift field will be an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write one or more constructors and the appropriate accessor and mutator methods for the class.

In a particular factory, a shift supervisor is a salaried employee who supervises a shift. In addition to a salary, the shift supervisor earns a yearly bonus when his or her shift meets production goals. Design a ShiftSupervisor class that extends the Employee class. The ShiftSupervisor class should have a field that holds the annual salary and a field that holds the annual production bonus that a shift supervisor has earned. Write one or more constructors and the appropriate accessor and mutator methods for the class. Demonstrate the classes by writing a program that uses ProductionWorker and ShiftSupervisor objects.

import java.time.Instant;
import java.util.Date;
import java.util.Scanner;



public class Main {



public static void main(String[] args) {
Scanner sc = new Scanner(System.in);



ShiftSupervisor supervisor = new ShiftSupervisor();
ProductionWorker worker = new ProductionWorker();



System.out.println("Workers Management System");
System.out.println("Create Supervisors");
//Prompts
System.out.println("Enter supervisor name");
String sName = sc.nextLine();
System.out.println("Enter employee number [Patters XXX-L]");
String eNumber = sc.nextLine();
//Setters
supervisor.setsName(sName);
supervisor.seteNumber(eNumber);



System.out.println("Supervisor in company: " + supervisor.getsName() + " Number: " + 
supervisor.geteNumber());



System.out.println("Set " + supervisor.getsName()+ " annual fee");
double annualFee = sc.nextDouble();
supervisor.setAnnualSalary(annualFee);



System.out.println("Set " + supervisor.geteName() + " production bonus: ");
double annualProductionBonus = sc.nextDouble();
supervisor.setAnnualProductionBonus(annualProductionBonus);




//Prompts
System.out.println("Enter worker name: ");
String eName = sc.nextLine();
worker.seteName(eName);



System.out.println("Enter production worker number [Patters XXX-L]");
String eWNumber = sc.nextLine();
//Setters
worker.seteNumber(eWNumber);



System.out.println("Production worker in company: " + worker.geteName() + " Number: " + 
worker.geteNumber());



System.out.println("Set " + worker.geteName() + " shift [0 or 1] is allowed");
int shiftNumber = sc.nextInt();
worker.setShift(shiftNumber);



System.out.println("Set " + worker.geteName() + " hourly pay rate");
double payRate = sc.nextDouble();
worker.setHourRate(payRate);



System.out.println("Supervisor: " + supervisor.geteName() + " " + supervisor.geteNumber() + " Annual 
Salary : " + supervisor.getAnnualSalary() + " Production Bonus: " + 
supervisor.getAnnualProductionBonus());
System.out.println("Production Worker: " + worker.geteName() + " " + worker.geteNumber() + " 
HourlyRate : " + worker.getHourRate() + " Shift: " + worker.getShift());



}
}

    // NEW CLASS
    
ProductionWorker class



enum Shift {
Day,
Night
}



public class ProductionWorker extends Employee {
private Shift shift;
private double hourRate;



//Getters
public double getHourRate() {
return hourRate;
}



public Shift getShift() {
return shift;
}



//Setters
public void setHourRate(double hourRate) {
this.hourRate = hourRate;
}



public void setShift(int shift) {
if (shift ==1 ) {
this.shift = Shift.Day;
} else if (shift == 2) {
this.shift = Shift.Night;
}
}
}

    // New Class
The employee class



import java.util.Date;



public class Employee {



private String eName;
private String sName;
private String eNumber;
private Date hireDate;



public Employee(){};



public Employee(String eName, String sName, String eNumber, Date hireDate){
this.eName = eName;
this.sName = sName;
this.eNumber = eNumber;
this.hireDate = hireDate;
};



//Getters
public Date getHireDate() {
return hireDate;
}



public String geteName() {
return eName;
}



public String getsName() {
return sName;
}



public String geteNumber() {
return eNumber;
}



//Setters



public void seteName(String eName) {
this.eName = eName;
}



public void setsName(String sName) {
this.sName = sName;
}



public void seteNumber(String eNumber) {
this.eNumber = eNumber;
}



public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
}
Neel Mehta
  • 29
  • 4
  • 1
    Do not mix `nextLine` with the other `nextXXX` methods, it will not work the way you expect (see the linked duplicate). Your user input comes as a full line of input, hence you also have to read a full line each time. So instead do `Double.parseDouble(scanner.nextLine())` and similar. – Zabuzard May 24 '21 at 20:39
  • Can you please explain what you mean by not mixing the next line? Where should I modify my code so it works correctly? – Neel Mehta May 24 '21 at 20:55
  • 1
    Have you read the linked duplicate in detail? It explains everything. If there is still something unclear, come back with a more concrete question please. – Zabuzard May 24 '21 at 21:00
  • I searched it and I did not understand :( I'll include all my classes in the code. If possible run it in IDE. You will find that while running code it does not take an employee's name and goes to the next line!! If you can assist me to edit my code in order that it runs properly. I'll really appreciate your help. – Neel Mehta May 24 '21 at 21:20
  • _"I did not understand"_ is not really what I would call a concrete and precise description of what remains unclear after reading the duplicate. So I am still convinced that you did not really read it and merely just skimmed over it. – Zabuzard May 24 '21 at 22:04

0 Answers0