forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 6
/
DeleteCommandParser.java
78 lines (67 loc) · 2.59 KB
/
DeleteCommandParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package io.xpire.logic.parser;
import java.util.Set;
import io.xpire.commons.core.Messages;
import io.xpire.commons.core.index.Index;
import io.xpire.logic.commands.DeleteCommand;
import io.xpire.logic.parser.exceptions.ParseException;
import io.xpire.model.item.Quantity;
import io.xpire.model.tag.Tag;
/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteCommandParser implements Parser<DeleteCommand> {
/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args) throws ParseException {
String[] splitArgs = args.split("\\|", 2);
Index index;
try {
index = ParserUtil.parseIndex(splitArgs[0]);
} catch (ParseException pe) {
throw new ParseException(
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
}
if (containsTag(splitArgs)) {
return deleteTagsCommand(index, splitArgs[1]);
} else if (containsQuantity(splitArgs)) {
return deleteQuantityCommand(index, splitArgs[1]);
} else if (splitArgs.length > 1) {
throw new ParseException(
String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
} else {
return deleteItemCommand(index);
}
}
private boolean containsTag (String[] args) {
return args.length > 1 && args[1].contains("#");
}
private boolean containsQuantity(String[] args) {
return (args.length == 2)
? isIntegerType(args[1]) : false;
}
/**
* Returns true if given argument is of type integer.
*/
private boolean isIntegerType(String arg) {
try {
Integer.parseInt(arg);
} catch (NumberFormatException e) {
return false;
}
return true;
}
private DeleteCommand deleteItemCommand(Index index) {
return new DeleteCommand(index);
}
private DeleteCommand deleteTagsCommand(Index index, String arg) throws ParseException {
Set<Tag> set = ParserUtil.parseTagsFromInput(arg);
return new DeleteCommand(index, set);
}
private DeleteCommand deleteQuantityCommand(Index index, String arg) throws ParseException {
Quantity newQuantity = ParserUtil.parseQuantity(arg);
return new DeleteCommand(index, newQuantity);
}
}