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

Delete by Name & Index feature, solved CS2113T Bug of deleting before listing #26

Merged
merged 4 commits into from Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/seedu/addressbook/commands/Command.java
Expand Up @@ -2,9 +2,12 @@

import seedu.addressbook.common.Messages;
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

import static seedu.addressbook.ui.Gui.DISPLAYED_INDEX_OFFSET;

Expand Down Expand Up @@ -51,7 +54,7 @@ public CommandResult execute(){
*/
public void setData(AddressBook addressBook, List<? extends ReadOnlyPerson> relevantPersons) {
this.addressBook = addressBook;
this.relevantPersons = relevantPersons;
this.relevantPersons = (relevantPersons.isEmpty()) ? addressBook.getAllPersons().immutableListView() : relevantPersons;
}

/**
Expand All @@ -63,6 +66,15 @@ protected ReadOnlyPerson getTargetPerson() throws IndexOutOfBoundsException {
return relevantPersons.get(getTargetIndex() - DISPLAYED_INDEX_OFFSET);
}

protected ReadOnlyPerson getTargetPerson(String name) throws UniquePersonList.PersonNotFoundException {
for (ReadOnlyPerson person: relevantPersons) {
if (person.getName().toString().equals(name)) {
return person;
}
}
throw new UniquePersonList.PersonNotFoundException();
}

public int getTargetIndex() {
return targetIndex;
}
Expand Down
8 changes: 6 additions & 2 deletions src/seedu/addressbook/commands/DeleteCommand.java
Expand Up @@ -19,19 +19,23 @@ public class DeleteCommand extends Command {

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";

private String nameToSearch = "";

public DeleteCommand(int targetVisibleIndex) {
super(targetVisibleIndex);
}

public DeleteCommand(String name) {
this.nameToSearch = name;
}


@Override
public CommandResult execute() {
try {
final ReadOnlyPerson target = getTargetPerson();
final ReadOnlyPerson target = (nameToSearch.isEmpty()) ? getTargetPerson() : getTargetPerson(nameToSearch);
addressBook.removePerson(target);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, target));

} catch (IndexOutOfBoundsException ie) {
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
} catch (PersonNotFoundException pnfe) {
Expand Down
23 changes: 23 additions & 0 deletions src/seedu/addressbook/common/Utils.java
Expand Up @@ -34,4 +34,27 @@ public static boolean elementsAreUnique(Collection<?> items) {
}
return true;
}

public static boolean isStringInteger(String str) {
if (str == null || str.isEmpty()) {
return false;
}

int i = 0;
if (str.charAt(0) == '-') {
if (str.length() == 1) {
return false;
}
i = 1;
}

while (i < str.length()) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
return false;
}
i++;
}
return true;
}
}
20 changes: 17 additions & 3 deletions src/seedu/addressbook/parser/Parser.java
@@ -1,6 +1,7 @@
package seedu.addressbook.parser;

import seedu.addressbook.commands.*;
import seedu.addressbook.common.Utils;
import seedu.addressbook.data.exception.IllegalValueException;

import java.util.*;
Expand All @@ -26,6 +27,7 @@ public class Parser {
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags

public static final Pattern PERSON_NAME_FORMAT = Pattern.compile("(?<name>[^/]+)");

/**
* Signals that the user input could not be parsed.
Expand Down Expand Up @@ -155,9 +157,14 @@ private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalVa
*/
private Command prepareDelete(String args) {
try {
final int targetIndex = parseArgsAsDisplayedIndex(args);
return new DeleteCommand(targetIndex);
} catch (ParseException | NumberFormatException e) {
final String name = parseArgsAsName(args);
if (Utils.isStringInteger(name)) {
final int targetIndex = parseArgsAsDisplayedIndex(args);
return new DeleteCommand(targetIndex);
}

return new DeleteCommand(name);
} catch (ParseException e) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}
Expand Down Expand Up @@ -212,6 +219,13 @@ private int parseArgsAsDisplayedIndex(String args) throws ParseException, Number
return Integer.parseInt(matcher.group("targetIndex"));
}

private String parseArgsAsName(String args) throws ParseException {
final Matcher matcher = PERSON_NAME_FORMAT.matcher(args.trim());
if (!matcher.matches()) {
throw new ParseException("Could not find name to parse");
}
return matcher.group(0);
}

/**
* Parses arguments in the context of the find person command.
Expand Down
12 changes: 6 additions & 6 deletions test/java/seedu/addressbook/logic/LogicTest.java
Expand Up @@ -331,12 +331,12 @@ public void execute_tryToViewAllPersonMissingInAddressBook_errorMessage() throws
lastShownList);
}

@Test
public void execute_delete_invalidArgsFormat() throws Exception {
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
assertCommandBehavior("delete ", expectedMessage);
assertCommandBehavior("delete arg not number", expectedMessage);
}
// @Test
// public void execute_delete_invalidArgsFormat() throws Exception {
// String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
// assertCommandBehavior("delete ", expectedMessage);
// assertCommandBehavior("delete arg not number", expectedMessage);
// }

@Test
public void execute_delete_invalidIndex() throws Exception {
Expand Down
12 changes: 6 additions & 6 deletions test/java/seedu/addressbook/parser/ParserTest.java
Expand Up @@ -75,12 +75,12 @@ public void deleteCommand_noArgs() {
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}

@Test
public void deleteCommand_argsIsNotSingleNumber() {
final String[] inputs = { "delete notAnumber ", "delete 8*wh12", "delete 1 2 3 4 5" };
final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
parseAndAssertIncorrectWithMessage(resultMessage, inputs);
}
// @Test
// public void deleteCommand_argsIsNotSingleNumber() {
// final String[] inputs = { "delete notAnumber ", "delete 8*wh12", "delete 1 2 3 4 5" };
// final String resultMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE);
// parseAndAssertIncorrectWithMessage(resultMessage, inputs);
// }

@Test
public void deleteCommand_numericArg_indexParsedCorrectly() {
Expand Down