Hi,

I'm supposed to write a program using super and subclasses to calculate monthly payroll for 5 employees. There are 4 types of employees, salaried, hourly, commissioned, and base plus commissioned. Anyway, I've gotten it to build with no errors and it runs almost like it should but since it prompts for the employees' information multiple times, it only keeps the last info entered for any given field. Can anyone help me figure out how to make it use each set of inputs separately instead of only keeping the most recent input? Below is my main class, followed by my super class and driver classes. I wasn't sure if it would help to see all of that.

// PayrollSystemTest.java
// Employee hierarchy test program.

import javax.swing.JOptionPane;
public class PayrollSystemTest 
{
   public static void main( String args[] ) 
   {
	   String first = "";
		String last = "";
		String ssn = "";
		String salaryString;
		double salary = 0;
		String wageString;
		double wage = 0;
		String hoursString;
		int hours = 0;
		String salesString;
		double sales = 0;
		String rateString;
		double rate = 0;
		String empTypeString = "";
		int empType = 0;
		int count;
		
		for(count = 0; count < 5; count++)
		{
			empTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE); 
			empType = Integer.parseInt(empTypeString);
			
			switch(empType)
			{
			case 1:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			case 2:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
				wage = Double.parseDouble(wageString);
				hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
				hours = Integer.parseInt(hoursString);
				break;
				
			case 3:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				break;
				
			case 4:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			default:
				JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
				break;
			}
				
		}	
     
		
		 // create subclass objects
      SalariedEmployee salariedEmployee = 
         new SalariedEmployee(first, last, ssn, salary);
      HourlyEmployee hourlyEmployee = 
         new HourlyEmployee(first, last, ssn, wage, hours);
      CommissionEmployee commissionEmployee = 
         new CommissionEmployee(first, last, ssn, sales, rate);
      BasePlusCommissionEmployee basePlusCommissionEmployee = 
         new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);
			
		
			
					
      // create four-element Employee array
      Employee employees[] = new Employee[ 4 ]; 

      // initialize array with Employees
      employees[ 0 ] = salariedEmployee;
      employees[ 1 ] = hourlyEmployee;
      employees[ 2 ] = commissionEmployee; 
      employees[ 3 ] = basePlusCommissionEmployee;

      System.out.println( "Employees processed polymorphically:\n" );
      
      // generically process each element in array employees
      for ( Employee currentEmployee : employees ) 
      {
         System.out.println( currentEmployee ); // invokes toString

         // determine whether element is a BasePlusCommissionEmployee
         if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
         {
            // downcast Employee reference to 
            // BasePlusCommissionEmployee reference
            BasePlusCommissionEmployee employee = 
               ( BasePlusCommissionEmployee ) currentEmployee;

            double oldBaseSalary = employee.getBaseSalary();
            employee.setBaseSalary( 1.10 * oldBaseSalary );
            System.out.printf( 
               "new base salary with 10%% increase is: $%,.2f\n",
               employee.getBaseSalary() );
         } // end if

         System.out.printf( 
            "earned $%,.2f\n\n", currentEmployee.earnings() );
      } // end for

      // get type name of each object in employees array
      for ( int j = 0; j < employees.length; j++ )
         System.out.printf( "Employee %d is a %s\n", j, 
            employees[ j ].getClass().getName() ); 
   } // end main
} // end class PayrollSystemTest
// Employee.java
// Employee abstract superclass.

public abstract class Employee 
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;
	private String birthDate;
	
   // three-argument constructor
   public Employee( String first, String last, String ssn )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;	
   } // end three-argument Employee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      lastName = last;
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      socialSecurityNumber = ssn; // should validate
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   } // end method getSocialSecurityNumber

   // return String representation of Employee object
   public String toString()
   {
      return String.format( "%s %s\nsocial security number: %s", 
         getFirstName(), getLastName(), getSocialSecurityNumber() );
   } // end method toString
	
   // abstract method overridden by subclasses
   public abstract double earnings(); // no implementation here
} // end abstract class Employee
// SalariedEmployee.java
// SalariedEmployee class extends Employee.

public class SalariedEmployee extends Employee 
{
   private double weeklySalary;

   // four-argument constructor
   public SalariedEmployee( String first, String last, String ssn, 
      double salary )
   {
      super( first, last, ssn ); // pass to Employee constructor
      setWeeklySalary( salary ); // validate and store salary
   } // end four-argument SalariedEmployee constructor
	
	// set salary
   public void setWeeklySalary( double salary )
   {
      weeklySalary = salary < 0.0 ? 0.0 : salary;
   } // end method setWeeklySalary

   // return salary
   public double getWeeklySalary()
   {
      return weeklySalary;
   } // end method getWeeklySalary

   // calculate earnings; override abstract method earnings in Employee
   public double earnings()
   {
      return getWeeklySalary() * 4;
   } // end method earnings

   // return String representation of SalariedEmployee object
   public String toString()
   {
      return String.format( "salaried employee: %s\n%s: $%,.2f", 
         super.toString(), "weekly salary", getWeeklySalary() );
   } // end method toString
} // end class SalariedEmployee
// HourlyEmployee.java
// HourlyEmployee class extends Employee.

public class HourlyEmployee extends Employee 
{
   private double wage; // wage per hour
   private double hours; // hours worked for week

   // five-argument constructor
   public HourlyEmployee( String first, String last, String ssn, 
      double hourlyWage, double hoursWorked )
   {
      super( first, last, ssn );
      setWage( hourlyWage ); // validate and store hourly wage
      setHours( hoursWorked ); // validate and store hours worked
   } // end five-argument HourlyEmployee constructor

   // set wage
   public void setWage( double hourlyWage )
   {
      wage = ( hourlyWage < 0.0 ) ? 0.0 : hourlyWage;
   } // end method setWage

   // return wage
   public double getWage()
   {
      return wage;
   } // end method getWage

   // set hours worked
   public void setHours( double hoursWorked )
   {
      hours = ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) ) ?
         hoursWorked : 0.0;
   } // end method setHours

   // return hours worked
   public double getHours()
   {
      return hours;
   } // end method getHours

   // calculate earnings; override abstract method earnings in Employee
   public double earnings()
   {
      if ( getHours() <= 40 ) // no overtime
         return getWage() * getHours() * 4;
      else
         return (40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5) * 4;
   } // end method earnings

   // return String representation of HourlyEmployee object
   public String toString()
   {
      return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f", 
         super.toString(), "hourly wage", getWage(), 
         "hours worked", getHours() );
   } // end method toString
} // end class HourlyEmployee
// CommissionEmployee.java
// CommissionEmployee class extends Employee.

public class CommissionEmployee extends Employee 
{
   private double grossSales; // gross weekly sales
   private double commissionRate; // commission percentage

   // five-argument constructor
   public CommissionEmployee( String first, String last, String ssn, 
      double sales, double rate )
   {
      super( first, last, ssn );
      setGrossSales( sales );
      setCommissionRate( rate );
   } // end five-argument CommissionEmployee constructor

   // set commission rate
   public void setCommissionRate( double rate )
   {
      commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
   } // end method setCommissionRate

   // return commission rate
   public double getCommissionRate()
   {
      return commissionRate;
   } // end method getCommissionRate

   // set gross sales amount
   public void setGrossSales( double sales )
   {
      grossSales = ( sales < 0.0 ) ? 0.0 : sales;
   } // end method setGrossSales

   // return gross sales amount
   public double getGrossSales()
   {
      return grossSales;
   } // end method getGrossSales

   // calculate earnings; override abstract method earnings in Employee
   public double earnings()
   {
      return getCommissionRate() * getGrossSales() * 4;
   } // end method earnings

   // return String representation of CommissionEmployee object
   public String toString()
   {
      return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f", 
         "commission employee", super.toString(), 
         "gross sales", getGrossSales(), 
         "commission rate", getCommissionRate() );
   } // end method toString
} // end class CommissionEmployee
// BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class extends CommissionEmployee.

public class BasePlusCommissionEmployee extends CommissionEmployee 
{
   private double baseSalary; // base salary per week

   // six-argument constructor
   public BasePlusCommissionEmployee( String first, String last, 
      String ssn, double sales, double rate, double salary )
   {
      super( first, last, ssn, sales, rate );
      setBaseSalary( salary ); // validate and store base salary
   } // end six-argument BasePlusCommissionEmployee constructor

   // set base salary
   public void setBaseSalary( double salary )
   {
      baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative
   } // end method setBaseSalary

   // return base salary
   public double getBaseSalary()
   {
      return baseSalary;
   } // end method getBaseSalary

   // calculate earnings; override method earnings in CommissionEmployee
   public double earnings()
   {
      return getBaseSalary() * 4 + super.earnings();
   } // end method earnings

   // return String representation of BasePlusCommissionEmployee object
   public String toString()
   {
      return String.format( "%s %s; %s: $%,.2f", 
         "base-salaried", super.toString(), 
         "base salary", getBaseSalary() );
   } // end method toString   
} // end class BasePlusCommissionEmployee

Try to use a tree structure instead of array.

The assignment requires the use of a subclass array.

The assignment requires the use of a subclass array.

Here are the instructions for this assignment I'm working on.

1) Develop a Java application to calculate the monthly paychecks for a number of different types of employees. The employee types are created in a subclass array based on parent base class Employee. Initial code is provided for each class and for a driver class.

You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory.

You should first compile and execute the original classes and then make your customer modifications for this program.

You should compile Date.java first, then Employee.java, then CommissionEmployee.java, then BasePlusCommission.java, then HourlyEmployee.java, then SalariedEmployee.java, and finally PayrollSystemTest.java. And maintain this compilation order when you make your customized modifications to these classes later.

As part of the modifications you need to make to the supplied code, you are to prompt the user for all the data that is now provided in the code for the class PayrollSystemTest (i.e. instead of using these values from their declaration in the code, get data from prompting the user for it, one value at a time).

In addition you need to add a new private instance field "birthDate" to the Employee class. Add get and set methods to Employee class for this new birthDate field. The birthdate is to be displayed using a customized toDateString method in the Date class. The birthDate field must be determined from separate integer values, supplied by the user, for the month, day, and year. These three parameter values should be supplied to a modified Employee class constructor, where the single birthDate field is initialized.

In class PayrollSystemTest create an array of Employee variables to store references to the various employee objects. In a loop, calculate the monthly paycheck amount for each Employee (polymorphically), and add a $100.00 bonus to the person's monthly payroll amount if the current month (i.e. November) is the month in which the Employee's birthday occurs.

Your program should input data and create the five employees below. Your program must calculate their monthly salaries for November (assumed to be the "current" month). Note that the monthly salary is four times the weekly salary.

a) one Salaried Employee with a weekly salary of $1000 and a birthday of January 1, 1960,
b) one Salaried Employee with a weekly salary of $1000 and a birthday in this month (November 1, 1961)
c) one Commission Employee Worker with a gross weekly sales of $12,000 and a 5% commission rate and a birthday of February 1, 1962,
d) one Base Plus Commission Employee with a gross weekly sales of $10,000, a 3% commission, and a $500 base weekly salary with a birthday of March 1, 1963, and
e) one Hourly Employee with a wage of $20/hour working for 40 hours and a birthday of April 1, 1964

You should make up your own employee names for the five required employees above. Make sure you create the employees in the order above.

In prompting the user for all input values for the five employees above, your program
essentially replaces the existing main() method of PayrollSystemTest.java. You may user whatever input class you want (e.g. JOptionPane, Scanner). Keep all other lines of the code in PayrollSystemTest as they are originally, including the code that gives a 10% increase to the Base Plus Commission employee.

Here is the code provided for the Employee class, Date class, and PayrollSystemTest. Above, you can see what I've done with it so far. I'm not sure if I really even need to do anything inside the other subclasses. I'M SO LOST!!!

// PayrollSystemTest.java
// Employee hierarchy test program.

public class PayrollSystemTest 
{
   public static void main( String args[] ) 
   {
      // create subclass objects
      SalariedEmployee salariedEmployee = 
         new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
      HourlyEmployee hourlyEmployee = 
         new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
      CommissionEmployee commissionEmployee = 
         new CommissionEmployee( 
         "Sue", "Jones", "333-33-3333", 10000, .06 );
      BasePlusCommissionEmployee basePlusCommissionEmployee = 
         new BasePlusCommissionEmployee( 
         "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );

      System.out.println( "Employees processed individually:\n" );
      
      System.out.printf( "%s\n%s: $%,.2f\n\n", 
         salariedEmployee, "earned", salariedEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n",
         hourlyEmployee, "earned", hourlyEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n",
         commissionEmployee, "earned", commissionEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n", 
         basePlusCommissionEmployee, 
         "earned", basePlusCommissionEmployee.earnings() );

      // create four-element Employee array
      Employee employees[] = new Employee[ 4 ]; 

      // initialize array with Employees
      employees[ 0 ] = salariedEmployee;
      employees[ 1 ] = hourlyEmployee;
      employees[ 2 ] = commissionEmployee; 
      employees[ 3 ] = basePlusCommissionEmployee;

      System.out.println( "Employees processed polymorphically:\n" );
      
      // generically process each element in array employees
      for ( Employee currentEmployee : employees ) 
      {
         System.out.println( currentEmployee ); // invokes toString

         // determine whether element is a BasePlusCommissionEmployee
         if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
         {
            // downcast Employee reference to 
            // BasePlusCommissionEmployee reference
            BasePlusCommissionEmployee employee = 
               ( BasePlusCommissionEmployee ) currentEmployee;

            double oldBaseSalary = employee.getBaseSalary();
            employee.setBaseSalary( 1.10 * oldBaseSalary );
            System.out.printf( 
               "new base salary with 10%% increase is: $%,.2f\n",
               employee.getBaseSalary() );
         } // end if

         System.out.printf( 
            "earned $%,.2f\n\n", currentEmployee.earnings() );
      } // end for

      // get type name of each object in employees array
      for ( int j = 0; j < employees.length; j++ )
         System.out.printf( "Employee %d is a %s\n", j, 
            employees[ j ].getClass().getName() ); 
   } // end main
} // end class PayrollSystemTest
// Employee.java
// Employee abstract superclass.

public abstract class Employee 
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;

   // three-argument constructor
   public Employee( String first, String last, String ssn )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
   } // end three-argument Employee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      lastName = last;
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      socialSecurityNumber = ssn; // should validate
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   } // end method getSocialSecurityNumber

   // return String representation of Employee object
   public String toString()
   {
      return String.format( "%s %s\nsocial security number: %s", 
         getFirstName(), getLastName(), getSocialSecurityNumber() );
   } // end method toString

   // abstract method overridden by subclasses
   public abstract double earnings(); // no implementation here
} // end abstract class Employee
// Date.java 
// Date class declaration.

public class Date 
{
   private int month; // 1-12
   private int day;   // 1-31 based on month
   private int year;  // any year

   // constructor: call checkMonth to confirm proper value for month; 
   // call checkDay to confirm proper value for day
   public Date( int theMonth, int theDay, int theYear )
   {
      month = checkMonth( theMonth ); // validate month
      year = theYear; // could validate year
      day = checkDay( theDay ); // validate day

      System.out.printf( 
         "Date object constructor for date %s\n", this );
   } // end Date constructor

   // utility method to confirm proper month value
   private int checkMonth( int testMonth )
   {
      if ( testMonth > 0 && testMonth <= 12 ) // validate month
         return testMonth;
      else // month is invalid 
      { 
         System.out.printf( 
            "Invalid month (%d) set to 1.", testMonth );
         return 1; // maintain object in consistent state
      } // end else
   } // end method checkMonth

   // utility method to confirm proper day value based on month and year
   private int checkDay( int testDay )
   {
      int daysPerMonth[] = 
         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   
      // check if day in range for month
      if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
         return testDay;
   
      // check for leap year
      if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
           ( year % 4 == 0 && year % 100 != 0 ) ) )
         return testDay;
   
      System.out.printf( "Invalid day (%d) set to 1.", testDay );
      return 1;  // maintain object in consistent state
   } // end method checkDay
   
   // return a String of the form month/day/year
   public String toString()
   { 
      return String.format( "%d/%d/%d", month, day, year ); 
   } // end method toString
} // end class Date

Edited 6 Years Ago by BobbieJean: n/a

Hi,

I was given a code for an assignment to modify. It has a main class, a super class, a few subclasses, and a date class that doesn't really seem like a subclass, just another class. I feel like I have tried everything I can think of and I just don't know what to do at all so I put it back to the original code. Here's the instructions for the part that's giving me trouble and part of the original code that I am supposed to modify.

"You need to add a new private instance field "birthDate" to the Employee class. Add get and set methods to Employee class for this new birthDate field. The birthdate is to be displayed using a customized toDateString method in the Date class. The birthDate field must be determined from separate integer values, supplied by the user, for the month, day, and year. These three parameter values should be supplied to a modified Employee class constructor, where the single birthDate field is initialized."

(My instructor says this is the easy part but I just don't see it!)

// PayrollSystemTest.java
// Employee hierarchy test program.

public class PayrollSystemTest 
{
   public static void main( String args[] ) 
   {
      // create subclass objects
      SalariedEmployee salariedEmployee = 
         new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
      HourlyEmployee hourlyEmployee = 
         new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
      CommissionEmployee commissionEmployee = 
         new CommissionEmployee( 
         "Sue", "Jones", "333-33-3333", 10000, .06 );
      BasePlusCommissionEmployee basePlusCommissionEmployee = 
         new BasePlusCommissionEmployee( 
         "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );

      System.out.println( "Employees processed individually:\n" );
      
      System.out.printf( "%s\n%s: $%,.2f\n\n", 
         salariedEmployee, "earned", salariedEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n",
         hourlyEmployee, "earned", hourlyEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n",
         commissionEmployee, "earned", commissionEmployee.earnings() );
      System.out.printf( "%s\n%s: $%,.2f\n\n", 
         basePlusCommissionEmployee, 
         "earned", basePlusCommissionEmployee.earnings() );

      // create four-element Employee array
      Employee employees[] = new Employee[ 4 ]; 

      // initialize array with Employees
      employees[ 0 ] = salariedEmployee;
      employees[ 1 ] = hourlyEmployee;
      employees[ 2 ] = commissionEmployee; 
      employees[ 3 ] = basePlusCommissionEmployee;

      System.out.println( "Employees processed polymorphically:\n" );
      
      // generically process each element in array employees
      for ( Employee currentEmployee : employees ) 
      {
         System.out.println( currentEmployee ); // invokes toString

         // determine whether element is a BasePlusCommissionEmployee
         if ( currentEmployee instanceof BasePlusCommissionEmployee ) 
         {
            // downcast Employee reference to 
            // BasePlusCommissionEmployee reference
            BasePlusCommissionEmployee employee = 
               ( BasePlusCommissionEmployee ) currentEmployee;

            double oldBaseSalary = employee.getBaseSalary();
            employee.setBaseSalary( 1.10 * oldBaseSalary );
            System.out.printf( 
               "new base salary with 10%% increase is: $%,.2f\n",
               employee.getBaseSalary() );
         } // end if

         System.out.printf( 
            "earned $%,.2f\n\n", currentEmployee.earnings() );
      } // end for

      // get type name of each object in employees array
      for ( int j = 0; j < employees.length; j++ )
         System.out.printf( "Employee %d is a %s\n", j, 
            employees[ j ].getClass().getName() ); 
   } // end main
} // end class PayrollSystemTest
// Employee.java
// Employee abstract superclass.

public abstract class Employee 
{
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;

   // three-argument constructor
   public Employee( String first, String last, String ssn )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
   } // end three-argument Employee constructor

   // set first name
   public void setFirstName( String first )
   {
      firstName = first;
   } // end method setFirstName

   // return first name
   public String getFirstName()
   {
      return firstName;
   } // end method getFirstName

   // set last name
   public void setLastName( String last )
   {
      lastName = last;
   } // end method setLastName

   // return last name
   public String getLastName()
   {
      return lastName;
   } // end method getLastName

   // set social security number
   public void setSocialSecurityNumber( String ssn )
   {
      socialSecurityNumber = ssn; // should validate
   } // end method setSocialSecurityNumber

   // return social security number
   public String getSocialSecurityNumber()
   {
      return socialSecurityNumber;
   } // end method getSocialSecurityNumber

   // return String representation of Employee object
   public String toString()
   {
      return String.format( "%s %s\nsocial security number: %s", 
         getFirstName(), getLastName(), getSocialSecurityNumber() );
   } // end method toString

   // abstract method overridden by subclasses
   public abstract double earnings(); // no implementation here
} // end abstract class Employee
// Date.java 
// Date class declaration.

public class Date 
{
   private int month; // 1-12
   private int day;   // 1-31 based on month
   private int year;  // any year

   // constructor: call checkMonth to confirm proper value for month; 
   // call checkDay to confirm proper value for day
   public Date( int theMonth, int theDay, int theYear )
   {
      month = checkMonth( theMonth ); // validate month
      year = theYear; // could validate year
      day = checkDay( theDay ); // validate day

      System.out.printf( 
         "Date object constructor for date %s\n", this );
   } // end Date constructor

   // utility method to confirm proper month value
   private int checkMonth( int testMonth )
   {
      if ( testMonth > 0 && testMonth <= 12 ) // validate month
         return testMonth;
      else // month is invalid 
      { 
         System.out.printf( 
            "Invalid month (%d) set to 1.", testMonth );
         return 1; // maintain object in consistent state
      } // end else
   } // end method checkMonth

   // utility method to confirm proper day value based on month and year
   private int checkDay( int testDay )
   {
      int daysPerMonth[] = 
         { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   
      // check if day in range for month
      if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
         return testDay;
   
      // check for leap year
      if ( month == 2 && testDay == 29 && ( year % 400 == 0 || 
           ( year % 4 == 0 && year % 100 != 0 ) ) )
         return testDay;
   
      System.out.printf( "Invalid day (%d) set to 1.", testDay );
      return 1;  // maintain object in consistent state
   } // end method checkDay
   
   // return a String of the form month/day/year
   public String toString()
   { 
      return String.format( "%d/%d/%d", month, day, year ); 
   } // end method toString
} // end class Date

Hi,
"You need to add a new private instance field "birthDate" to the Employee class. Add get and set methods to Employee class for this new birthDate field. The birthdate is to be displayed using a customized toDateString method in the Date class. The birthDate field must be determined from separate integer values, supplied by the user, for the month, day, and year. These three parameter values should be supplied to a modified Employee class constructor, where the single birthDate field is initialized."

(My instructor says this is the easy part but I just don't see it!)

your instructor is right. this is very easy, and the instructions tell you (step by step) what to do.
the only classes you'll need to adjust are the Employee class and the Date class.
first, you add a var birthDate (with its setter and getter) to the Employee class
indeed, Date is not a subclass to Employee, but there are (if I'm correct some sublasses of Employee in your app, those will also (through inheritance) get access to birthDate (through its setter and getter)
the next thing you need to do, is to add a method in Date, through which you are going to print your date, as a formatted String. so, you send your birthDate as parameter to that method, and use printf (or something similar) to print it
lastly, you create a new constructor in Employee, which takes 3 integers as parameters. using these integers, which the constructor will set as the day, month and year of the date, and will declare and instantiate the birtDate.

I don't know whether I can make it more obvious than this, without just writing the code, so I hope this helps. if not, try and show us what you think it is or might be

It seems to me like your problem is in your main method in your first post... I haven't read through your other classes but the way you are getting info from the user is as follows:

for (count = 0; count < 5; count++)
{
    //get temporary info from user
}
//create objects from temporary info

You keep getting the info for creating an object and then you discard it, and you end up only creating objects based on the last info. Instead, try this:

public static void main( String args[] ) 
   {
                // create four-element Employee array
                Employee employees[] = new Employee[ 4 ];

	        String first = "";
		String last = "";
		String ssn = "";
		String salaryString;
		double salary = 0;
		String wageString;
		double wage = 0;
		String hoursString;
		int hours = 0;
		String salesString;
		double sales = 0;
		String rateString;
		double rate = 0;
		String empTypeString = "";
		int empType = 0;
		int count;
		
		for(count = 0; count < 5; count++)
		{
			empTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE); 
			empType = Integer.parseInt(empTypeString);
			
			switch(empType)
			{
			case 1:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			case 2:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
				wage = Double.parseDouble(wageString);
				hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
				hours = Integer.parseInt(hoursString);
				break;
				
			case 3:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				break;
				
			case 4:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			default:
				JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
				break;
			}
		
		        switch (count)
                        {
                              case 0: Employees[0] = new SalariedEmployee(first, last, ssn, salary);break;
                              case 1: Employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);break;
                              case 2: Employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);break;
                              case 3: Employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);break;
                             //I don't know whether you want a fifth case, since you run through the loop 5 times but only create 4 objects...
                        }		
		}

     //More syntax here...
     }

Hopefully this made some sense...

You changed your main method in the second post and I can't really follow it all without comments. But in your first post, what I said above seems to be the problem.

Edited 6 Years Ago by kvass: n/a

Comments
Very helpful! Very well explained!

your instructor is right. this is very easy, and the instructions tell you (step by step) what to do.
the only classes you'll need to adjust are the Employee class and the Date class.
first, you add a var birthDate (with its setter and getter) to the Employee class
indeed, Date is not a subclass to Employee, but there are (if I'm correct some sublasses of Employee in your app, those will also (through inheritance) get access to birthDate (through its setter and getter)
the next thing you need to do, is to add a method in Date, through which you are going to print your date, as a formatted String. so, you send your birthDate as parameter to that method, and use printf (or something similar) to print it
lastly, you create a new constructor in Employee, which takes 3 integers as parameters. using these integers, which the constructor will set as the day, month and year of the date, and will declare and instantiate the birtDate.

I don't know whether I can make it more obvious than this, without just writing the code, so I hope this helps. if not, try and show us what you think it is or might be

Thank you very much! I think sometimes it just takes wording it differently to make a big difference. I'll see what I can do with what you said in mind and if nothing else, I'll do exactly what you said and show you guys what I think it should be. Honestly, I think it just comes down to my not fully understanding inheritance among other things. This class is Advanced Programming I and I am also taking Calculus. (On top of that, it's set up in terms, 9 weeks, instead of semesters, 16 weeks. Same amount of info, half the time...) If you've ever taken Calc, then you may remember that the problems involved for that class are also time-consuming so I don't get to spend as much time thoroughly reading my chapters for programming as I would like/need to. I think if programming were my only class, I'd have no problem. Anyway, sorry if I'm not grasping something that should be easy. The most (java) classes I've dealt with so far for one program is two or three at the most. All together this one has, I think, seven so it's definitely more "elements" (for lack of better words) of one program than I've done before which seems to make it harder to understand and harder to know which part is not right. Anyway, sorry to all if my questions seem stupid...It's not a good excuse but it's the truth that I am definitely a bit overwhelmed...Sorry again.

your instructor is right. this is very easy, and the instructions tell you (step by step) what to do.
the only classes you'll need to adjust are the Employee class and the Date class.
first, you add a var birthDate (with its setter and getter) to the Employee class
indeed, Date is not a subclass to Employee, but there are (if I'm correct some sublasses of Employee in your app, those will also (through inheritance) get access to birthDate (through its setter and getter)
the next thing you need to do, is to add a method in Date, through which you are going to print your date, as a formatted String. so, you send your birthDate as parameter to that method, and use printf (or something similar) to print it
lastly, you create a new constructor in Employee, which takes 3 integers as parameters. using these integers, which the constructor will set as the day, month and year of the date, and will declare and instantiate the birtDate.

I don't know whether I can make it more obvious than this, without just writing the code, so I hope this helps. if not, try and show us what you think it is or might be

I already replied once to your answer thanking you but I do still have one quick question. When you say "create a new constructor in Employee, which takes 3 integers as parameters. using these integers, which the constructor will set as the day, month and year of the date, and will declare and instantiate the birtDate.", is that in addition to what is already in the Employee constructor? I'm thinking that, "yes" it is in addition to that but, you said a "new" constructor so I just wanted to clarify if I do indeed need to keep what is already in that constructor or completely replace it with the date stuff. I'm pretty sure you meant "in addition to" and even typing this out I REALLY fee like that's a stupid question but I have gotten so confused with all the other stuff that I didn't want to misunderstand something (even more) simple than what I originally asked about. Sorry. Just trying to cover all my bases.

It seems to me like your problem is in your main method in your first post... I haven't read through your other classes but the way you are getting info from the user is as follows:

for (count = 0; count < 5; count++)
{
    //get temporary info from user
}
//create objects from temporary info

You keep getting the info for creating an object and then you discard it, and you end up only creating objects based on the last info. Instead, try this:

public static void main( String args[] ) 
   {
                // create four-element Employee array
                Employee employees[] = new Employee[ 4 ];

	        String first = "";
		String last = "";
		String ssn = "";
		String salaryString;
		double salary = 0;
		String wageString;
		double wage = 0;
		String hoursString;
		int hours = 0;
		String salesString;
		double sales = 0;
		String rateString;
		double rate = 0;
		String empTypeString = "";
		int empType = 0;
		int count;
		
		for(count = 0; count < 5; count++)
		{
			empTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE); 
			empType = Integer.parseInt(empTypeString);
			
			switch(empType)
			{
			case 1:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			case 2:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
				wage = Double.parseDouble(wageString);
				hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
				hours = Integer.parseInt(hoursString);
				break;
				
			case 3:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				break;
				
			case 4:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			default:
				JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
				break;
			}
		
		        switch (count)
                        {
                              case 0: Employees[0] = new SalariedEmployee(first, last, ssn, salary);break;
                              case 1: Employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);break;
                              case 2: Employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);break;
                              case 3: Employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);break;
                             //I don't know whether you want a fifth case, since you run through the loop 5 times but only create 4 objects...
                        }		
		}

     //More syntax here...
     }

Hopefully this made some sense...

You changed your main method in the second post and I can't really follow it all without comments. But in your first post, what I said above seems to be the problem.

Hey,

Sorry about the confusion. The SECOND code I posted was the original code that my instructor provided me to modify to do what is required for the assignment. I had FIRST posted the code the way I had modified it so far. (Since I'm pretty lost about what I'm doing here, that would make sense for it to be hard for you to follow. LOL) Anyway, The second one shows what I started with, and the first shows, basically what I have tried. My question for you, though, is (and I know, it may be a stupid one but my brain is just fried on this one): I may have missed something but, I didn't see anything different. So, how do I get it to not discard the info that I get each time. (Again, I've been up all night working on this so my brain may not be firing as quickly as it should and it's just FRIED! So, sorry If I missed something or if that's a stupid question.)

The reason I have the loop going for 5 times is because, even though there are only 4 types of employees, the instructor wants to see input for 5. Here's the example input he provided us with:

"a) one Salaried Employee with a weekly salary of $1000 and a birthday of January 1, 1960,
b) one Salaried Employee with a weekly salary of $1000 and a birthday in this month (November 1, 1961)
c) one Commission Employee Worker with a gross weekly sales of $12,000 and a 5% commission rate and a birthday of February 1, 1962,
d) one Base Plus Commission Employee with a gross weekly sales of $10,000, a 3% commission, and a $500 base weekly salary with a birthday of March 1, 1963, and
e) one Hourly Employee with a wage of $20/hour working for 40 hours and a birthday of April 1, 1964"

That's why I thought I should have my loop continue prompting 5 times for info instead of 4 times but I may be looking at this wrong. Anyway, I'll take another look at your response after I've had some rest and maybe then I'll catch what you tried to show me. Sorry, I've known for a while that I need to get some rest but I just couldn't leave it alone. Anyway, thank you SOOO MUCH for your response. I think a fresh look at it later will help me GREATLY! Thanks again.

PS: I had two different questions about the same program, one thread I named "Help with Inheritance" and the other I named simply "Inheritance". At the moment, it seems that both my questions wound up in one thread when I didn't mean for that. So anyway, that could cause some confusion also because I included different parts for each question. Sorry 'bout that.

Edited 6 Years Ago by BobbieJean: n/a

It seems to me like your problem is in your main method in your first post... I haven't read through your other classes but the way you are getting info from the user is as follows:

for (count = 0; count < 5; count++)
{
    //get temporary info from user
}
//create objects from temporary info

You keep getting the info for creating an object and then you discard it, and you end up only creating objects based on the last info. Instead, try this:

public static void main( String args[] ) 
   {
                // create four-element Employee array
                Employee employees[] = new Employee[ 4 ];

	        String first = "";
		String last = "";
		String ssn = "";
		String salaryString;
		double salary = 0;
		String wageString;
		double wage = 0;
		String hoursString;
		int hours = 0;
		String salesString;
		double sales = 0;
		String rateString;
		double rate = 0;
		String empTypeString = "";
		int empType = 0;
		int count;
		
		for(count = 0; count < 5; count++)
		{
			empTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE); 
			empType = Integer.parseInt(empTypeString);
			
			switch(empType)
			{
			case 1:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			case 2:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
				wage = Double.parseDouble(wageString);
				hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
				hours = Integer.parseInt(hoursString);
				break;
				
			case 3:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				break;
				
			case 4:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			default:
				JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
				break;
			}
		
		        switch (count)
                        {
                              case 0: Employees[0] = new SalariedEmployee(first, last, ssn, salary);break;
                              case 1: Employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);break;
                              case 2: Employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);break;
                              case 3: Employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);break;
                             //I don't know whether you want a fifth case, since you run through the loop 5 times but only create 4 objects...
                        }		
		}

     //More syntax here...
     }

Hopefully this made some sense...

You changed your main method in the second post and I can't really follow it all without comments. But in your first post, what I said above seems to be the problem.

Hi,

Even thought I'm exhausted, I took one more look at your response to try to see what I missed and I think I see it. So you basically declared the array BEFORE getting user input? Hopefully I got your point this time. I'll try that real quick and see how it goes.

Just so you know, the switch statement you have might need 'break' statements after each 'case' statement is over. Otherwise it will execute the code in the other case statements as well.

It seems to me like your problem is in your main method in your first post... I haven't read through your other classes but the way you are getting info from the user is as follows:

for (count = 0; count < 5; count++)
{
    //get temporary info from user
}
//create objects from temporary info

You keep getting the info for creating an object and then you discard it, and you end up only creating objects based on the last info. Instead, try this:

public static void main( String args[] ) 
   {
                // create four-element Employee array
                Employee employees[] = new Employee[ 4 ];

	        String first = "";
		String last = "";
		String ssn = "";
		String salaryString;
		double salary = 0;
		String wageString;
		double wage = 0;
		String hoursString;
		int hours = 0;
		String salesString;
		double sales = 0;
		String rateString;
		double rate = 0;
		String empTypeString = "";
		int empType = 0;
		int count;
		
		for(count = 0; count < 5; count++)
		{
			empTypeString = JOptionPane.showInputDialog(null, "What type of employee are you?\n\nEnter the number that corresponds to your answer.\n1. Salaried\n2. Hourly\n3. Commissioned\n4. Base Plus Commissioned", "Employee Type", JOptionPane.QUESTION_MESSAGE); 
			empType = Integer.parseInt(empTypeString);
			
			switch(empType)
			{
			case 1:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salaryString = JOptionPane.showInputDialog(null, "What is your weekly salary?", "Salary", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			case 2:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				wageString = JOptionPane.showInputDialog(null, "What is your hourly wage?", "Hourly Wage", JOptionPane.QUESTION_MESSAGE);
				wage = Double.parseDouble(wageString);
				hoursString = JOptionPane.showInputDialog(null, "How many hours per week do you work?", "Hours Worked", JOptionPane.QUESTION_MESSAGE);
				hours = Integer.parseInt(hoursString);
				break;
				
			case 3:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				break;
				
			case 4:
				first = JOptionPane.showInputDialog(null, "What is your first name?", "First Name", JOptionPane.QUESTION_MESSAGE);
				last = JOptionPane.showInputDialog(null, "What is your last name?", "Last Name", JOptionPane.QUESTION_MESSAGE);
				ssn = JOptionPane.showInputDialog(null, "What is your social security number?", "SSN", JOptionPane.QUESTION_MESSAGE);
				salesString = JOptionPane.showInputDialog(null, "What are your weekly gross sales?", "Gross Sales", JOptionPane.QUESTION_MESSAGE);
				sales = Double.parseDouble(salesString);
				rateString = JOptionPane.showInputDialog(null, "What percent of commission do you receive?", "Commsission Rate", JOptionPane.QUESTION_MESSAGE);
				rate = Double.parseDouble(rateString);
				salaryString = JOptionPane.showInputDialog(null, "What is your base pay?", "Base Pay", JOptionPane.QUESTION_MESSAGE);
				salary = Double.parseDouble(salaryString);
				break;
				
			default:
				JOptionPane.showMessageDialog(null, "Invalid entry!", "Error", JOptionPane.ERROR_MESSAGE);
				break;
			}
		
		        switch (count)
                        {
                              case 0: Employees[0] = new SalariedEmployee(first, last, ssn, salary);break;
                              case 1: Employees[1] = new HourlyEmployee(first, last, ssn, wage, hours);break;
                              case 2: Employees[2] = new CommissionEmployee(first, last, ssn, sales, rate);break;
                              case 3: Employees[3] = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary);break;
                             //I don't know whether you want a fifth case, since you run through the loop 5 times but only create 4 objects...
                        }		
		}

     //More syntax here...
     }

Hopefully this made some sense...

You changed your main method in the second post and I can't really follow it all without comments. But in your first post, what I said above seems to be the problem.

Hi again! This helped a TON! Now it keeps the input for each employee but I think your right about maybe needing another case because the loop goes through five times. The instructor wants to see 5 employees, two of them are the same type of employee so I'm trying to figure out if I make another case for the user to choose from or just with the array. I'm not sure if that makes sense but anyway, I'm trying to think of how to display the information for 5 employees when there are only 4 employee types. Any suggestions?

Hi again! This helped a TON! Now it keeps the input for each employee but I think your right about maybe needing another case because the loop goes through five times. The instructor wants to see 5 employees, two of them are the same type of employee so I'm trying to figure out if I make another case for the user to choose from or just with the array. I'm not sure if that makes sense but anyway, I'm trying to think of how to display the information for 5 employees when there are only 4 employee types. Any suggestions?

I got it! Thank you SOOOOO MUCH for your help!

This question has already been answered. Start a new discussion instead.