Skip to content

Instantly share code, notes, and snippets.

@YanchevskayaAnna
Created June 8, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YanchevskayaAnna/a998d4c6d6a9d7531713a1b41fb3e899 to your computer and use it in GitHub Desktop.
Save YanchevskayaAnna/a998d4c6d6a9d7531713a1b41fb3e899 to your computer and use it in GitHub Desktop.
MyData
package week1.day2.student;
/**
* Created by mykhailov on 22.05.2016.
*/
public class MyDate implements Comparable {
private int yearsBirth;
private int monthBirth;
private int dayBirth;
public MyDate(int yearsBirth, int monthBirth, int dayBirth) {
this.yearsBirth = yearsBirth;
this.monthBirth = monthBirth;
this.dayBirth = dayBirth;
}
public int getYearsBirth() {
return yearsBirth;
}
public int getMonthBirth() {
return monthBirth;
}
public int getDayBirth() {
return dayBirth;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
if (yearsBirth != myDate.yearsBirth) return false;
if (monthBirth != myDate.monthBirth) return false;
return dayBirth == myDate.dayBirth;
}
@Override
public int hashCode() {//Yanchevskaya A. Возвращать ноль не нужно, лучше удалить метод
return 0;
}
public String asString() { //Yanchevskaya A. Нужно переделать на toString()
return String.format("%d.%d.%d", dayBirth, monthBirth, yearsBirth);
}
@Override
public int compareTo(Object o) {
if (this == o) return 0;
if (o != null && o instanceof Student) { //Yanchevskaya A. Почему дату сравниваете со студентом?
} else {
MyDate myDate = (MyDate) o;
if (yearsBirth != myDate.yearsBirth) {
return Math.abs(yearsBirth - myDate.yearsBirth); //Yanchevskaya A. Зачем брать по модулю? Будет возвращать всегда положительное или ноль, а нам как раз и нужно отрицательное, в случае если o1 < o2
} else
if (monthBirth != myDate.monthBirth) {
return Math.abs(monthBirth - myDate.monthBirth);
} else
if (dayBirth != myDate.dayBirth)
return Math.abs(dayBirth - myDate.dayBirth);
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment