473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Polymorphism and the payroll program

143 100+
I have a program that uses Employee as the superclass. It has subclasses of SalariedEmployee, CommissionEmployee, BasePlusCommissionEmployee, and HoulyEmployee. This program works great to provide such data as first name, last name, SSN, and the salary information required. I have modified the program to include date of birth by adding a Date class. The birthdate is added to the output by the Employee superclass toString() method.

My problem: I need to modify the program to give each employee a $100 bonus during the month of their birthday. I have tried doing this in both the Employee class and the main method of the Test class.

My Questions: Which class would be most appropriate for this? (I think it needs to be in the Employee class?) Currently I am trying to do this in the Test class in conjuction with an array I have set up to polymorphically (which I don't fully understand yet) process the data. I get an error message that reads "currentMonth variable cannot be referenced from a static context"

Here's the main() method of the Test class and the toString() method of the Employee class. Can someone tell me what I'm doing wrong?

[HTML]public class PayrollSystemTest
{
final private int currentMonth = 3;

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

// 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
//the problem is with this if statement. If it is commented out and only the body
//of the else statement is left, the program works, but without the bonus feature.
if (currentMonth == currentEmployee.getMon())
{
System.out.println("Birthday bonus this month: $100.00");
System.out.println("New amount + bonus earned: " + currentEmployee.earnings() + 100);
}
else
{

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[/HTML]

Employee class toString() method:
[HTML] public String toString()
{
return String.format( "%s %s\nsocial security number: %s\nbirthday: %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getDate() );
} // end method toString[/HTML]
Mar 22 '07 #1
8 6383
r035198x
13,262 8TB
I have a program that uses Employee as the superclass. It has subclasses of SalariedEmployee, CommissionEmployee, BasePlusCommissionEmployee, and HoulyEmployee. This program works great to provide such data as first name, last name, SSN, and the salary information required. I have modified the program to include date of birth by adding a Date class. The birthdate is added to the output by the Employee superclass toString() method.

My problem: I need to modify the program to give each employee a $100 bonus during the month of their birthday. I have tried doing this in both the Employee class and the main method of the Test class.

My Questions: Which class would be most appropriate for this? (I think it needs to be in the Employee class?) Currently I am trying to do this in the Test class in conjuction with an array I have set up to polymorphically (which I don't fully understand yet) process the data. I get an error message that reads "currentMonth variable cannot be referenced from a static context"

Here's the main() method of the Test class and the toString() method of the Employee class. Can someone tell me what I'm doing wrong?

[HTML]public class PayrollSystemTest
{
final private int currentMonth = 3;

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

// 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
//the problem is with this if statement. If it is commented out and only the body
//of the else statement is left, the program works, but without the bonus feature.
if (currentMonth == currentEmployee.getMon())
{
System.out.println("Birthday bonus this month: $100.00");
System.out.println("New amount + bonus earned: " + currentEmployee.earnings() + 100);
}
else
{

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[/HTML]

Employee class toString() method:
[HTML] public String toString()
{
return String.format( "%s %s\nsocial security number: %s\nbirthday: %s",
getFirstName(), getLastName(), getSocialSecurityNumber(), getDate() );
} // end method toString[/HTML]
Add the bonus logic to the method that calculates the salary every month.
Mar 23 '07 #2
teddarr
143 100+
That's part of the problem. Each different category has the earnings calculated in the respective subclass. The bonus is to be added in either the super class (Employee) or the test class (PayrollTest) and using the Date class.
Mar 23 '07 #3
r035198x
13,262 8TB
That's part of the problem. Each different category has the earnings calculated in the respective subclass. The bonus is to be added in either the super class (Employee) or the test class (PayrollTest) and using the Date class.
Add it to the Superclass then.
Mar 24 '07 #4
teddarr
143 100+
This is what I am trying to do. When I do I get the error or one similar to the one I listed in my first post. I don't know how to call a specific instance of the date class to pull only the month from it. I have tried using 'this', I have tried several different things and nothing seems to work for me. Could you please be more specific. I know I need to do add the logic to the Employee class.....but what?

I have the body of an if statement written, I need the if statement itself. I apparently don't have the knowledge I need to write it right now....but that is not from lack of trying.
Mar 24 '07 #5
r035198x
13,262 8TB
This is what I am trying to do. When I do I get the error or one similar to the one I listed in my first post. I don't know how to call a specific instance of the date class to pull only the month from it. I have tried using 'this', I have tried several different things and nothing seems to work for me. Could you please be more specific. I know I need to do add the logic to the Employee class.....but what?

I have the body of an if statement written, I need the if statement itself. I apparently don't have the knowledge I need to write it right now....but that is not from lack of trying.
In the calculate salary method, get the Employee's DOB, wrap it in a regorianCalendar, and get the month o birth using the get method of the gc class, then create another gc with today's date and extract the month in a similar manner. The rest is now straightforward.
Mar 26 '07 #6
teddarr
143 100+
I have the program to the point where the Date class gives a number corresponding to the month. We are allowed to use a declared constant (for simplicity sake) for the current month. So.....

I am to the point where I need an if statement that compares the constant int currentMonth with the getMonth method of each instance of the different subclasses.

This is where my error comes in. I really think I am having trouble getting you to understand the problem. It would be a lot easier if I could post all of the code. BUT ist of all, it's against the rules, and 2nd, it would be really burdensome to post all of the different classes on the site.

The calculate salary method is in a sunclass that we are not supposed to modify for this calculation as we are supposed to make the changes once in either the Employee class or the test class. Not 4 times (once each in the subclasses). That defeats the purpose of the exercise.

Can you think of any key information I could be leaving out? Please let me know so I can post it. As it is, so far I have already considered all of your suggestions and they do not fit the problem.
Mar 26 '07 #7
r035198x
13,262 8TB
I have the program to the point where the Date class gives a number corresponding to the month. We are allowed to use a declared constant (for simplicity sake) for the current month. So.....

I am to the point where I need an if statement that compares the constant int currentMonth with the getMonth method of each instance of the different subclasses.

This is where my error comes in. I really think I am having trouble getting you to understand the problem. It would be a lot easier if I could post all of the code. BUT ist of all, it's against the rules, and 2nd, it would be really burdensome to post all of the different classes on the site.

The calculate salary method is in a sunclass that we are not supposed to modify for this calculation as we are supposed to make the changes once in either the Employee class or the test class. Not 4 times (once each in the subclasses). That defeats the purpose of the exercise.

Can you think of any key information I could be leaving out? Please let me know so I can post it. As it is, so far I have already considered all of your suggestions and they do not fit the problem.
Have a think about this and see if wont work for you


Inside the Employee class (Base class), you put a method called calculateBirthDayBonus

//The parameter month is the month for which the payroll is running
Expand|Select|Wrap|Line Numbers
  1.  public double calculateBirthDayBonus (int month) { 
  2.      int birthMonth = ///  
  3.      if(month == birthMonth) {
  4.          return theBonus;
  5.       }
  6.       else {
  7.          return 0.0;
  8.       }
  9. }
  10.  
  11.  
In each of the subclasses all you need to do is to adjust the calculate salary method like this
Expand|Select|Wrap|Line Numbers
  1.  public doulble calculateSalary() { 
  2.      double bonus = calculateBirthDayBonus(thisMonth);
  3.      //continue to get normal salary and then add the bonus at the end
  4. }
  5.  
Mar 26 '07 #8
teddarr
143 100+
I'll give that a try. That's one approach I haven't tried yet. I think it may work.

It always gets me how simple some solutions are and why our brains block out the easy answers.

Thanks.
Mar 26 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Randi | last post by:
Hi, Looking for some help with this payrool project I have for class. This is what the instructor asks for so far. I have it working without errors but am getting some funky numbers. I am not...
0
by: zexx | last post by:
Need some advise.... Payroll program Access97. Payroll is printed then posted to a hx table thro' a query with the payroll calculations that freeze that payroll's numbers for that period. The...
1
by: Akinyemi | last post by:
I am developing a payroll program.. The data generated will be saved in an Access Database. I want a situation whereby when an employee leaves the organization or retires, instead of deleting his...
10
by: ycg0771 | last post by:
I'm trying to modify the following program so that it uses a class to store and retrieve the employee's name, the hourly rate, and the number of hours worked. Use a constructor to initialize the...
8
by: John Sitka | last post by:
Hi, There are four pay types standard shiftpremium doubletime doubletimepremium each hour a person books can be one of these types
5
by: jbailey006 | last post by:
I am trying to add the following information to a payroll program I am working on for assignment. Modify the Payroll Program so that it uses a class to store and retrieve the employee's name, the...
1
by: Akinyemi | last post by:
I have almost finished writing my Payroll Program. But I am wondering how the program can be used for different months. For example, after, say January 2007 Payroll, the user would want to prepare...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.