forked from AY1920S1-CS2103T-T09-4/main
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Price.java
58 lines (47 loc) · 1.44 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
55
56
57
58
package seedu.address.model.phone;
import java.text.NumberFormat;
import java.text.ParseException;
import static seedu.address.commons.util.AppUtil.checkArgument;
/**
* Represents a Phone's price in the SML.
* Guarantees: immutable; is valid as declared in {@link #isValidPrice(String)}
*/
public class Price {
public static final String MESSAGE_CONSTRAINTS =
"Prices must be non-negative, start with \'$\' and have at most 2 decimals.";
public final String value;
/**
* Constructs a {@code Price}.
*
* @param price A valid price.
*/
public Price(String price) {
checkArgument(isValidPrice(price), MESSAGE_CONSTRAINTS);
value = price;
}
/**
* Returns true if a given string is a valid price.
*/
public static boolean isValidPrice(String price) {
try {
Number number = NumberFormat.getCurrencyInstance().parse(price);
return number != null;
} catch (ParseException e) {
return false;
}
}
@Override
public String toString() {
return value;
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Price // instanceof handles nulls
&& value.equals(((Price) other).value)); // state check
}
@Override
public int hashCode() {
return value.hashCode();
}
}