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][T11-1]Jesulayomi Kupoluyi #50

Open
wants to merge 3 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
10 changes: 9 additions & 1 deletion docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ What's different from AddressBook-Level1:
* Support for storing address (`a/`) and tags (`t/`)
* Support for marking a contact detail as 'private' (`pa/`) (`pe/`) (`pp/`)
* View details of a person (`view` : shows non-private details), (`viewall` : shows all details)
* View the list an updated list of people when adding someone using the AddAndListCommand

== Viewing help : `help`

Expand Down Expand Up @@ -182,4 +183,11 @@ The file name must end in `.txt` for it to be acceptable to the program.

When running the program inside IntelliJ, you can set command line parameters
before running the program.
====

==== Adding And Listing in the program: 'Add and List'

This gives an updated list after adding a person. +
Format: 'AddAndList'



75 changes: 75 additions & 0 deletions src/seedu/addressbook/commands/AddAndList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.addressbook.commands;

import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.*;

Choose a reason for hiding this comment

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

Imported classes should always be listed explicitly. E.g. java.util.List and not java.util.*, which does not follow NUS Java coding standards.

import seedu.addressbook.data.tag.Tag;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class AddAndList extends Command{

/**

Choose a reason for hiding this comment

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

this comment should be placed on top of a method.

* Adds a person to the address book.
*/

public static final String COMMAND_WORD = "addAndList";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a person to the address book, and see people already in it. "
+ "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";

public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book";

private final Person toAdd;


/**
* Convenience constructor using raw values.
*
* @throws IllegalValueException if any of the raw values are invalid
*/
public AddAndList(String name,
String phone, boolean isPhonePrivate,
String email, boolean isEmailPrivate,
String address, boolean isAddressPrivate,
Set<String> tags) throws IllegalValueException {
final Set<Tag> tagSet = new HashSet<>();
for (String tagName : tags) {
tagSet.add(new Tag(tagName));
}
this.toAdd = new Person(
new Name(name),
new Phone(phone, isPhonePrivate),
new Email(email, isEmailPrivate),
new Address(address, isAddressPrivate),
tagSet
);
}

public AddAndList(Person toAdd) {
this.toAdd = toAdd;
}

public ReadOnlyPerson getPerson() {
return toAdd;
}

@Override
public CommandResult execute() {

Choose a reason for hiding this comment

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

Missing header comment. All non-trivial methods should have java doc format header comments.

try {
addressBook.addPerson(toAdd);
List<ReadOnlyPerson> allPersons = addressBook.getAllPersons().immutableListView();
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons);
} catch (UniquePersonList.DuplicatePersonException dpe) {
return new CommandResult(MESSAGE_DUPLICATE_PERSON);
}
}

}



15 changes: 5 additions & 10 deletions src/seedu/addressbook/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package seedu.addressbook.commands;

import java.util.HashSet;
import java.util.Set;

import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.Address;
import seedu.addressbook.data.person.Email;
import seedu.addressbook.data.person.Name;
import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.UniquePersonList;
import seedu.addressbook.data.person.*;

Choose a reason for hiding this comment

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

Imported classes should always be listed explicitly. E.g. java.util.List and not java.util.*, which does not follow NUS Java coding standards.

import seedu.addressbook.data.tag.Tag;

import java.util.HashSet;
import java.util.Set;

/**
* Adds a person to the address book.
*/
Expand All @@ -31,6 +25,7 @@ public class AddCommand extends Command {

private final Person toAdd;


/**
* Convenience constructor using raw values.
*
Expand Down
1 change: 1 addition & 0 deletions src/seedu/addressbook/data/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void addPerson(Person toAdd) throws DuplicatePersonException {
allPersons.add(toAdd);
}


/**
* Returns true if an equivalent person exists in the address book.
*/
Expand Down
53 changes: 34 additions & 19 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
package seedu.addressbook.parser;

import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import seedu.addressbook.commands.*;
import seedu.addressbook.data.exception.IllegalValueException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import seedu.addressbook.commands.AddCommand;
import seedu.addressbook.commands.ClearCommand;
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.DeleteCommand;
import seedu.addressbook.commands.ExitCommand;
import seedu.addressbook.commands.FindCommand;
import seedu.addressbook.commands.HelpCommand;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.commands.ListCommand;
import seedu.addressbook.commands.ViewAllCommand;
import seedu.addressbook.commands.ViewCommand;
import seedu.addressbook.data.exception.IllegalValueException;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.addressbook.common.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;

/**
* Parses user input.
Expand Down Expand Up @@ -76,6 +62,9 @@ public Command parseCommand(String userInput) {
case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);

case AddAndList.COMMAND_WORD:

Choose a reason for hiding this comment

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

This indentation is not consistent with the other cases. It affects the readability of your code.

return prepareAddAndList(arguments);

case DeleteCommand.COMMAND_WORD:
return prepareDelete(arguments);

Expand Down Expand Up @@ -135,6 +124,32 @@ private Command prepareAdd(String args) {
}
}

private Command prepareAddAndList(String args) {
final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(args.trim());
// Validate arg string format
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddAndList.MESSAGE_USAGE));
}
try {
return new AddAndList(
matcher.group("name"),

matcher.group("phone"),
isPrivatePrefixPresent(matcher.group("isPhonePrivate")),

matcher.group("email"),
isPrivatePrefixPresent(matcher.group("isEmailPrivate")),

matcher.group("address"),
isPrivatePrefixPresent(matcher.group("isAddressPrivate")),

getTagsFromArgs(matcher.group("tagArguments"))
);
} catch (IllegalValueException ive) {
return new IncorrectCommand(ive.getMessage());
}
}

/**
* Returns true if the private prefix is present for a contact detail in the add command's arguments string.
*/
Expand Down
139 changes: 139 additions & 0 deletions test/java/seedu/addressbook/commands/AndAndListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@


package seedu.addressbook.commands;

import org.junit.Test;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.data.person.*;
import seedu.addressbook.util.TestUtil;

import java.util.*;

import static org.junit.Assert.*;

public class AndAndListTest {



private static final List<ReadOnlyPerson> EMPTY_PERSON_LIST = Collections.emptyList();
private static final Set<String> EMPTY_STRING_SET = Collections.emptySet();

@Test
public void addAndList_invalidName_throwsException() {
final String[] invalidNames = { "", " ", "[]\\[;]" };
for (String name : invalidNames) {
assertConstructingInvalidAddCmdThrowsException(name, Phone.EXAMPLE, true, Email.EXAMPLE, false,
Address.EXAMPLE, true, EMPTY_STRING_SET);
}
}

@Test
public void addAndList_invalidPhone_throwsException() {
final String[] invalidNumbers = { "", " ", "1234-5678", "[]\\[;]", "abc", "a123", "+651234" };
for (String number : invalidNumbers) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, number, false, Email.EXAMPLE, true,
Address.EXAMPLE, false, EMPTY_STRING_SET);
}
}

@Test
public void addAndList_invalidEmail_throwsException() {
final String[] invalidEmails = { "", " ", "def.com", "@", "@def", "@def.com", "abc@",
"@invalid@email", "invalid@email!", "!invalid@email" };
for (String email : invalidEmails) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, false, email, false,
Address.EXAMPLE, false, EMPTY_STRING_SET);
}
}

@Test
public void addAndList_invalidAddress_throwsException() {
final String[] invalidAddresses = { "", " " };
for (String address : invalidAddresses) {
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE,
true, address, true, EMPTY_STRING_SET);
}
}

@Test
public void addAndList_invalidTags_throwsException() {
final String[][] invalidTags = { { "" }, { " " }, { "'" }, { "[]\\[;]" }, { "validTag", "" },
{ "", " " } };
for (String[] tags : invalidTags) {
Set<String> tagsToAdd = new HashSet<>(Arrays.asList(tags));
assertConstructingInvalidAddCmdThrowsException(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE,
true, Address.EXAMPLE, false, tagsToAdd);
}
}

/**
* Asserts that attempting to construct an add command with the supplied
* invalid data throws an IllegalValueException
*/
private void assertConstructingInvalidAddCmdThrowsException(String name, String phone,
boolean isPhonePrivate, String email, boolean isEmailPrivate, String address,
boolean isAddressPrivate, Set<String> tags) {
try {
new AddCommand(name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate,
tags);
} catch (IllegalValueException e) {
return;
}
String error = String.format(
"An add command was successfully constructed with invalid input: %s %s %s %s %s %s %s %s",
name, phone, isPhonePrivate, email, isEmailPrivate, address, isAddressPrivate, tags);
fail(error);
}

@Test
public void addAndList_validData_correctlyConstructed() throws Exception {
AddCommand command = new AddCommand(Name.EXAMPLE, Phone.EXAMPLE, true, Email.EXAMPLE, false,
Address.EXAMPLE, true, EMPTY_STRING_SET);
ReadOnlyPerson p = command.getPerson();

// TODO: add comparison of tags to person.equals and equality methods to
// individual fields that compare privacy to simplify this
assertEquals(Name.EXAMPLE, p.getName().fullName);
assertEquals(Phone.EXAMPLE, p.getPhone().value);
assertTrue(p.getPhone().isPrivate());
assertEquals(Email.EXAMPLE, p.getEmail().value);
assertFalse(p.getEmail().isPrivate());
assertEquals(Address.EXAMPLE, p.getAddress().value);
assertTrue(p.getAddress().isPrivate());
boolean isTagListEmpty = !p.getTags().iterator().hasNext();
assertTrue(isTagListEmpty);
}

@Test
public void addAndList_emptyAddressBook_addressBookContainsPerson() {
Person p = TestUtil.generateTestPerson();
AddCommand command = new AddCommand(p);
AddressBook book = new AddressBook();
command.setData(book, EMPTY_PERSON_LIST);
CommandResult result = command.execute();
UniquePersonList people = book.getAllPersons();

assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
assertFalse(result.getRelevantPersons().isPresent());
assertEquals(String.format(AddCommand.MESSAGE_SUCCESS, p), result.feedbackToUser);
}

@Test
public void addAndList_addressBookAlreadyContainsPerson_addressBookUnmodified() throws Exception {
Person p = TestUtil.generateTestPerson();
AddressBook book = new AddressBook();
book.addPerson(p);
AddCommand command = new AddCommand(p);
command.setData(book, EMPTY_PERSON_LIST);
CommandResult result = command.execute();

assertFalse(result.getRelevantPersons().isPresent());
assertEquals(AddCommand.MESSAGE_DUPLICATE_PERSON, result.feedbackToUser);
UniquePersonList people = book.getAllPersons();
assertTrue(people.contains(p));
assertEquals(1, people.immutableListView().size());
}
}