forked from AY1920S1-CS2103T-T09-4/main
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cost.java
60 lines (49 loc) · 1.5 KB
/
Cost.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
59
60
package seedu.address.model.phone;
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;
import java.text.NumberFormat;
import java.text.ParseException;
/**
* Represents a Phone's cost in the SML.
* Guarantees: immutable; is valid as declared in {@link #isValidCost(String)}
*/
public class Cost {
public static final String MESSAGE_CONSTRAINTS =
"Costs must be non-negative, start with \'$\' and have at most 2 decimals.";
public final String value;
/**
* Constructs a {@code Cost}.
*
* @param cost A valid cost.
*/
public Cost(String cost) {
requireNonNull(cost);
checkArgument(isValidCost(cost), MESSAGE_CONSTRAINTS);
value = cost;
}
/**
* Returns true if a given string is a valid cost.
*/
public static boolean isValidCost(String cost) {
try {
Number number = NumberFormat.getCurrencyInstance().parse(cost);
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 Cost // instanceof handles nulls
&& value.equals(((Cost) other).value)); // state check
}
@Override
public int hashCode() {
return value.hashCode();
}
}