Java Programming Lab Employee and ProductionWorker Classes Design a class named
ID: 3680346 • Letter: J
Question
Java Programming Lab
Employee and ProductionWorker Classes
Design a class named Employee. The class should keep the following information in fields:
* Employee name
* 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.
* 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:
* Shift (An integer)
* 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. Demonstrate the classes by writting a program that uses a ProductionWorker object.
Explanation / Answer
ProductionWorkerMain.java
import java.text.DecimalFormat;
import java.util.Scanner;
class Emp {
public String nm;//empname
public String num;//empno
public String hireDt;//hiredt
private boolean validEmpNumber = false;
//constructor method
Emp(String n, String nu, String hd) {
nm = n;
num = nu;
hireDt = hd;
}
public String getName() {
return nm;
}
public String getNumber() {
while (validEmpNumber = false) {
if (num.length() != 5) {
System.out.println("Invalid employee number, please try again.");
} else if (((!Character.isDigit(num.charAt(0))) || ((!Character.isDigit(num.charAt(1))))
|| ((!Character.isDigit(num.charAt(2)))) || (num.charAt(3) != '-')
|| (!(num.charAt(4) >= 'A' && num.charAt(4) <= 'M')))) {
System.out.println("Invalid employee number, please try again.");
System.out.println("Enter your employee number: ");
} else {
validEmpNumber = true;
}
}
return num;
}
public String getHireDate() {
return hireDt;
}
// * Checking validity of employee number
// *
public boolean isValidEmployeeNumber(String number) {
boolean status = true;
if (number.length() != 5) {
System.out.println("Invalid employee number.");
} else {
if (((!Character.isDigit(number.charAt(0))) || ((!Character.isDigit(number.charAt(1))))
|| ((!Character.isDigit(number.charAt(2)))) || (number.charAt(3) != '-')
|| ((!Character.isDigit(number.charAt(4))))
|| (!(number.charAt(4) >= 'A' && number.charAt(4) <= 'M')))) {
status = false;
}
}
return status;
}
public String toString() {
String str = "Name: " + nm + " Employee Number: ";
if (num == "") {
str += "Invalid Employee Number";
} else {
str += num;
}
str += (" Hire Date: " + hireDt);
return str;
}
}
class ProductionWorker extends Emp //productioworker is the subclass of emp
{
public static int dayShift = 1;
public static int nightShift = 2;
private int shift;
private double payRate;
private boolean rightShift = false;
public String getShift() //EDIT#2: FIXED (this will be good enough for me!)
{
String shiftTime;
if (shift == 1) {
shiftTime = "Day";
} else if (shift == 2) {
shiftTime = "Night";
} else if (shift == 3) {
shiftTime = "Alternating";
} else {
shiftTime = "On Call";
}
return shiftTime;
}
public void setShift(int s) {
shift = s;
}
public double getPayRate() {
return payRate;
}
public void setPayRate(double rate) {
payRate = rate;
}
/**
* Constructor initializes a name, number and hire date
*/
public ProductionWorker(//String name, String number, String hireDate,
String n, String num, String hd, int sh, double rate) {
super(n, num, hd);
shift = sh;
payRate = rate;
}
public ProductionWorker(String n, String num, String hd) {
super(n, num, hd);
shift = dayShift;
payRate = 0.0;
}
public String toString() {
DecimalFormat dollar = new DecimalFormat("#,##0.00");
String str = super.toString();
str += " Shift: ";
if (shift == dayShift) {
str += "Day";
} else if (shift == nightShift) {
str += "Nights";
} else {
str += "Invalid Shift Number";
}
str += (" Hourly Pay Rate: $"
+ dollar.format(payRate));
return str;
}
}
public class ProductionWorkerMain {
public static void main(String[] args) {//driver program
String nm;
String n;
String numb;
String num;
String hireDate;
String hd;
int shift;
double payRate;
double rate;
Scanner s = new Scanner(System.in);
System.out.println("Enter your name: ");
nm = s.nextLine();
System.out.println("Enter your employee number: ");
System.out.println("(Format: NNN-L)");
numb = s.nextLine();
System.out.println("Enter your hire date: ");
hireDate = s.nextLine();
System.out.println("Enter your payrate: ");
payRate = s.nextDouble();
System.out.println("Enter your shift (day=1, night=2): ");
shift = s.nextInt();
//Production worker object
ProductionWorker ProductionWorker = new ProductionWorker(nm, numb,
hireDate, shift, payRate);
System.out.println("---------- Employee Information -----------");
System.out.println("Name: " + ProductionWorker.getName());
System.out.println("Employee Number: " + ProductionWorker.getNumber());
System.out.println("Hire Date: " + ProductionWorker.getHireDate());
System.out.println("Pay Rate: " + ProductionWorker.getPayRate());
System.out.println("Shift: " + ProductionWorker.getShift());
}
}
output
Enter your name:
MIKE
Enter your employee number:
(Format: NNN-L)
123-L
Enter your hire date:
12-MAR-2010
Enter your payrate:
55555
Enter your shift (day=1, night=2):
2
---------- Employee Information -----------
Name: MIKE
Employee Number: 123-L
Hire Date: 12-MAR-2010
Pay Rate: 55555.0
Shift: Night
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.