Title: Employee Extends Date
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Employee extends Date

Employee.java (not a good approach)

package advanced_classes;

/**
We have violated the "IS A" relationship
*/
public class Employee extends Date
{
   private String name;

   public Employee( )
   {
       super("January", 1, 1000);
        name = "No name";
   }

   /**
    Precondition: Neither theName nor theDate is null.
   */
   public Employee(String theName, Date theDate)
   {
       super(theDate);
       if (theName == null || theDate == null)
       {
            System.out.println("Fatal Error creating employee.");
            System.exit(0);
       }
       name = theName;
   }

   public Employee(Employee originalObject)
   {
       super(originalObject.getHireDate());
        name = originalObject.name;
   }

   public String getName( )
   {
       return name;
   }

   public Date getHireDate( )
   {
       return new Date(getMonth(), getDay(), getYear());
   }

   /**
    Precondition newName is not null.
   */
   public void setName(String newName)
   {
       if (newName == null)
       {
            System.out.println("Fatal Error setting employee name.");
            System.exit(0);
       }
      else
           name = newName;
   }

   /**
    Precondition newDate is not null.
   */
   public void setHireDate(Date newDate)
   {
       if (newDate == null)
       {
            System.out.println("Fatal Error setting employee hire date.");
            System.exit(0);
       }
       else
       {
           setMonth(newDate.getMonth());
           setDay(newDate.getDay());
           setYear(newDate.getYear());

       }
   }


   public String toString( )
   {
       return (name + " " + getHireDate().toString( ));
   }


   public boolean equals(Employee otherEmployee)
   {
       return (name.equals(otherEmployee.name)
                      && getHireDate().equals(otherEmployee.getHireDate()));
   }
}




Last Modified: October 24, 2013 1:30 PM