forked from nus-cs2103-AY2223S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
109 lines (90 loc) · 3.78 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
package meowmeow;
import meowmeow.commands.AddCommand;
import meowmeow.commands.Command;
import meowmeow.commands.DefaultCommand;
import meowmeow.commands.DeleteCommand;
import meowmeow.commands.ExitCommand;
import meowmeow.commands.FindCommand;
import meowmeow.commands.HiCommand;
import meowmeow.commands.ListCommand;
import meowmeow.commands.MarkCommand;
import meowmeow.commands.UndoCommand;
/**
* Class Parser is a class that parses the user's input and translates it into a Command.
*/
public class Parser {
public static Command parse(String userInput) {
userInput = userInput.trim();
String[] splitUI = userInput.split(" ");
assert splitUI.length > 0 : "splitUI should not be empty";
switch (splitUI[0]) {
case "hi":
return new HiCommand();
case "list":
return new ListCommand();
case "bye":
return new ExitCommand();
case "undo":
return new UndoCommand();
default:
break;
}
String[] splitUserInput = userInput.split(" ", 2);
String cmdWord = splitUserInput[0];
switch (cmdWord) {
case "mark":
try {
int taskNum = Integer.parseInt(splitUserInput[1]);
return new MarkCommand(true, taskNum);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= Meowmeow needs a number for the task you want to mark!");
}
case "unmark":
try {
int taskNum = Integer.parseInt(splitUserInput[1]);
return new MarkCommand(false, taskNum);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= Meowmeow needs a number for the task you want to unmark!");
}
case "todo":
try {
String taskName = splitUserInput[1];
return new AddCommand('T', taskName);
} catch (ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= Meowmeow needs a name for the todo you want to add!");
}
case "deadline":
try {
String nameAndLocalDateTime = splitUserInput[1];
return new AddCommand('D', nameAndLocalDateTime);
} catch (ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= To add a deadline type it in in this format: "
+ "deadline (name) /by (YYYY-MM-DDTHH:MM:SS)");
}
case "event":
try {
String nameAndTime = splitUserInput[1];
return new AddCommand('E', nameAndTime);
} catch (ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= To add an event type it in in this format: event (name) /at (time)");
}
case "delete":
try {
int taskToDelete = Integer.parseInt(splitUserInput[1]);
return new DeleteCommand(taskToDelete);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= Meowmeow needs a number for the task you want to delete!");
}
case "find":
try {
String taskToFind = splitUserInput[1];
return new FindCommand(taskToFind);
} catch (ArrayIndexOutOfBoundsException e) {
return new DefaultCommand("=0w0= Meowmeow needs a name for the task you want to find!");
}
default:
return new DefaultCommand("Sowwie meowmeow doesn't understand what you said uwu ♥ \n"
+ "Try typing something else! owo");
}
}
}