-
Notifications
You must be signed in to change notification settings - Fork 0
/
Price.java
54 lines (48 loc) · 1.04 KB
/
Price.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
49
50
51
52
53
54
package hw5;
public class Price implements Comparable<Price>{
private int dollars;
private int cents;
public Price(int dollars, int cents) {
if (dollars < 0 || cents < 0 || cents > 99)
throw new IllegalArgumentException();
this.dollars = dollars;
this.cents = cents;
}
public String toString() {
String answer = "$" + dollars + ".";
if (cents < 10)
answer = answer + "0" + cents;
else
answer = answer + cents;
return answer;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Price other = (Price) obj;
if (cents != other.cents)
return false;
if (dollars != other.dollars)
return false;
return true;
}
@Override
public int compareTo(Price o) {
int thi = (this.dollars*100) + this.cents;
int oth = (o.dollars*100) + o.cents;
if(thi > oth) {
return 1;
}
else if(oth > thi) {
return -1;
}
else {
return 0;
}
}
}