lundi 26 octobre 2020

comparison logic in this if statement

I was wondering if you can explain to me this comparison logic:

public class Workplace {
    private int index = 0;
    private String name;
    private Employee[] employees;

    public Workplace(String name, int numberOfEmployees){
        this.name = name;
        this.employees = new Employee[numberOfEmployees];
    }

    public Employee[] returnEmployees(Employee e){
        Employee youngestEmployee = null;
        Employee oldestEmployee = null;

        for(int i=0; i<index; i++){
            if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
                youngestEmployee = employees[i];
            }
            else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
                oldestEmployee = employees[i];
            }
        }
        return new Employee[] {youngestEmployee, oldestEmployee};
    }
}

In particular, the if statement comparison logic over here:

if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
    youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
    oldestEmployee = employees[i];
}

I've got another class called Employee, in which I have declared the get and set methods. And have the age variable declared as an int. So I know that this if statement comparison logic wants to get the youngest and oldest employee but I am getting confused at this part:

employees[i].getAge() > youngestEmployee.getAge()

Instead of saying less than, here it is greater than. I couldn't find an answer yet. Thank you for your time!

Aucun commentaire:

Enregistrer un commentaire