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][T12-4]Lau Wei Tang #169

Open
wants to merge 2 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
6 changes: 4 additions & 2 deletions docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ Put a `p` before the phone / email / address prefixes to mark it as `private`. `
be seen using the `viewall` command.

Persons can have any number of tags (including 0).

Address must be of this format: `blockNumber, streetNumber, unitNumber, postalCodeNumber`
****

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/123, John street, #01-01, S111111`
* `add Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison, Newgate street, #21-03, S222222 t/criminal t/friend`

== Listing all persons : `list`

Expand Down
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.12.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class AddCommand extends Command {
+ "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"
+ "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, S600292 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 Down
77 changes: 71 additions & 6 deletions src/seedu/addressbook/data/person/Address.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@

import seedu.addressbook.data.exception.IllegalValueException;

import java.util.Objects;

/**
* Represents a Person's address in the address book.
* Guarantees: immutable; is valid as declared in {@link #isValidAddress(String)}
*/
public class Address {

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 EXAMPLE = "123, some street, some unit, some postal code";
public static final String MESSAGE_ADDRESS_CONSTRAINTS = "Person addresses must have 4 parameters only.";
public static final String ADDRESS_VALIDATION_REGEX = ".+";

//public final String value;
private final Block block;
private final Street street;
private final Unit unit;
private final PostalCode postalCode;
public final String value;

private boolean isPrivate;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use blank lines sparingly. It's good to have blank lines to separate logical code blocks, but too many blank lines will increase the height of the code, which increases the amount of scrolling.


/**
Expand All @@ -27,30 +35,87 @@ public Address(String address, boolean isPrivate) throws IllegalValueException {
throw new IllegalValueException(MESSAGE_ADDRESS_CONSTRAINTS);
}
this.value = trimmedAddress;
this.block = assignBlock(trimmedAddress);
this.street = assignStreet(trimmedAddress);
this.unit = assignUnit(trimmedAddress);
this.postalCode = assignPostalCode(trimmedAddress);

}

/*
* Returns the Person's block of his address.
*
* @param trimmedAddress the String to be operated on
*/
public Block assignBlock(String trimmedAddress) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a different name for this method, as the block is not in fact assigned, but rather extracted out from the trimmed address.

return new Block(trimmedAddress.split(",")[0].trim());
}

/*
* Returns the Person's street of his address.
*
* @param trimmedAddress the String to be operated on
*/
public Street assignStreet(String trimmedAddress) {
return new Street(trimmedAddress.split(",")[1].trim());
}

/*
* Returns the Person's unit of his address.
*
* @param trimmedAddress the String to be operated on
*/
public Unit assignUnit(String trimmedAddress) {
return new Unit(trimmedAddress.split(",")[2].trim());
}

/*
* Returns the Person's block of his address.
*
* @param trimmedAddress the String to be operated on
*/
public PostalCode assignPostalCode(String trimmedAddress) {
return new PostalCode(trimmedAddress.split(",")[3].trim());
}

/**
* Returns true if a given string is a valid person address.
*/
public static boolean isValidAddress(String test) {
return test.matches(ADDRESS_VALIDATION_REGEX);
return (test.matches(ADDRESS_VALIDATION_REGEX)) && (test.split(",").length == 4);
}

public Block getBlock() {
return this.block;
}

public Street getStreet() {
return this.street;
}

public Unit getUnit() {
return this.unit;
}

public PostalCode getPostalCode() {
return this.postalCode;
}

@Override
public String toString() {
return value;
return block + ", " + street + ", " + unit + ", " + postalCode;
}

@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
&& this.value.equals(((Address) other).value));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! However, the indentation for wrapped lines should be 8 spaces.

}

@Override
public int hashCode() {
return value.hashCode();
return Objects.hash(block, street, unit, postalCode);
}

public boolean isPrivate() {
Expand Down
25 changes: 25 additions & 0 deletions src/seedu/addressbook/data/person/Block.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.data.person;

/**
* The block of a person address.
*/
public class Block {
private String blockNumber;

public Block(String blockNumber) {
this.blockNumber = blockNumber;
}

public String getBlockNumber() {
return this.blockNumber;
}

public void setBlockNumber(String blockNumber) {
this.blockNumber = blockNumber;
}

@Override
public String toString() {
return this.blockNumber;
}
}
25 changes: 25 additions & 0 deletions src/seedu/addressbook/data/person/PostalCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.data.person;

/**
* The postal code of a person address.
*/
public class PostalCode {
private String postalCodeNumber;

public PostalCode(String postalCodeNumber) {
this.postalCodeNumber = postalCodeNumber;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possible improvement is to utilize regex so that the postal code must be of the form "Sxxxxxx", where x must be a digit from 0 to 9.

}

public String getPostalCodeNumber() {
return this.postalCodeNumber;
}

public void setPostalCodeNumber(String postalCodeNumber) {
this.postalCodeNumber = postalCodeNumber;
}

@Override
public String toString() {
return this.postalCodeNumber;
}
}
25 changes: 25 additions & 0 deletions src/seedu/addressbook/data/person/Street.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.data.person;

/**
* The street of a person address.
*/
public class Street {
private String streetNumber;

public Street(String streetNumber) {
this.streetNumber = streetNumber;
}

public String getStreetNumber() {
return this.streetNumber;
}

public void setStreetNumber(String streetNumber) {
this.streetNumber = streetNumber;
}

@Override
public String toString() {
return this.streetNumber;
}
}
25 changes: 25 additions & 0 deletions src/seedu/addressbook/data/person/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package seedu.addressbook.data.person;

/**
* The unit of a person address.
*/
public class Unit {
private String unitNumber;

public Unit(String unitNumber) {
this.unitNumber = unitNumber;
}

public String getUnitNumber() {
return this.unitNumber;
}

public void setUnitNumber(String unitNumber) {
this.unitNumber = unitNumber;
}

@Override
public String toString() {
return this.unitNumber;
}
}
6 changes: 5 additions & 1 deletion src/seedu/addressbook/storage/AddressBookEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ private static String encodePersonToString(Person person) {
encodedPersonBuilder.append("e/").append(person.getEmail().value);

encodedPersonBuilder.append(person.getAddress().isPrivate() ? " p" : " ");
encodedPersonBuilder.append("a/").append(person.getAddress().value);
encodedPersonBuilder.append("a/")
.append(person.getAddress().getBlock().getBlockNumber() + ", ")
.append(person.getAddress().getStreet().getStreetNumber() + ", ")
.append(person.getAddress().getUnit().getUnitNumber() + ", ")
.append(person.getAddress().getPostalCode().getPostalCodeNumber());

person.getTags().forEach(tag -> encodedPersonBuilder.append(" t/").append(tag.tagName));

Expand Down
4 changes: 2 additions & 2 deletions test/data/StorageFileTest/ValidData.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
John Doe p/98765432 e/[email protected] a/John street, block 123, #01-01
Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison t/friend t/criminal
John Doe p/98765432 e/[email protected] a/123, John street, #01-01, S111111
Betsy Crowe pp/1234567 e/[email protected] pa/Newgate Prison, Newgate street, #21-03, S222222 t/friend t/criminal
Loading