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

[T6A3][F11-C3] Le Minh Duc #848

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
60 changes: 5 additions & 55 deletions src/seedu/addressbook/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import seedu.addressbook.commands.*;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.preparedcommands.PreparedCommand;

import java.util.*;

Choose a reason for hiding this comment

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

resolve imports properly. It is easy to do with eclipse shortcut.

import java.util.regex.Matcher;
Expand All @@ -19,14 +20,6 @@ public class Parser {
public static final Pattern KEYWORDS_ARGS_FORMAT =
Pattern.compile("(?<keywords>\\S+(?:\\s+\\S+)*)"); // one or more keywords separated by whitespace

public static final Pattern PERSON_DATA_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes
Pattern.compile("(?<name>[^/]+)"
+ " (?<isPhonePrivate>p?)p/(?<phone>[^/]+)"
+ " (?<isEmailPrivate>p?)e/(?<email>[^/]+)"
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags


/**
* Signals that the user input could not be parsed.
*/
Expand Down Expand Up @@ -57,7 +50,9 @@ public Command parseCommand(String userInput) {

final String commandWord = matcher.group("commandWord");
final String arguments = matcher.group("arguments");
switch (commandWord) {
PreparedCommand pc = new PreparedCommand(arguments);
return pc.prepare();
/*switch (commandWord) {
Copy link

@cmkumar87 cmkumar87 Oct 7, 2016

Choose a reason for hiding this comment

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

Don't comment out code that is not being used. It is an ugly practice when a revision control system such as git is available. We can simply revert a change if it doesn't work. So, if you want to remove / move a chunk of of code commit after doing so to create a point to which you can revert to, if you wish so later.


case AddCommand.COMMAND_WORD:
return prepareAdd(arguments);
Expand Down Expand Up @@ -87,6 +82,7 @@ public Command parseCommand(String userInput) {
default:
return new HelpCommand();
}
*/
}

/**
Expand All @@ -95,52 +91,6 @@ public Command parseCommand(String userInput) {
* @param args full command args string
* @return the prepared command
*/
private Command prepareAdd(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, AddCommand.MESSAGE_USAGE));
}
try {
return new AddCommand(
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());
}
}

/**
* Checks whether the private prefix of a contact detail in the add command's arguments string is present.
*/
private static boolean isPrivatePrefixPresent(String matchedPrefix) {
return matchedPrefix.equals("p");
}

/**
* Extracts the new person's tags from the add command's tag arguments string.
* Merges duplicate tag strings.
*/
private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalValueException {
// no tags
if (tagArguments.isEmpty()) {
return Collections.emptySet();
}
// replace first delimiter prefix, then split
final Collection<String> tagStrings = Arrays.asList(tagArguments.replaceFirst(" t/", "").split(" t/"));
return new HashSet<>(tagStrings);
}


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

import static seedu.addressbook.common.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

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

import seedu.addressbook.commands.AddCommand;
import seedu.addressbook.commands.Command;
import seedu.addressbook.commands.IncorrectCommand;
import seedu.addressbook.data.exception.IllegalValueException;

public class PreparedAddCommand extends PreparedCommand {

public static final Pattern PERSON_DATA_ARGS_FORMAT = // '/' forward slashes are reserved for delimiter prefixes
Pattern.compile("(?<name>[^/]+)"
+ " (?<isPhonePrivate>p?)p/(?<phone>[^/]+)"
+ " (?<isEmailPrivate>p?)e/(?<email>[^/]+)"
+ " (?<isAddressPrivate>p?)a/(?<address>[^/]+)"
+ "(?<tagArguments>(?: t/[^/]+)*)"); // variable number of tags


PreparedAddCommand(String args) {
super(args);
}

@Override
public Command prepare(){
final Matcher matcher = PERSON_DATA_ARGS_FORMAT.matcher(arguments.trim());
// Validate arg string format
if (!matcher.matches()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
try {
return new AddCommand(
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());
}
}

/**
* Checks whether the private prefix of a contact detail in the add command's arguments string is present.
*/
private static boolean isPrivatePrefixPresent(String matchedPrefix) {
return matchedPrefix.equals("p");
}

/**
* Extracts the new person's tags from the add command's tag arguments string.
* Merges duplicate tag strings.
*/
private static Set<String> getTagsFromArgs(String tagArguments) throws IllegalValueException {
// no tags
if (tagArguments.isEmpty()) {
return Collections.emptySet();
}
// replace first delimiter prefix, then split
final Collection<String> tagStrings = Arrays.asList(tagArguments.replaceFirst(" t/", "").split(" t/"));
return new HashSet<>(tagStrings);
}

}
17 changes: 17 additions & 0 deletions src/seedu/addressbook/preparedcommands/PreparedCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package seedu.addressbook.preparedcommands;

import seedu.addressbook.commands.Command;

public class PreparedCommand {

protected final String arguments;

public PreparedCommand(String args) {
this.arguments = args;
}

public Command prepare() {
return null;
}

}