Home > Net >  how to count employees
how to count employees

Time:12-31

If anyone can help it would be very appreciated.

CodePudding user response:

I can't see any code which initialize or increment int count variable. But as you said, you don't need count variable and just use size() method in employees List

@Override
public int getEmployeeCount() {
    return this.employees.size();
}

CodePudding user response:

In @BeforeEach you're test by creating 4 employees.

  • Your hire method does 'employees.add(p);' , so it expands your list.
  • Your fire method does not do anything, just returning false.

Yet you expect in test testFire and testGetEmployeeCount that the number of employees has decreased. That does not happen and will fail.

You need the following fix: IMPORTANT - Implement an equals and hash code on your PersonImpl class (so you can compare equal objects content instead of object-hash value). You can use guava or apache commmons or lombok or any other way to do that.

Then implement in 'fire' method:

    @Override
    public boolean fire(Person p) {
        return employees.remove(p);
    }

In this case I assume you will implement limitations in the 'hire' method on your class to have duplicate employees, so you need only to remove it once. If employees can be duplicate, then do to remove the employee including duplicates:

return employees.removeAll(Collections.singletonList(p));
  • Related