2

I'm working on a Java program where I can keep track of my employee payroll. There are two types of employees, hourly and salary.

I have a while loop asking for the input and creating the objects so that I can add as many or as few employees as I want.

When I create my object from my class, I simply use:

HourlyEmployee employee = new HourlyEmployee(type, name, hours, rate);

However, if this is in a while loop, will I be creating several instances of the class type HourlyEmployee with the same name "employee"? Does it even matter if they have the same name (I just want to display the information for each employee on the screen later).

If so, how do I write my code so that the name of each HourlyEmployee object is dynamic as well?

Thanks!

Let me know if you guys want the rest of the code.

3 Answers 3

5

The way to do it is to put all Employee objects into a collection.

A good starting point is this tutorial.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the guide. Collection will def work better than array in my case.
2

Yes, you will be create several HourlyEmployee objects with the name "employee". As this question sounds a little homework-y, I won't give an example, but I will recommend that you investigate making an array of HourlyEmployee objects.

4 Comments

In the future, when you ask homework questions on SO, you should use the "homework" tag. If you don't, some people will view it as dishonest and downvote your question.
My apologies. Will add it now and on future posts. Ty.
It seems like when you create an array of HourlyEmployee, the number of employees cannot be dynamic because you have to declare the size.
True. I should have said "ArrayList" or "Collection" instead of "Array".
0

Try this,

  1. Create an abstract class called Employee,

  2. Then 2 concrete classes HourlyEmployee and SalaryEmployee.

  3. Use ArrayList to store the employees, having same names wont create a clash, but Map would have been a better option where u can have Ids as unique keys to identify employees even though they have same names.

Eg:

public void addEmp(Employee employee){

ArrayList<? extends Employee> emp;

 while(true){   // Use a boolean variable to terminate the loop at certain conditions

    for (Employee e : emp) {

        e.add(employee);


     }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.