Requirement 2 failed.
The validator gave me the recommandation:
Achte darauf, dass compareTo den Wert 0 zurückgibt, wenn ein Objekt mit sich selbst verglichen wird.
I tested it with:
if (this.equals(o))
wertGesamt = 0;
and
if (this == o)
wertGesamt = 0;
without achievment.
Have you any idea?package de.codegym.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 b1 = new Beach("Palm Beach", 122.5f, 9);
// Beach b2 = new Beach("Snape Beach", 92.9f, 8);
//
// System.out.println("Vergleich = " + b1.compareTo(b2));
// System.out.println("Vergleich = " + b1.compareTo(b1));
// System.out.println("Vergleich = " + b2.compareTo(b2));
}
@Override
public synchronized int compareTo(Beach o) {
int wertDistanz = 0, wertQuality, wertGesamt;
if ((this.getDistance() - o.getDistance()) > 10)
wertDistanz = -2;
else if ((this.getDistance() - o.getDistance()) > 0 &&
(this.getDistance() - o.getDistance()) < 10)
wertDistanz = -1;
else if ((this.getDistance() - o.getDistance()) <= 0)
wertDistanz = 1;
if ((this.getQuality() - o.getQuality()) < 0)
wertQuality = -1;
else if ((this.getQuality() - o.getQuality()) > 0)
wertQuality = 1;
else
wertQuality = 0;
wertGesamt = wertDistanz + wertQuality;
if (this.equals(o))
wertGesamt = 0;
return wertGesamt;
}
}