11package spike .parser ;
2+
23import java .time .LocalDateTime ;
34import java .time .format .DateTimeFormatter ;
45import java .time .format .DateTimeParseException ;
@@ -52,11 +53,14 @@ public String getCommand() {
5253 * @return a proper command to be executed
5354 */
5455 public Command parseCommand (String inputLine , TaskList tasks ) {
56+ // Extract the words
5557 String [] commandWords = inputLine .split (" " );
58+ // Get the command name and check validity
5659 CommandName type = validateCommand (commandWords [0 ]);
5760 if (type == null ) {
5861 return new IncorrectCommand ("Sorry, I am not programmed to do this yet :(" );
5962 }
63+ // Start to generate command
6064 switch (type ) {
6165 case LIST :
6266 try {
@@ -100,14 +104,27 @@ public Command parseCommand(String inputLine, TaskList tasks) {
100104 }
101105 }
102106
107+ /**
108+ * Parses the find command.
109+ *
110+ * @param inputLine user raw input
111+ * @return a command object ready to be executed
112+ * @throws SpikeException if the keyword for find is missing
113+ */
103114 private Command parseFind (String inputLine ) throws SpikeException {
104115 if (inputLine .length () <= 5 ) {
105116 throw new SpikeException ("Kindly enter the keyword for finding task" );
106117 }
107118 return new FindCommand (inputLine .substring (5 ));
108119 }
109120
110-
121+ /**
122+ * Parses the list command.
123+ *
124+ * @param inputLine user raw input
125+ * @return a command object ready to be executed
126+ * @throws SpikeException if the keyword for list by date is missing
127+ */
111128 private Command parseList (String inputLine ) throws SpikeException {
112129 if (inputLine .length () >= 5 ) {
113130 // User tries to list task by date
@@ -122,6 +139,15 @@ private Command parseList(String inputLine) throws SpikeException {
122139 }
123140 }
124141
142+ /**
143+ * Parses the add command
144+ *
145+ * @param c command name
146+ * @param command whole command in string
147+ * @param commandWords command broken down into words
148+ * @return a command object ready to be executed
149+ * @throws SpikeException if any parameter is missing
150+ */
125151 private Command parseAdd (CommandName c , String command , String [] commandWords ) throws SpikeException {
126152 DateTimeFormatter dtf = DateTimeFormatter .ofPattern ("yyyy-MM-dd HHmm" );
127153 switch (c ) {
@@ -163,6 +189,14 @@ private Command parseAdd(CommandName c, String command, String[] commandWords) t
163189 }
164190 }
165191
192+ /**
193+ * Parses delete command.
194+ *
195+ * @param commandWords command broken down into words
196+ * @param tasks current task list
197+ * @return a command object ready to be executed
198+ * @throws SpikeException if any parameter is missing
199+ */
166200 private Command parseDelete (String [] commandWords , TaskList tasks ) throws SpikeException {
167201 if (commandWords .length != 2 || isInt (commandWords [1 ]) == -1
168202 || isInt (commandWords [1 ]) > tasks .getListSize ()) {
@@ -172,6 +206,15 @@ private Command parseDelete(String[] commandWords, TaskList tasks) throws SpikeE
172206 return new DeleteCommand (toDelete );
173207 }
174208
209+ /**
210+ * Parses mark or unmark command
211+ *
212+ * @param c command name
213+ * @param commandWords command broken down into words
214+ * @param tasks current task list
215+ * @return a command object ready to be executed
216+ * @throws SpikeException if any parameter is missing
217+ */
175218 private Command parseToggleMark (CommandName c , String [] commandWords , TaskList tasks ) throws SpikeException {
176219 if (c .getCommand ().equals ("mark" )) {
177220 if (commandWords .length != 2 || isInt (commandWords [1 ]) == -1
@@ -190,13 +233,20 @@ private Command parseToggleMark(CommandName c, String[] commandWords, TaskList t
190233 }
191234 }
192235
236+ /**
237+ * Parses exit command
238+ *
239+ * @return a command ready to be executed
240+ */
193241 private Command parseExit () {
194242 return new ExitCommand ();
195243 }
196244
197245
198246 /**
199247 * Parses date and time input by user and returns valid LocalDateTime object
248+ *
249+ * @return object containing date and time information
200250 */
201251 private static LocalDateTime parseDateTime (String s , DateTimeFormatter dtf ) {
202252 try {
@@ -210,6 +260,8 @@ private static LocalDateTime parseDateTime(String s, DateTimeFormatter dtf) {
210260 /**
211261 * Checks whether the input string is integer.
212262 * If yes, return it, else return -1.
263+ *
264+ * @return indicator of whether the input is an integer
213265 */
214266 private static int isInt (String str ) {
215267 try {
@@ -224,6 +276,8 @@ private static int isInt(String str) {
224276 /**
225277 * Checks whether it is a valid command.
226278 * If valid, return that command enum number, else return null.
279+ *
280+ * @return command name if the input is valid
227281 */
228282 private CommandName validateCommand (String input ) {
229283 for (CommandName c : CommandName .values ()) {
0 commit comments