From 46c7fe3424681895de47f91a445fd9276a262d0b Mon Sep 17 00:00:00 2001 From: Yoochard Date: Sun, 4 Mar 2018 21:31:27 +0800 Subject: [PATCH 1/3] Override getPrintableString() method --- src/seedu/addressbook/data/person/Address.java | 11 ++++++++++- src/seedu/addressbook/data/person/Email.java | 11 +++++++++-- src/seedu/addressbook/data/person/Name.java | 6 +++++- src/seedu/addressbook/data/person/Phone.java | 11 ++++++++++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/seedu/addressbook/data/person/Address.java b/src/seedu/addressbook/data/person/Address.java index 8ac726444..35ba76a4b 100644 --- a/src/seedu/addressbook/data/person/Address.java +++ b/src/seedu/addressbook/data/person/Address.java @@ -6,7 +6,7 @@ * 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 implements Printable{ public static final String EXAMPLE = "123, some street"; public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses can be in any format"; @@ -52,6 +52,15 @@ public int hashCode() { return value.hashCode(); } + @Override + public String getPrintableString() { + if(isPrivate()) { + return "Address: (private) " + value; + } else { + return "Address: " + value; + } + } + public boolean isPrivate() { return isPrivate; } diff --git a/src/seedu/addressbook/data/person/Email.java b/src/seedu/addressbook/data/person/Email.java index c946f1eb3..c0701ef6d 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 implements Printable{ public static final String EXAMPLE = "valid@e.mail"; public static final String MESSAGE_EMAIL_CONSTRAINTS = @@ -54,7 +54,14 @@ public int hashCode() { return value.hashCode(); } - + @Override + public String getPrintableString() { + if(isPrivate()) { + return "Email: (private) " + value; + } else { + return "Email: " + value; + } + } public boolean isPrivate() { return isPrivate; } diff --git a/src/seedu/addressbook/data/person/Name.java b/src/seedu/addressbook/data/person/Name.java index 487b7ad9c..27810622b 100644 --- a/src/seedu/addressbook/data/person/Name.java +++ b/src/seedu/addressbook/data/person/Name.java @@ -9,7 +9,7 @@ * Represents a Person's name in the address book. * Guarantees: immutable; is valid as declared in {@link #isValidName(String)} */ -public class Name { +public class Name implements Printable{ public static final String EXAMPLE = "John Doe"; public static final String MESSAGE_NAME_CONSTRAINTS = "Person names should be spaces or alphanumeric characters"; @@ -61,4 +61,8 @@ public int hashCode() { return fullName.hashCode(); } + @Override + public String getPrintableString() { + return "Name: " + fullName; + } } diff --git a/src/seedu/addressbook/data/person/Phone.java b/src/seedu/addressbook/data/person/Phone.java index b5a556de4..9285bf96f 100644 --- a/src/seedu/addressbook/data/person/Phone.java +++ b/src/seedu/addressbook/data/person/Phone.java @@ -6,7 +6,7 @@ * 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 implements Printable{ public static final String EXAMPLE = "123456789"; public static final String MESSAGE_PHONE_CONSTRAINTS = "Person phone numbers should only contain numbers"; @@ -53,6 +53,15 @@ public int hashCode() { return value.hashCode(); } + @Override + public String getPrintableString() { + if(isPrivate()) { + return "Phone: (private) " + value; + } else { + return "Phone: " + value; + } + } + public boolean isPrivate() { return isPrivate; } From 060cc9b2de8d9f09aac1fa5d8589840bfe611cb2 Mon Sep 17 00:00:00 2001 From: Yoochard Date: Sun, 4 Mar 2018 21:31:42 +0800 Subject: [PATCH 2/3] Create Printable Interface --- src/seedu/addressbook/data/person/Printable.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/seedu/addressbook/data/person/Printable.java diff --git a/src/seedu/addressbook/data/person/Printable.java b/src/seedu/addressbook/data/person/Printable.java new file mode 100644 index 000000000..e7569b20f --- /dev/null +++ b/src/seedu/addressbook/data/person/Printable.java @@ -0,0 +1,15 @@ +package seedu.addressbook.data.person; + + +/** + * This is an interface to provide an object's printable representation. + */ +public interface Printable { + + + /** + * Returns a String representation of an object + */ + + String getPrintableString(); +} From ea66c0cb2aa229cffb0bf6a69d3e9b974d227555 Mon Sep 17 00:00:00 2001 From: Yoochard Date: Sun, 4 Mar 2018 21:32:42 +0800 Subject: [PATCH 3/3] Use StringBuilder & StringJoiner to return, adjust getAsTextShowAll() method --- .../data/person/ReadOnlyPerson.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/seedu/addressbook/data/person/ReadOnlyPerson.java b/src/seedu/addressbook/data/person/ReadOnlyPerson.java index f3678da99..0f693ca26 100644 --- a/src/seedu/addressbook/data/person/ReadOnlyPerson.java +++ b/src/seedu/addressbook/data/person/ReadOnlyPerson.java @@ -2,6 +2,7 @@ import seedu.addressbook.data.tag.Tag; import seedu.addressbook.data.tag.UniqueTagList; +import java.util.StringJoiner; /** * A read-only immutable interface for a Person in the addressbook. @@ -36,29 +37,14 @@ default boolean isSameStateAs(ReadOnlyPerson other) { * Formats the person as text, showing all contact details. */ default String getAsTextShowAll() { - final StringBuilder builder = new StringBuilder(); - final String detailIsPrivate = "(private) "; - builder.append(getName()) - .append(" Phone: "); - if (getPhone().isPrivate()) { - builder.append(detailIsPrivate); - } - builder.append(getPhone()) - .append(" Email: "); - if (getEmail().isPrivate()) { - builder.append(detailIsPrivate); - } - builder.append(getEmail()) - .append(" Address: "); - if (getAddress().isPrivate()) { - builder.append(detailIsPrivate); - } - builder.append(getAddress()) - .append(" Tags: "); + String info = getPrintableString(getName(), getPhone(), getEmail(), getAddress()); + final StringBuilder sb = new StringBuilder(); + sb.append(info); + sb.append(" Tags: "); for (Tag tag : getTags()) { - builder.append(tag); + sb.append(tag); } - return builder.toString(); + return sb.toString(); } /** @@ -82,4 +68,12 @@ default String getAsTextHidePrivate() { } return builder.toString(); } + + default String getPrintableString(Printable... printables) { + StringJoiner sj = new StringJoiner(" "); + for (Printable printable: printables) { + sj.add(printable.getPrintableString()); + } + return sj.toString(); + } }