From d921105a1b2a67dba7cb2900f419798baaa77b9c Mon Sep 17 00:00:00 2001 From: Lee Jin Shun Date: Mon, 20 Feb 2017 09:55:00 +0800 Subject: [PATCH] Extract commonalities from Phone, Email and Address classes into a parent class called Contact --- .../addressbook/data/person/Address.java | 30 ++------------- .../addressbook/data/person/Contact.java | 37 +++++++++++++++++++ src/seedu/addressbook/data/person/Email.java | 31 ++-------------- src/seedu/addressbook/data/person/Phone.java | 30 ++------------- 4 files changed, 47 insertions(+), 81 deletions(-) create mode 100644 src/seedu/addressbook/data/person/Contact.java diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 25e462ad6..c1575f867 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -6,14 +6,12 @@ * 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,14 +19,11 @@ 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)) { + super(address, isPrivate); + if (!isValidAddress(this.value)) { throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS); } - this.value = trimmedAddress; } - /** * Returns true if a given string is a valid person email. */ @@ -36,24 +31,5 @@ public static boolean isValidAddress(String test) { return test.matches(ADDRESS_VALIDATION_REGEX); } - @Override - public String toString() { - return value; - } - - @Override - public boolean equals(Object other) { - return other == this // short circuit if same object - || (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..ac3c3acec --- /dev/null +++ b/src/seedu/addressbook/data/person/Contact.java @@ -0,0 +1,37 @@ +package seedu.addressbook.data.person; + +/** + * Parent Class of Phone, Email, Address. + */ + +public abstract class Contact { + public final String value; + private boolean isPrivate; + + public Contact(String info, boolean isPrivate){ + String trimmedInfo = info.trim(); + this.isPrivate = isPrivate; + this.value = trimmedInfo; + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || other.getClass().isInstance(this) // instanceof handles nulls + && this.value.equals(this.getClass().cast(other).value); // state check + } + + @Override + public int hashCode() { + return value.hashCode(); + } + + public boolean isPrivate() { + return isPrivate; + } +} diff --git a/src/seedu/addressbook/data/person/Email.java b/src/seedu/addressbook/data/person/Email.java index e242e6d36..9a796068f 100644 --- a/src/seedu/addressbook/data/person/Email.java +++ b/src/seedu/addressbook/data/person/Email.java @@ -6,15 +6,14 @@ * 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 = "Person emails should be 2 alphanumeric/period strings separated by '@'"; public static final String EMAIL_VALIDATION_REGEX = "[\\w\\.]+@[\\w\\.]+"; - public final String value; - private boolean isPrivate; + /** * Validates given email. @@ -22,12 +21,10 @@ 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)) { + super(email, isPrivate); + if (!isValidEmail(value)) { throw new IllegalValueException(MESSAGE_EMAIL_CONSTRAINTS); } - this.value = trimmedEmail; } /** @@ -37,25 +34,5 @@ public static boolean isValidEmail(String test) { return test.matches(EMAIL_VALIDATION_REGEX); } - @Override - public String toString() { - return value; - } - - @Override - public boolean equals(Object other) { - return other == this // short circuit if same object - || (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 942ab6619..590902e94 100644 --- a/src/seedu/addressbook/data/person/Phone.java +++ b/src/seedu/addressbook/data/person/Phone.java @@ -6,14 +6,12 @@ * 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,12 +19,10 @@ 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)) { + super(phone, isPrivate); + if (!isValidPhone(this.value)) { throw new IllegalValueException(MESSAGE_PHONE_CONSTRAINTS); } - this.value = trimmedPhone; } /** @@ -36,24 +32,4 @@ public static boolean isValidPhone(String test) { return test.matches(PHONE_VALIDATION_REGEX); } - @Override - public String toString() { - return value; - } - - @Override - public boolean equals(Object other) { - return other == this // short circuit if same object - || (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; - } }