forked from CS2113-AY1819S1-F10-3/main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NRIC.java
58 lines (47 loc) · 1.72 KB
/
NRIC.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.addressbook.data.person;
import seedu.addressbook.data.exception.IllegalValueException;
import java.util.Arrays;
import java.util.List;
/**
* Represents a Person's identification number(NRIC or FIN) in the EX-SI-53.
* Guarantees: immutable; is valid as declared in {@link #isValidNRIC(String)}
*/
public class NRIC {
public static final String EXAMPLE = "s1234567a";
public static final String MESSAGE_NAME_CONSTRAINTS = "NRIC/FIN should start with 's'/'t'/'g'/'f' and end with a letter and " +
"must have 7 digits in between, no spaces";
public static final String NAME_VALIDATION_REGEX = "[sStTgGfF][0-9]{7}[a-zA-Z]";
public final String identificationNumber;
/**
* Validates given nric.
*
* @throws IllegalValueException if given name string is invalid.
*/
public NRIC(String nric) throws IllegalValueException {
nric = nric.trim();
if (!isValidNRIC(nric)) {
throw new IllegalValueException(MESSAGE_NAME_CONSTRAINTS);
}
this.identificationNumber = nric;
}
/**
* Returns true if a given string is a valid NRIC.
*/
public static boolean isValidNRIC(String test) {
return test.matches(NAME_VALIDATION_REGEX);
}
@Override
public String toString() {
return identificationNumber;
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof NRIC // instanceof handles nulls
&& this.identificationNumber.equals(((NRIC) other).identificationNumber)); // state check
}
@Override
public int hashCode() {
return identificationNumber.hashCode();
}
}