| 1 | public class Weight implements Comparable<Weight> { |
| 2 | private int weight; |
| 3 | |
| 4 | Weight() { |
| 5 | } |
| 6 | |
| 7 | Weight(int weight) { |
| 8 | setWeight(weight); |
| 9 | } |
| 10 | |
| 11 | public int getWeight() { |
| 12 | return weight; |
| 13 | } |
| 14 | |
| 15 | public void setWeight(int weight) { |
| 16 | if (validateWeight(weight)) { |
| 17 | this.weight = weight; |
| 18 | } else { |
| 19 | throw new IllegalArgumentException("Weight cannot be negative or zero"); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | private boolean validateWeight(int weight) { |
| 24 | return (weight > 0); |
| 25 | } |
| 26 | |
| 27 | @Override |
| 28 | public int compareTo(Weight weight) { |
| 29 | int distance = weight.weight - this.getWeight(); |
| 30 | if (distance >= 0) |
| 31 | return distance; |
| 32 | else |
| 33 | return -distance; |
| 34 | } |
| 35 | } |