Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[W5.6a][T16-1]Gian Zhi Cai #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 3 additions & 29 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,22 @@
* 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.
*
* @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
Expand All @@ -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;
}
}
56 changes: 56 additions & 0 deletions src/seedu/addressbook/data/person/Contact.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
33 changes: 3 additions & 30 deletions src/seedu/addressbook/data/person/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,23 @@
* 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 = "[email protected]";
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.
*
* @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
Expand All @@ -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;
}
}
32 changes: 3 additions & 29 deletions src/seedu/addressbook/data/person/Phone.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,22 @@
* 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.
*
* @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
Expand All @@ -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;
}
}