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

Review Learning Outcomes #465

Open
wants to merge 20 commits 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
12 changes: 6 additions & 6 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Help is also shown if you enter an incorrect command e.g. `abcd`
== Adding a person: `add`

Adds a person to the address book. +
Format: `add NAME [p]p/PHONE_NUMBER [p]e/EMAIL [p]a/ADDRESS [t/TAG]...`
Format: `add NAME [p]p/PHONE_NUMBER [p]e/EMAIL [p]a/BLOCK, STREET, UNIT, POSTAL_CODE [t/TAG]...`

****
Words in `UPPER_CASE` are the parameters, items in `SQUARE_BRACKETS` are optional,
Expand All @@ -64,8 +64,8 @@ Persons can have any number of tags (including 0).

Examples:

* `add John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01`
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/criminal t/friend`
* `add John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01, 544483`
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison, Newgate Street, unit, 439485 t/criminal t/friend`

== Listing all persons : `list`

Expand All @@ -79,14 +79,14 @@ Format: `find KEYWORD [MORE_KEYWORDS]`

[NOTE]
====
The search is case sensitive, the order of the keywords does not matter, only the name is searched,
The search is case insensitive, the order of the keywords does not matter, only the name is searched,
and persons matching at least one keyword will be returned (i.e. `OR` search).
====

Examples:

* `find John` +
Returns `John Doe` but not `john`.
* `find John` and `find john` +
Returns `John Doe`.

* `find Betsy Tim John` +
Returns Any person having names `Betsy`, `Tim`, or `John`.
Expand Down
3 changes: 1 addition & 2 deletions src/seedu/addressbook/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private CommandResult executeCommand(Command command) {
storage.save(addressBook);
return result;
} catch (Exception e) {
ui.showToUser(e.getMessage());
throw new RuntimeException(e);
return new CommandResult(e.getMessage());
}
}

Expand Down
25 changes: 19 additions & 6 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import java.util.HashSet;
import java.util.Set;

import seedu.addressbook.data.address.Block;
import seedu.addressbook.data.address.PostalCode;
import seedu.addressbook.data.address.Street;
import seedu.addressbook.data.address.Unit;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.address.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
Expand All @@ -22,9 +26,9 @@ public class AddCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book. "
+ "Contact details can be marked private by prepending 'p' to the prefix.\n"
+ "Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/ADDRESS [t/TAG]...\n"
+ "Parameters: NAME [p]p/PHONE [p]e/EMAIL [p]a/BLOCK, STREET, UNIT, POSTAL_CODE [t/TAG]...\n"
+ "Example: " + COMMAND_WORD
+ " John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25 t/friends t/owesMoney";
+ " John Doe p/98765432 e/[email protected] a/311, Clementi Ave 2, #02-25, 568837 t/friends t/owesMoney";

public static final String MESSAGE_SUCCESS = "New person added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";
Expand All @@ -39,7 +43,11 @@ public class AddCommand extends Command {
public AddCommand(String name,
String phone, boolean isPhonePrivate,
String email, boolean isEmailPrivate,
String address, boolean isAddressPrivate,
String block,
String street,
String unit,
String postalCode,
boolean isAddressPrivate,
Set<String> tags) throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
Expand All @@ -49,7 +57,12 @@ public AddCommand(String name,
new Name(name),
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
new Address(
new Block(block),
new Street(street),
new Unit(unit),
new PostalCode(postalCode),
isAddressPrivate),
tagSet
);
}
Expand All @@ -65,7 +78,7 @@ public ReadOnlyPerson getPerson() {
@Override
public CommandResult execute() {
try {
addressBook.addPerson(toAdd);
String a = addressBook.addPerson(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
} catch (UniquePersonList.DuplicatePersonException dpe) {
return new CommandResult(MESSAGE_DUPLICATE_PERSON);
Expand Down
6 changes: 5 additions & 1 deletion src/seedu/addressbook/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class CommandResult {

/** The feedback message to be shown to the user. Contains a description of the execution result */
public final String feedbackToUser;
private final String feedbackToUser;

/** The list of persons that was produced by the command */
private final List<? extends ReadOnlyPerson> relevantPersons;
Expand All @@ -26,6 +26,10 @@ public CommandResult(String feedbackToUser, List<? extends ReadOnlyPerson> relev
this.relevantPersons = relevantPersons;
}

public String getFeedbackToUser() {
return feedbackToUser;
}

/**
* Returns a list of persons relevant to the command command result, if any.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/seedu/addressbook/commands/FindCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.addressbook.commands;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -50,11 +51,24 @@ private List<ReadOnlyPerson> getPersonsWithNameContainingAnyKeyword(Set<String>
final List<ReadOnlyPerson> matchedPersons = new ArrayList<>();
for (ReadOnlyPerson person : addressBook.getAllPersons()) {
final Set<String> wordsInName = new HashSet<>(person.getName().getWordsInName());
if (!Collections.disjoint(wordsInName, keywords)) {
if (!disjointIgnoreCase(wordsInName, keywords)) {
matchedPersons.add(person);
}
}
return matchedPersons;
}

private static boolean disjointIgnoreCase(Set<String> coll1, Set<String> coll2) {
return Collections.disjoint(lowercased(coll1), lowercased(coll2));
}

private static Set<String> lowercased(Set<String> strings) {
String[] stringsArray = strings.toArray(new String[0]);
for (int i = 0; i < stringsArray.length; ++i) {
stringsArray[i] = stringsArray[i].toLowerCase();
}
strings.clear();
strings.addAll(Arrays.asList(stringsArray));
return strings;
}
}
13 changes: 12 additions & 1 deletion src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package seedu.addressbook.data;

import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Printable;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.person.UniquePersonList.DuplicatePersonException;
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException;
import seedu.addressbook.data.tag.Tagging;

/**
* Represents the entire address book. Contains the data of the address book.
Expand Down Expand Up @@ -34,8 +36,17 @@ public AddressBook(UniquePersonList persons) {
*
* @throws DuplicatePersonException if an equivalent person already exists.
*/
public void addPerson(Person toAdd) throws DuplicatePersonException {
public String addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
return getPrintableString(toAdd.getName(), toAdd.getEmail(), toAdd.getPhone());
}

private String getPrintableString(Printable... printables) {
StringBuilder sb = new StringBuilder();
for (Printable printable : printables) {
sb.append(printable.getPrintableString()).append(", ");
}
return sb.deleteCharAt(sb.length() - 2).toString().trim();
}

/**
Expand Down
57 changes: 57 additions & 0 deletions src/seedu/addressbook/data/address/Address.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package seedu.addressbook.data.address;

/**
* Represents a Person's address in the address book.
*/
public class Address {

private Block block;
private Street street;
private Unit unit;
private PostalCode postalCode;
private boolean isPrivate;

public Address(Block block, Street street, Unit unit, PostalCode postalCode, boolean isPrivate) {
this.block = block;
this.street = street;
this.unit = unit;
this.postalCode = postalCode;
this.isPrivate = isPrivate;
}

public Block getBlock() {
return block;
}

public Street getStreet() {
return street;
}

public Unit getUnit() {
return unit;
}

public PostalCode getPostalCode() {
return postalCode;
}

@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
final String separator = ", ";
builder.append(getBlock())
.append(separator);
builder.append(getStreet())
.append(separator);
builder.append(getUnit())
.append(separator);
builder.append(getPostalCode());
return builder.toString();
}

public boolean isPrivate() {
return isPrivate;
}


}
47 changes: 47 additions & 0 deletions src/seedu/addressbook/data/address/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package seedu.addressbook.data.address;

import seedu.addressbook.data.exception.IllegalValueException;

public class Block {
public static final String EXAMPLE = "123";
private static final String MESSAGE_BLOCK_CONSTRAINTS = "Address blocks can be in any format";
private static final String BLOCK_VALIDATION_REGEX = ".+";
private final String addressBlock;

/**
* Validates given block.
*
* @throws IllegalValueException if given block string is invalid.
*/
public Block(String block) throws IllegalValueException {
String trimmedBlock = block.trim();
if (!isValidBlock(trimmedBlock)) {
throw new IllegalValueException(MESSAGE_BLOCK_CONSTRAINTS);
}
this.addressBlock = trimmedBlock;
}

/**
* Returns true if the given string is a valid address block.
*/
private static boolean isValidBlock(String test) {
return test.matches(BLOCK_VALIDATION_REGEX);
}

@Override
public String toString() {
return addressBlock;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Block // instanceof handles nulls
&& this.addressBlock.equals(((Block) other).addressBlock)); // state check
}

@Override
public int hashCode() {
return addressBlock.hashCode();
}
}
47 changes: 47 additions & 0 deletions src/seedu/addressbook/data/address/PostalCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package seedu.addressbook.data.address;

import seedu.addressbook.data.exception.IllegalValueException;

public class PostalCode {
public static final String EXAMPLE = "123456";
private static final String MESSAGE_POSTAL_CODE_CONSTRAINTS = "Address postal codes can be in any format";
private static final String POSTAL_CODE_VALIDATION_REGEX = ".+";
private final String addressPostalCode;

/**
* Validates given postal code.
*
* @throws IllegalValueException if given postal code string is invalid.
*/
public PostalCode(String postalCode) throws IllegalValueException {
String trimmedPostalCode = postalCode.trim();
if (!isValidPostalCode(trimmedPostalCode)) {
throw new IllegalValueException(MESSAGE_POSTAL_CODE_CONSTRAINTS);
}
this.addressPostalCode = trimmedPostalCode;
}

/**
* Returns true if the given string is a valid address block.
*/
private static boolean isValidPostalCode(String test) {
return test.matches(POSTAL_CODE_VALIDATION_REGEX);
}

@Override
public String toString() {
return addressPostalCode;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Block // instanceof handles nulls
&& this.addressPostalCode.equals(((PostalCode) other).addressPostalCode)); // state check
}

@Override
public int hashCode() {
return addressPostalCode.hashCode();
}
}
48 changes: 48 additions & 0 deletions src/seedu/addressbook/data/address/Street.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.addressbook.data.address;

import seedu.addressbook.data.exception.IllegalValueException;

public class Street {

public static final String EXAMPLE = "some street";
private static final String MESSAGE_STREET_CONSTRAINTS = "Address streets can be in any format";
private static final String STREET_VALIDATION_REGEX = ".+";
private final String addressStreet;

/**
* Validates given street.
*
* @throws IllegalValueException if given street string is invalid.
*/
public Street(String street) throws IllegalValueException {
String trimmedStreet = street.trim();
if (!isValidStreet(trimmedStreet)) {
throw new IllegalValueException(MESSAGE_STREET_CONSTRAINTS);
}
this.addressStreet = trimmedStreet;
}

/**
* Returns true if the given string is a valid address street.
*/
private static boolean isValidStreet(String test) {
return test.matches(STREET_VALIDATION_REGEX);
}

@Override
public String toString() {
return addressStreet;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Street // instanceof handles nulls
&& this.addressStreet.equals(((Street) other).addressStreet)); // state check
}

@Override
public int hashCode() {
return addressStreet.hashCode();
}
}
Loading