X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FWeight.java;h=47547ba4f524711c42a3bc1c3e561fec0be48853;hb=b974e749af3a4b2df3737b2d361233c704dcc4d8;hp=3f2a829163130f8bba5c5ed1e22e5fdd0fa916c3;hpb=9749119587561f5848349b7a1b9e7d9342944f0d;p=Persons_Comparator.git diff --git a/src/Weight.java b/src/Weight.java index 3f2a829..47547ba 100644 --- a/src/Weight.java +++ b/src/Weight.java @@ -1,6 +1,9 @@ -public class Weight { +public class Weight implements Comparable { 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.weight - this.getWeight(); + if (distance >= 0) + return distance; + else + return -distance; + } }