diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 131d71b46..ca3d24679 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -6,14 +6,13 @@ * Represents a Person's address in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)} */ -public class Address { +public class Address extends Contact{ public static final String EXAMPLE = "123, some street"; public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses can be in any format"; public static final String ADDRESS_VALIDATION_REGEX = ".+"; public final String value; - private boolean isPrivate; /** * Validates given address. @@ -21,24 +20,8 @@ public class Address { * @throws IllegalValueException if given address string is invalid. */ public Address(String address, boolean isPrivate) throws IllegalValueException { - String trimmedAddress = address.trim(); - this.isPrivate = isPrivate; - if (!isValidAddress(trimmedAddress)) { - throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); - } - this.value = trimmedAddress; - } - - /** - * Returns true if a given string is a valid person address. - */ - public static boolean isValidAddress(String test) { - return test.matches(ADDRESS_VALIDATION_REGEX); - } - - @Override - public String toString() { - return value; + super(address, isPrivate, MESSAGE_ADDRESS_CONSTRAINTS, ADDRESS_VALIDATION_REGEX); + this.value = getValue(); } @Override @@ -47,13 +30,4 @@ public boolean equals(Object other) { || (other instanceof Address // instanceof handles nulls && this.value.equals(((Address) other).value)); // state check } - - @Override - public int hashCode() { - return value.hashCode(); - } - - public boolean isPrivate() { - return isPrivate; - } } diff --git a/src/seedu/addressbook/data/person/Contact.java b/src/seedu/addressbook/data/person/Contact.java new file mode 100644 index 000000000..482f3c7b3 --- /dev/null +++ b/src/seedu/addressbook/data/person/Contact.java @@ -0,0 +1,56 @@ +package seedu.addressbook.data.person; + +import seedu.addressbook.data.exception.IllegalValueException; + +/** + * Represents a Person's contact in the address book. + * Guarantees: immutable; is valid as declared in {@link #isValidContact(String)} + */ +public class Contact { + private boolean isPrivate; + private String value; + public Contact(){ + } + + /** + * Validates given contact data like phone, address and email address. + * + * @throws IllegalValueException if given phone/address/email address string is invalid. + */ + public Contact(String contactData, boolean isPrivate, String MESSAGE_CONSTRAINTS, String VALIDATION_REGEX) throws IllegalValueException { + this.isPrivate = isPrivate; + + String trimmedContactData = contactData.trim(); + + if (!isValidContact(trimmedContactData, VALIDATION_REGEX)) { + throw new IllegalValueException(MESSAGE_CONSTRAINTS); + } + + this.value = trimmedContactData; + } + + public boolean isPrivate() { + return isPrivate; + } + + public String getValue() { + return value; + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public String toString() { + return value; + } + + /** + * Returns true if the given string is a valid person phone number or email address or address. + */ + public static boolean isValidContact(String test, String VALIDATION_REGEX) { + return test.matches(VALIDATION_REGEX); + } +} diff --git a/src/seedu/addressbook/data/person/Email.java b/src/seedu/addressbook/data/person/Email.java index ad623b5b8..f30ddcbd9 100644 --- a/src/seedu/addressbook/data/person/Email.java +++ b/src/seedu/addressbook/data/person/Email.java @@ -6,7 +6,7 @@ * Represents a Person's email in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidEmail(String)} */ -public class Email { +public class Email extends Contact{ public static final String EXAMPLE = "valid@e.mail"; public static final String MESSAGE_EMAIL_CONSTRAINTS = @@ -14,7 +14,6 @@ public class Email { public static final String EMAIL_VALIDATION_REGEX = "[\\w\\.]+@[\\w\\.]+"; public final String value; - private boolean isPrivate; /** * Validates given email. @@ -22,24 +21,8 @@ public class Email { * @throws IllegalValueException if given email address string is invalid. */ public Email(String email, boolean isPrivate) throws IllegalValueException { - this.isPrivate = isPrivate; - String trimmedEmail = email.trim(); - if (!isValidEmail(trimmedEmail)) { - throw new IllegalValueException(MESSAGE_EMAIL_CONSTRAINTS); - } - this.value = trimmedEmail; - } - - /** - * Returns true if the given string is a valid person email. - */ - public static boolean isValidEmail(String test) { - return test.matches(EMAIL_VALIDATION_REGEX); - } - - @Override - public String toString() { - return value; + super(email, isPrivate, MESSAGE_EMAIL_CONSTRAINTS, EMAIL_VALIDATION_REGEX); + this.value = getValue(); } @Override @@ -48,14 +31,4 @@ public boolean equals(Object other) { || (other instanceof Email // instanceof handles nulls && this.value.equals(((Email) other).value)); // state check } - - @Override - public int hashCode() { - return value.hashCode(); - } - - - public boolean isPrivate() { - return isPrivate; - } } diff --git a/src/seedu/addressbook/data/person/Phone.java b/src/seedu/addressbook/data/person/Phone.java index 431a7a882..6e6ea014c 100644 --- a/src/seedu/addressbook/data/person/Phone.java +++ b/src/seedu/addressbook/data/person/Phone.java @@ -6,14 +6,13 @@ * Represents a Person's phone number in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidPhone(String)} */ -public class Phone { +public class Phone extends Contact{ public static final String EXAMPLE = "123456789"; public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers"; public static final String PHONE_VALIDATION_REGEX = "\\d+"; public final String value; - private boolean isPrivate; /** * Validates given phone number. @@ -21,24 +20,8 @@ public class Phone { * @throws IllegalValueException if given phone string is invalid. */ public Phone(String phone, boolean isPrivate) throws IllegalValueException { - this.isPrivate = isPrivate; - String trimmedPhone = phone.trim(); - if (!isValidPhone(trimmedPhone)) { - throw new IllegalValueException(MESSAGE_PHONE_CONSTRAINTS); - } - this.value = trimmedPhone; - } - - /** - * Returns true if the given string is a valid person phone number. - */ - public static boolean isValidPhone(String test) { - return test.matches(PHONE_VALIDATION_REGEX); - } - - @Override - public String toString() { - return value; + super(phone, isPrivate, MESSAGE_PHONE_CONSTRAINTS, PHONE_VALIDATION_REGEX); + this.value = getValue(); } @Override @@ -47,13 +30,4 @@ public boolean equals(Object other) { || (other instanceof Phone // instanceof handles nulls && this.value.equals(((Phone) other).value)); // state check } - - @Override - public int hashCode() { - return value.hashCode(); - } - - public boolean isPrivate() { - return isPrivate; - } }