Убедись, что для разных объектов возвращаются действительно разные значения compareTo. Что не так?
package com.javarush.task.task17.task1714;
/*
Comparable
*/
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;
}
public static void main(String[] args) {
// Beach beach1 = new Beach("Пляж1", 100, 200);
// Beach beach2 = new Beach("Пляж2", 150, 200);
// System.out.println(beach2.compareTo(beach1));
// System.out.println(beach1.compareTo(beach2));
}
////////////////////////////////////////////////////////////
@Override
public synchronized int compareTo(Beach beach) {
int result = 0;
float a, b;
a = this.distance + this.quality;
b = beach.distance + beach.quality;
if (a > b) {
result = (int) (a - b);
}
else if (a < b) {
result = (int) ((b - a) * (- 1));
}
return result;
}
}