So this is what i am trying to do. Create just a 5 element array and using the scanner utility get some information from the user that can then be passed down to an object that is dynamically created by the array. The object requires paramaters due to constructors.

Employee[] emp = new Employee[5];

Scanner sc = new Scanner(System.in)

So yeah i am completely stuck and not to sure which way to go with this. Help would be awesome while i continue to search the web for answers.

Uhm, an array cannot create objects. After you "get the data from the user" use an if statement to check if the element of the array to which you want to assign the data is null, and if so, create an instance of the "object" assign it to that element. After the check and possible creation, then assign the data.

Yes, what you are doing there is creating an array where you can place 5 employee objects.

To fill your array you will need to loop through it, adding a new Employee() and asking for input with relevant paramaters in its constructor each time.

so there is an abstract employee superclass with several subclasses that require parameters. Each subclass uses the super(parameters here) as well as its own arguments that must be passed to the constructor. Yes i am sure this very sloppy. Compiles though. Trying to get the final salary multiplied by 4 though to represent a monthly salary, hence the earned variable. When i try to manipulate the printf statement regarding the amount earned it doesn't seem to work. Says the variable hasn't been initialized. You are suggesting something like this then?

// PayrollSystemTest.java
// Employee hierarchy test program.
import java.util.Scanner;

public class PayrollSystemTest 
{
   public static void main( String args[] ) 
   {
  		
		String fN, lN, SN, emp;
		String Salary = "Salary";
		String Comm = "Comm";
		String Base = "Base";
		String Hourly = "Hourly";
		int mo, da, ye, x;
		double sal, hourwage, hourwork, r, earned, sales;
		Employee[] employees = new Employee[5];
		
		for(x = 0; x < employees.length; ++x)
		{
			Scanner input = new Scanner(System.in);
			System.out.print("Please enter your worker type: ");
			emp = input.nextLine();
			if(emp.equals(Salary))
			{
				System.out.print("Enter your first name: ");
				fN = input.nextLine();
				System.out.print("Enter your last name: ");
				lN = input.nextLine();
				System.out.print("Enter your SSN: ");
				SN = input.nextLine();
				System.out.print("Enter your birth month in numerical format: ");
				mo = input.nextInt();
				System.out.print("Enter the date of the month you were born: ");
				da = input.nextInt();
				System.out.print("Enter the year you were born: ");
				ye = input.nextInt();
				System.out.print("Enter your weekly salary: ");
				sal = input.nextDouble();
				earned = sal * 4;
				employees[x] = new SalariedEmployee(fN, lN, SN, mo, da, ye, sal);
			}
			else
				if(emp.equals(Comm))
				{
					System.out.print("Enter your first name: ");
					fN = input.nextLine();
					System.out.print("Enter your last name: ");
					lN = input.nextLine();
					System.out.print("Enter your SSN: ");
					SN = input.nextLine();
					System.out.print("Enter your birth month in numerical format: ");
					mo = input.nextInt();
					System.out.print("Enter the date of the month you were born: ");
					da = input.nextInt();
					System.out.print("Enter the year you were born: ");
					ye = input.nextInt();
					System.out.print("Enter your weekly sales: ");
					sales = input.nextDouble();
					System.out.print("Enter your commission rate: ");
					r = input.nextDouble();
					earned = sales * (r/100) + sales;
					employees[x] = new CommissionEmployee(fN, lN, SN, mo, da, ye, sales, r);
				}
				else
					if(emp.equals(Base))
					{
						System.out.print("Enter your first name: ");
						fN = input.nextLine();
						System.out.print("Enter your last name: ");
						lN = input.nextLine();
						System.out.print("Enter your SSN: ");
						SN = input.nextLine();
						System.out.print("Enter your birth month in numerical format: ");
						mo = input.nextInt();
						System.out.print("Enter the date of the month you were born: ");
						da = input.nextInt();
						System.out.print("Enter the year you were born: ");
						ye = input.nextInt();
						System.out.print("Enter your weekly sales: ");
						sales = input.nextDouble();
						System.out.print("Enter your commission rate: ");
						r = input.nextDouble();
						System.out.print("Enter your weekly salary: ");
						sal = input.nextDouble();
						earned = (r/100) * sales + sal + sales;
						employees[x] = new BasePlusCommissionEmployee(fN, lN, SN, mo, da, ye, sales, r, sal);
					}
					else
						if(emp.equals(Hourly))
						{
							System.out.print("Enter your first name: ");
							fN = input.nextLine();
							System.out.print("Enter your last name: ");
							lN = input.nextLine();
							System.out.print("Enter your SSN: ");
							SN = input.nextLine();
							System.out.print("Enter your birth month in numerical format: ");
							mo = input.nextInt();
							System.out.print("Enter the date of the month you were born: ");
							da = input.nextInt();
							System.out.print("Enter the year you were born: ");
							ye = input.nextInt();
							System.out.print("Enter your hourly wage: ");
							hourwage = input.nextDouble();
							System.out.print("Enter your hours worked: ");
							hourwork = input.nextDouble();
							earned = hourwage * hourwork;
							employees[x] = new HourlyEmployee(fN, lN, SN, mo, da, ye, hourwage, hourwork);
						}
						else
							System.out.print("Your entry was invalid.");
			}
			


				

			
			
			
									
				

      System.out.println( "Employees processed individually:\n" );
      
     


      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" + earned, 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

Yes, that is what I meant.

employees[ j ].getClass().getName() );

is this where your problem lies?

Edited 5 Years Ago by Akill10: n/a

Actually everything works and outputs correctly now minus adding a 100 dollar bonus if the month of the birthday is in November.

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() );
				employee.setBaseSalary( 4 * oldBaseSalary );
         } // end if
			
			if ( currentEmployee instanceof HourlyEmployee )
			{
				HourlyEmployee employee = ( HourlyEmployee ) currentEmployee;
				double oldHourlyWage = employee.getWage();
				double oldHours = employee.getHours();
				employee.setHours ( 4 * oldHours );
				employee.setWage( 4 * oldHours );
				Date bDay = employee.getBirthDate();
				if(bDay(mo) == 11)
				{
					employee.setWage( 100 + oldHourlyWage );
				}	
			}
			
			if ( currentEmployee instanceof SalariedEmployee )
			{
				SalariedEmployee employee = ( SalariedEmployee ) currentEmployee;
				double oldSalary = employee.getWeeklySalary();
				employee.setWeeklySalary( 4 * oldSalary);
			}
			
		
				

        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

getting my error right here though when trying to code in a statement to look for november month birthdays.

if ( currentEmployee instanceof HourlyEmployee )
			{
				HourlyEmployee employee = ( HourlyEmployee ) currentEmployee;
				double oldHourlyWage = employee.getWage();
				double oldHours = employee.getHours();
				employee.setHours ( 4 * oldHours );
				employee.setWage( 4 * oldHours );
				Date bDay = employee.getBirthDate();
				if(bDay(mo) == 11) // cannot find symbol....
				{
					employee.setWage( 100 + oldHourlyWage );
				}	
			}

employee class as pertains to the birthday.

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

   // three-argument constructor
   public Employee( String first, String last, String ssn, int month, int day, int year )
   {
      firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
		birthDate = new Date(month, day, year);
		
   } // end three-argument Employee constructor

 	// set birth date
	public void setBirthDate( int month, int day, int year)
	{
		
		birthDate = new Date(month, day, year);
		
	}// end birth date method
		  
	// get birth date method
	public Date getBirthDate()
	{
		return birthDate;
	}// end method get birth date

date class.....

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
	
	// method for returning birthdate
	public String toDateString()
	{
		 String info = "Date of Birth: " + month + "/" + day + "/" + year;
 		 return info;  
	}
	
   // 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

not really to sure where i am going wrong here.

what is the mo of bDay(mo)? shouldnt you use bDay.checkMonth()?

edit: nevermind, didnt look at the code in that method.

Edited 5 Years Ago by Akill10: n/a

that is the variable that was used in main to pass the month to the constructor. Yeah i realize that isn't working. Can't seem to wrap my head around this. Basically
i want to find a way to take the month that was entered by the user and compare to see if it was 11 for November, if so add 100 dollars to their earnings. Maybe i just need a break, sigh.

Well i can say this much, definitely learning a lot about Java throughout this project.

Possible Soln: Make a different method in Employee to return the month that is passed through the constructor.

private int imonth
 public Employee( String first, String last, String ssn, int month, int day, int year )
   {
 firstName = first;
      lastName = last;
      socialSecurityNumber = ssn;
		birthDate = new Date(month, day, year);

  this.imonth=month;
}

public int getMonth()
{
return imonth;
}

Then test, if employee.getMonth()==11.

Obviously not the most elegant solution, it's been a long day.
Just an idea!

Edited 5 Years Ago by Akill10: n/a

Wow thanks so much man. Put me right on track. Now i just have to nail down the math part to add it correctly. Thank you so much again.

I also don't know if it will even work to be perfectly honest
edit: No problem, I knew it would work I was only joking!

Edited 5 Years Ago by Akill10: n/a

Nailed it. Went with.....

if ( currentEmployee instanceof SalariedEmployee )
			{
				SalariedEmployee employee = ( SalariedEmployee ) currentEmployee;
				double oldSalary = employee.getWeeklySalary();
				employee.setWeeklySalary( 4 * oldSalary);
				int reward = employee.getMonth();
				if(bonus == reward)
				{
					double salary = employee.getWeeklySalary();
					employee.setWeeklySalary(100 + salary);
				}
			}

overspent my time on this, a solid 13 hours. Excellent learning though and thanks for all the positive input Akill10 and masijade.

This article has been dead for over six months. Start a new discussion instead.