-
Notifications
You must be signed in to change notification settings - Fork 249
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
base: master
Are you sure you want to change the base?
Changes from all commits
fe2956b
bc602e2
96ba584
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import seedu.addressbook.commands.*; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.preparedcommands.PreparedCommand; | ||
|
||
import java.util.*; | ||
import java.util.regex.Matcher; | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -87,6 +82,7 @@ public Command parseCommand(String userInput) { | |
default: | ||
return new HelpCommand(); | ||
} | ||
*/ | ||
} | ||
|
||
/** | ||
|
@@ -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); | ||
} | ||
|
||
|
||
/** | ||
|
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); | ||
} | ||
|
||
} |
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; | ||
} | ||
|
||
} |
There was a problem hiding this comment.
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.