forked from nus-cs2103-AY2122S2/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
266 lines (251 loc) · 8.27 KB
/
Parser.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package duke.helptool;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import duke.command.AddCommand;
import duke.command.Command;
import duke.command.DeleteCommand;
import duke.command.ExitCommand;
import duke.command.FindCommand;
import duke.command.ListCommand;
import duke.command.MarkCommand;
import duke.command.UnmarkCommand;
import duke.tag.Tag;
/**
* The type Parser.
*/
public class Parser {
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm");
/**
* Instantiates a new Parser.
*/
public Parser() {
}
/**
* Is valid date boolean.
*
* @param input the input
* @return the boolean
*/
public static boolean isValidDate(String input) {
try {
LocalDateTime.parse(input, dateFormat);
return true;
} catch (DateTimeParseException dtpe) {
return false;
}
}
/**
* Is empty boolean.
*
* @param input the input
* @return the boolean
*/
public static boolean isEmpty(String input) {
if (input.equals("")) {
return true;
}
int curr = 0;
while (curr < input.length()) {
if (input.charAt(curr) != ' ') {
return false;
}
curr++;
}
return true;
}
/**
* Add tag help method.
*
* @param input the input
* @return the tag
*/
public static Tag addTag(String input) {
String tagDescription;
if (!input.contains("#")) {
tagDescription = "";
} else {
int tagPos = input.indexOf("#");
tagDescription = input.substring(tagPos + 1);
}
return new Tag(tagDescription);
}
/**
* Add tag help method.
*
* @param input the input
* @param type the type
* @return the tag
*/
public static String getDescription(String input, String type) {
String description;
if (type.equals("T") && !input.contains("#")) {
description = input.substring(4);
} else if (type.equals("T") && input.contains("#")) {
int tagPos = input.indexOf("#");
description = input.substring(4, tagPos);
} else if (type.equals("D")) {
int byPos = input.indexOf("/by");
description = input.substring(9, byPos);
} else if (type.equals("E")) {
int atPos = input.indexOf("/at");
description = input.substring(6, atPos);
} else {
description = "";
}
return description;
}
/**
* Gets tag pos.
*
* @param input the input
* @return the tag pos
*/
public static int getTagPos(String input) {
if (!input.contains("#")) {
return -1;
} else {
return input.indexOf("#");
}
}
/**
* Parse action command.
*
* @param input the input
* @return the command
* @throws DukeException the duke exception
*/
public static Command parseAction(String input) throws DukeException {
boolean isDigit = !Character.isDigit(input.charAt(input.length() - 1));
int markDigit = Character.getNumericValue(input.charAt(input.length() - 1));
if (input.startsWith("mark")) {
// detect digit
if (isDigit) {
throw new DukeException("☹ OOPS!!! The mark cannot be done.");
}
return new MarkCommand(markDigit);
} else if (input.startsWith("unmark")) {
// detect unmark
if (isDigit) {
throw new DukeException("☹ OOPS!!! The unmark cannot be done.");
}
return new UnmarkCommand(markDigit);
} else {
// detect delete
if (isDigit) {
throw new DukeException("☹ OOPS!!! The delete cannot be done.");
}
return new DeleteCommand(markDigit);
}
}
/**
* Generate todo command.
*
* @param input the input
* @return the command
* @throws DukeException the duke exception
*/
public static Command generateTodo(String input) throws DukeException {
String description = getDescription(input, "T");
if (isEmpty(description)) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
Tag newTag = addTag(input);
return new AddCommand(description, "T", newTag);
}
/**
* Generate deadline command.
*
* @param input the input
* @return the command
* @throws DukeException the duke exception
*/
public static Command generateDeadline(String input) throws DukeException {
if (isEmpty(input.substring(8))) {
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
}
if (!input.contains("/by")) {
throw new DukeException("☹ OOPS!!! The date of a deadline cannot be empty.");
}
int byPos = input.indexOf("/by");
String description = getDescription(input, "D");
String byDateTime;
if (!input.contains("#")) {
byDateTime = input.substring(byPos + 4);
} else {
byDateTime = input.substring(byPos + 4, getTagPos(input) - 1);
}
if (isValidDate(byDateTime)) {
LocalDateTime dateTime = LocalDateTime.parse(byDateTime, dateFormat);
Tag newTag = addTag(input);
return new AddCommand(description, "D", dateTime, newTag);
} else {
throw new DukeException("☹ OOPS!!! The date time is not of correct format dd/MM/yyyy HHmm.");
}
}
/**
* Generate event command.
*
* @param input the input
* @return the command
* @throws DukeException the duke exception
*/
public static Command generateEvent(String input) throws DukeException {
if (isEmpty(input.substring(5))) {
throw new DukeException("☹ OOPS!!! The description of a event cannot be empty.");
}
if (!input.contains("/at")) {
throw new DukeException("☹ OOPS!!! The date of a event cannot be empty.");
}
int atPos = input.indexOf("/at");
String description = getDescription(input, "E");
String atDateTime;
if (!input.contains("#")) {
atDateTime = input.substring(atPos + 4);
} else {
atDateTime = input.substring(atPos + 4, getTagPos(input) - 1);
}
if (isValidDate(atDateTime)) {
Tag newTag = addTag(input);
LocalDateTime dateTime = LocalDateTime.parse(atDateTime, dateFormat);
return new AddCommand(description, "E", dateTime, newTag);
} else {
throw new DukeException("☹ OOPS!!! The date time is not of correct format dd/MM/yyyy HHmm.");
}
}
/**
* Parse command.
*
* @param input the input
* @return the command
* @throws DukeException the duke exception
*/
public static Command parse(String input) throws DukeException {
if (input.equals("bye")) {
// detect bye
return new ExitCommand();
} else if (input.equals("list")) {
// detect list
return new ListCommand();
} else if (input.startsWith("find")) {
String findDetail = input.substring(4);
if (isEmpty(findDetail)) {
throw new DukeException("☹ OOPS!!! The description of a find cannot be empty.");
}
return new FindCommand(findDetail);
} else {
if (input.startsWith("mark") || input.startsWith("unmark") || input.startsWith("delete")) {
return parseAction(input);
} else if (input.startsWith("todo")) {
// generate
return generateTodo(input);
} else if (input.startsWith("deadline")) {
return generateDeadline(input);
} else if (input.startsWith("event")) {
return generateEvent(input);
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
}
}