Вроде как потестил немного - разное значение, для разных объектов. Не просто -1 1 0 ибо в условии еще нужно знать "насколько лучше". Валидатор говорит то что в шапке вопроса. Может кто заметит что и подкинет таких входных данных чтобы мой код поломать?
package com.javarush.task.task17.task1714;
/*
Comparable
*/
import java.util.Objects;
public class Beach implements Comparable<Beach> {
private String name; //название
private float distance; //расстояние
private int quality; //качество
public Beach(String name, float distance, int quality) {
this.name = name;
this.distance = distance;
this.quality = quality;
}
public synchronized String getName() {
return name;
}
public synchronized void setName(String name) {
this.name = name;
}
public synchronized float getDistance() {
return distance;
}
public synchronized void setDistance(float distance) {
this.distance = distance;
}
public synchronized int getQuality() {
return quality;
}
public synchronized void setQuality(int quality) {
this.quality = quality;
}
@Override
public synchronized int compareTo(Beach o) {
if(o == null){
throw new NullPointerException("There must be some object.");
}
if( this.equals(o) ){
return 0;
}
return this.quality - o.quality;
}
@Override
public synchronized int hashCode() {
int hash = 7;
hash = 31 * hash + (name == null ? 0 : name.hashCode());
hash = 31 * hash + Objects.hash(distance,quality);
return hash;
}
@Override
public synchronized boolean equals(Object obj) {
if(obj == this){
return true;
}
if(!(obj instanceof Beach)){
return false;
}
Beach other = (Beach) obj;
boolean nameEquals = (this.name == null &&
other.name == null) ||
(this.name != null && this.name.equals(other.name));
boolean distancesEquals = (this.distance == other.distance);
boolean qualityEquals = (this.quality == other.quality);
return nameEquals && distancesEquals && qualityEquals;
}
public static void main(String[] args) {
int result = new Beach("1",15,46).compareTo(new Beach("2",12,24));
}
}