-
Notifications
You must be signed in to change notification settings - Fork 0
/
Weight.java
49 lines (42 loc) · 944 Bytes
/
Weight.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Weight implements Comparable<Weight> {
private int pounds;
private int ounces;
public Weight (int lbs, int oz) {
this.pounds = lbs;
this.ounces = oz;
}
public int compareTo(Weight other) {
if(other.getPounds() != pounds) {
return pounds - other.getPounds();
}
else {
return ounces - other.getOunces();
}
}
private void distribute() {
if(ounces >= 16) {
ounces -= 16;
pounds += 1;
}
}
public int getPounds(){
return pounds;
}
public int getOunces() {
return ounces;
}
public String toString () {
distribute();
String text = "";
if(pounds == 1) {
text += pounds + "1b ";
}
else if (pounds > 1) {
text += pounds + "lbs ";
}
if(ounces >= 1) {
text += ounces + "oz";
}
return text;
}
}