I am having problems with the output of my program. The progam should 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 inherits from 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 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 writing a program that uses a ProductionWorker object
Employee Class
import java.util.*;
public class Employee
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
/**
This constructor initializes an object with a name,
employee number, and hire date.
@param n The employee's name.
@param e The employee's number.
@param h The employee's hire date.
*/
public Employee(String n, String e, String h)
{
name = n;
employeeNumber = e;
hireDate = h;
}
/**
The no-arg constructor initializes an object with
null strings for name, employee number, and hire
date.
*/
public Employee()
{
name = "";
employeeNumber = "";
hireDate = "";
}
/**
The setName method sets the employee's name.
@param n The employee's name.
*/
public void setName(String n)
{
name = n;
}
/**
The setEmployeeNumber method sets the employee's
number.
@param e The employee's number.
*/
public void setEmployeeNumber(String e)
{
if (isValidEmpNum(e))
{
employeeNumber = e;
}
else
{
employeeNumber = "";
}
}
/**
The setHireDate method sets the employee's
hire date.
@param h The employee's hire date.
*/
public void setHireDate(String h)
{
hireDate = h;
}
/**
The getName method returns the employee's name.
@return The employee's name.
*/
public String getName()
{
return name;
}
/**
The getEmployeeNumber method returns the
employee's number.
@return The employee's number.
*/
public String getEmployeeNumber()
{
return employeeNumber;
}
/**
The getHireDate method returns the
employee's hire date.
@return The employee's hire date.
*/
public String getHireDate()
{
return hireDate;
}
/**
isValidEmpNum is a private method that
determines whether a string is a valid
employee number.
@param e The string containing an employee
number.
@return true if e references a valid ID number,
false otherwise.
*/
private boolean isValidEmpNum(String e)
{
boolean status = true;
if (e.length() != 5)
status = false;
else
{
if ((!Character.isDigit(e.charAt(0))) ||
(!Character.isDigit(e.charAt(1))) ||
(!Character.isDigit(e.charAt(2))) ||
(e.charAt(3) != '-') ||
(!Character.isLetter(e.charAt(4))) ||
//needs to check if between A and M
(!(e.charAt(4)>= 'A' && e.charAt(4)<= 'M')))
{
status = false;
}
}
return status;
}
/**
toString method
@return A reference to a String representation of
the object.
*/
public String toString()
{
String str = "Name: " + name + "\nEmployee Number: ";
if (employeeNumber == "")
{
str += "INVALID EMPLOYEE NUMBER";
}
else
{
str += employeeNumber;
}
str += ("\nHire Date: " + hireDate);
return str;
}
}
ProductionWorkerA Class
import java.util.*;
import java.text.DecimalFormat;
import java.text.DateFormat;
public class ProductionWorkerA extends EmployeeA
{
// Constants for the day and night shifts.
public static final int DAY_SHIFT = 1;
public static final int NIGHT_SHIFT = 2;
private int shift; // The employee's shift
private double payRate; // The employee's pay rate
/**
This constructor initializes an object with a name,
employee number, hire date, shift, and pay rate
@param n The employee's name.
@param num The employee's number.
@param date The employee's hire date.
@param sh The employee's shift.
@param rate The employee's pay rate.
*/
public ProductionWorkerA(String n, String num, String date,
int sh, double rate)
{
super(n, num, date);
shift = sh;
payRate = rate;
}
/**
The no-arg constructor initializes an object with
null strings for name, employee number, and hire
date. The day shift is selected, and the pay rate
is set to 0.0.
*/
public ProductionWorkerA()
{
super();
shift = DAY_SHIFT;
payRate = 0.0;
}
/**
The setShift method sets the employee's shift.
@param s The employee's shift.
*/
public void setShift(int s)
{
shift = s;
}
/**
The setPayRate method sets the employee's pay rate.
@param p The employee's pay rate.
*/
public void setPayRate(double p)
{
payRate = p;
}
/**
The getShift method returns the employee's shift.
@return The employee's shift.
*/
public int getShift()
{
return shift;
}
/**
The getPayRate method returns the employee's pay rate.
@return The employee's pay rate.
*/
public double getPayRate()
{
return payRate;
}
/**
toString method
@return A reference to a String representation of
the object.
*/
public String toString()
{
DecimalFormat dollar = new DecimalFormat("#,##0.00");
String str = super.toString();
str += "\nShift: ";
if (shift == DAY_SHIFT)
str += "Day";
else if (shift == NIGHT_SHIFT)
str += "Night";
else
str += "INVALID SHIFT NUMBER";
str += ("\nHourly Pay Rate: $" +
dollar.format(payRate));
return str;
}
}
ProductionWorkerDemo
import java.util.*;
import java.text.DecimalFormat;
import java.text.DateFormat;
/**
This program uses the Production Worker class that extends the employee
class.
*/
public class ProductionWorkerDemo
{
public static void main(String[] args)
{
String name; // Employee name
String employeeNumber; // Employee number
String hireDate; // Employee hire date
int shift; // Employee shift
double payRate; // Employee pay
// Creates Scanner object
Scanner s = new Scanner(System.in);
// Gets the user's name.
System.out.println("Enter your name: ");
name = s.nextLine();
// Gets the user's employee number.
System.out.println("Enter your employee number: ");
employeeNumber = s.nextLine();
// Gets the user's hire date.
System.out.println("Enter your hire date: ");
hireDate = s.nextLine();
// Gets the user's payrate.
System.out.println("Enter your payrate: ");
payRate = s.nextDouble();
// Gets the user's shift.
System.out.println("Enter your shift: ");
shift = s.nextInt();
// Creates an Production worker object.
ProductionWorkerA pw =
new ProductionWorkerA();
System.out.println();
System.out.println("Name: " + pw.getName());
System.out.println("Employee Number: " + pw.getEmployeeNumber());
System.out.println("Hire Date: " + pw.getHireDate());
System.out.println("Pay Rate: " + pw.getPayRate());
System.out.println("Shift: " + pw.getShift());
}
}
Output when I run the ProductionWorkerDemo
jGRASP exec: java ProductionWorkerDemo
Enter your name:
Glen
Enter your employee number:
333-l
Enter your hire date:
12/20/2009
Enter your payrate:
12.45
Enter your shift:
2
Name:
Employee Number:
Hire Date:
Pay Rate: 0.0
Shift: 1