Commit | Line | Data |
---|---|---|
1c118933 | 1 | public class Weight implements Comparable<Weight> { |
97491195 JB |
2 | private int weight; |
3 | ||
b974e749 JB |
4 | Weight() { |
5 | } | |
6 | ||
97491195 JB |
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; | |
bcdec887 JB |
18 | } else { |
19 | throw new IllegalArgumentException("Weight cannot be negative or zero"); | |
20 | } | |
97491195 JB |
21 | } |
22 | ||
23 | private boolean validateWeight(int weight) { | |
24 | return (weight > 0); | |
25 | } | |
1c118933 JB |
26 | |
27 | @Override | |
28 | public int compareTo(Weight weight) { | |
62e26730 | 29 | int distance = weight.getWeight() - this.getWeight(); |
613054ac JB |
30 | if (distance >= 0) |
31 | return distance; | |
32 | else | |
33 | return -distance; | |
1c118933 | 34 | } |
97491195 | 35 | } |