-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
180 lines (162 loc) · 6.42 KB
/
Duke.java
File metadata and controls
180 lines (162 loc) · 6.42 KB
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
import java.util.Scanner;
public class Duke {
public static final String DIVIDER = "___________________________________________________________";
public static final String INDENTATION = " ";
public static final String LOGO = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
public static final String HELLO_MESSAGE_2 = "Hello! I'm Duke, your friendly neighbourhood task manager";
public static final String HELLO_MESSAGE_3 = "What can I do for you? :D";
public static final String TASK_COMPLETED_MESSAGE = "You've completed the task! Well done!";
public static final String ADDED_TO_LIST = "I've added this to your list :D";
public static final String DEADLINE_DESCRIPTION_AND_DATE_SPLITTER = " /by ";
public static final String EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER = " /at ";
public static final String GOODBYE_MESSAGE = "Bye, hope to see you again soon! :)";
public static final String TYPE_SUITABLE_COMMAND_MESSAGE = "Sorry, you are using me wrongly. Please type in a suitable command :)";
public static final boolean IS_FINE = true;
public static final int TODO_STARTING_INDEX = 5;
public static final int DEADLINE_STARTING_INDEX = 9;
public static final int EVENT_STARTING_INDEX = 6;
public static Task[] tasks = new Task[100];
public static void main(String[] args) {
printStartingMessage();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while (!input.equals("bye")) {
processInput(input);
input = in.nextLine();
}
printGoodbyeMessage();
}
public static void printIndentationAndDivider() {
System.out.print(INDENTATION);
System.out.println(DIVIDER);
}
public static void printWordsWithIndentation(String words) {
System.out.print(INDENTATION);
System.out.println(words);
}
private static void printStartingMessage() {
System.out.println("Hello from\n" + LOGO);
printIndentationAndDivider();
printWordsWithIndentation(HELLO_MESSAGE_2);
printWordsWithIndentation(HELLO_MESSAGE_3);
printIndentationAndDivider();
System.out.println();
}
private static void printGoodbyeMessage() {
printIndentationAndDivider();
printWordsWithIndentation(GOODBYE_MESSAGE);
printIndentationAndDivider();
}
private static void processInput(String input) {
switch(input.split(" ")[0].toLowerCase()) {
case "list" :
executeListCase();
break;
case "done" :
executeDoneCase(input);
break;
case "todo" :
executeTaskCase(input, TODO_STARTING_INDEX,TaskType.TODO);
break;
case "deadline" :
executeTaskCase(input, DEADLINE_STARTING_INDEX,TaskType.DEADLINE);
break;
case "event" :
executeTaskCase(input, EVENT_STARTING_INDEX,TaskType.EVENT);
break;
default :
printIncorrectInputMessage();
}
}
private static void printTaskMessage() {
printIndentationAndDivider();
printWordsWithIndentation(ADDED_TO_LIST);
printWordsWithIndentation(tasks[Task.getTotalTasks() - 1].getStatusIconAndDescription());
printWordsWithIndentation(notifyNumberOfTasks());
printIndentationAndDivider();
System.out.println();
}
private static void printIncorrectInputMessage() {
printIndentationAndDivider();
printWordsWithIndentation(TYPE_SUITABLE_COMMAND_MESSAGE);
printIndentationAndDivider();
System.out.println();
}
private static void executeTaskCase(String input, int starting_index, TaskType type) {
String description = input.strip().substring(starting_index);
boolean isFine = addTask(description, type);
if(isFine) {
printTaskMessage();
}else {
printIncorrectInputMessage();
}
}
private static String[] parseInputForDifferentTask(String input,TaskType type) {
String[] parsedOutput = {};
switch(type) {
case TODO:
parsedOutput = new String[]{input};
break;
case DEADLINE:
parsedOutput = input.split(DEADLINE_DESCRIPTION_AND_DATE_SPLITTER);
break;
case EVENT:
parsedOutput = input.split(EVENT_DESCRIPTION_AND_DATE_TIME_SPLITTER);
break;
}
return parsedOutput;
}
private static boolean addTask(String input,TaskType type) {
String[] parsedOutput = parseInputForDifferentTask(input,type);
switch(type){
case TODO:
tasks[Task.getTotalTasks()] = new ToDo(parsedOutput[0]);
break;
case EVENT:
if(parsedOutput.length < 2) {
return !IS_FINE;
}
tasks[Task.getTotalTasks()] = new Event(parsedOutput[0], parsedOutput[1]);
break;
case DEADLINE:
if(parsedOutput.length < 2) {
return !IS_FINE;
}
tasks[Task.getTotalTasks()] = new Deadline(parsedOutput[0], parsedOutput[1]);
break;
}
Task.setTotalTasks(Task.getTotalTasks() + 1);
return IS_FINE;
}
private static String notifyNumberOfTasks() {
return "Now you have " + Task.getTotalTasks() + " task(s) in the list";
}
private static void executeDoneCase(String input) {
if(input.length() != 6) {
printIncorrectInputMessage();
return;
}
int index = Integer.parseInt(input.split(" ")[1]) - 1;
tasks[index].markAsDone();
printTaskCompletedMessage(tasks[index]);
}
private static void printTaskCompletedMessage(Task task) {
printIndentationAndDivider();
printWordsWithIndentation(TASK_COMPLETED_MESSAGE);
printWordsWithIndentation(task.getStatusIconAndDescription());
printIndentationAndDivider();
System.out.println();
}
private static void executeListCase() {
printIndentationAndDivider();
for (int i = 0; i < Task.getTotalTasks(); i++) {
printWordsWithIndentation(i + 1 + "." + tasks[i].getStatusIconAndDescription());
}
printIndentationAndDivider();
System.out.println();
}
}