Hi I'm new to programming so I'm having a problem where an if statement isn't working. In the Person class boolean olderThan ignores my if statement so it always returns false and I just don't get why that's happening.
import java.util.Calendar; public class Person { private String name; private MyDate birthday; public Person(String name, int pp, int kk, int vv) { this.name = name; this.birthday = new MyDate(pp, kk, vv); } public int age() { MyDate today = new MyDate ( Calendar.getInstance().get(Calendar.DATE) , Calendar.getInstance().get(Calendar.MONTH) + 1 , Calendar.getInstance().get(Calendar.YEAR)); return today.differenceInYears( this.birthday ); } public boolean olderThan(Person compared) { if ( this.age() > compared.age()){ return true; } return false; } public String getName() { return this.name; } public String toString() { return this.name + ", born " + this.birthday; } }
public class MyDate { private int day; private int month; private int year; public MyDate(int pv, int kk, int vv) { this.day = pv; this.month = kk; this.year = vv; } public String toString() { return this.day + "." + this.month + "." + this.year; } public boolean earlier(MyDate compared) { if (this.year < compared.year) { return true; } if (this.year == compared.year && this.month < compared.month) { return true; } if (this.year == compared.year && this.month == compared.month && this.day < compared.day) { return true; } return false; } public int differenceInYears (MyDate comparedDate){ return Math.abs((((this.year*365)+(this.month*30)+this.day)-((comparedDate.year *365)+(comparedDate.month*30)+comparedDate.day))/365); } }