Fix .compareTo() calculation in Size and Weight classes.
[Persons_Comparator.git] / src / Weight.java
index 3f2a829163130f8bba5c5ed1e22e5fdd0fa916c3..5e33327fac7ec77eac23a14ec0aa46a6b4e65342 100644 (file)
@@ -1,6 +1,9 @@
-public class Weight {
+public class Weight implements Comparable<Weight> {
     private int weight;
 
+    Weight() {
+    }
+
     Weight(int weight) {
         setWeight(weight);
     }
@@ -12,10 +15,21 @@ public class Weight {
     public void setWeight(int weight) {
         if (validateWeight(weight)) {
             this.weight = weight;
-        } /* FIXME: raise an error */
+        } else {
+            throw new IllegalArgumentException("Weight cannot be negative or zero");
+        }
     }
 
     private boolean validateWeight(int weight) {
         return (weight > 0);
     }
+
+    @Override
+    public int compareTo(Weight weight) {
+        int distance = weight.getWeight() - this.getWeight();
+        if (distance >= 0)
+            return distance;
+        else
+            return -distance;
+    }
 }