0

I am having trouble with a linked list I'm working on. I've created my own linked list of Employee objects. Each Employee object contains all your normal data like name, address, contact info, salary, years of employment etc.

What I am trying to do create a method in my list class which would modify all the objects in the list. For instance, I could increment the "years of employment" by 1 or I could increase the "salary" of all Employees in the list by 5% to give everyone a raise.

2
  • 1
    Can you post the implementation of the ListNode class? Commented Feb 3, 2014 at 21:43
  • I added the ListNode Class Commented Feb 3, 2014 at 21:57

3 Answers 3

1

so you have to call the change method on each object inside the List

LinkedList<Employee> employees = new ArrayList<Employee>();

public void increaseYearOfEmployment() {
    for (Employee e : employees) 
            e.yrsEmployed++;   //  instance variable
}

public void increaseSalary() {
    for (Employee e : employees) 
            e.yrsSalary++;       //  instance variable
}
Sign up to request clarification or add additional context in comments.

Comments

0

Ideally you would have a get method for the ListNode class in the form of listNode.getEmployee() and then you can just treat that object as an Employee object. So for example:

ListNode temp = first;
while (temp != null){
    temp.getEmployee().incrementYears(1);
    temp = temp.getNext();
}

But its hard to tell if we don'the ListNode and Employee classes implementations.

Comments

0

If the Employee fields were not set to private:

LinkedList<Employee> employees; //employee instance variable

...

public void increaseYrsEmployment() {
    for (Employee e : employees) e.yrsEmployed++;
}

If the Employee fields are set to private, you can add getter and setter methods to the class Employee to enable the fields to be accessed and modified.

1 Comment

Well I want to create the list and run the method in my main while keeping the definition in my list class. I updated original post to show this.

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.