-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDuke.java
More file actions
186 lines (164 loc) · 6.91 KB
/
Duke.java
File metadata and controls
186 lines (164 loc) · 6.91 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
181
182
183
184
185
186
package duke;
import java.util.Scanner;
public class Duke {
public static void main(String[] args) {
// Welcome Message
printWelcomeMessage();
// Active Chat
activeChat();
// Goodbye Message
printGoodbyeMessage();
}
private static void activeChat() {
boolean isBye = false;
String input;
Scanner in = new Scanner(System.in);
int counter = 0;
Task[] list = new Task[100];
while(!isBye){
//store input
input = in.nextLine();
//process input
if (input.equals("bye")){ //check if bye
isBye = true;
} else if (input.equals("list")) { //check if list
processList(counter, list);
} else if (input.contains("done") ) { //check if done
processDone(input, list);
} else {
try {
counter = processTasks(input, counter, list); //process tasks
} catch ( IllegalTaskException e ) {
System.out.println("Please include /by for deadline and /at for event");
}
}
}
}
private static void processList(int counter, Task[] list) {
if (list[0] != null){
printListMessage(counter, list);
} else {
printListButEmptyMessage();
}
}
private static void processDone(String input, Task[] list) {
int donePos = input.indexOf("done");
if (list[0] != null) {
if (input.length() < donePos + 5) {
printDoneButNotSpecificMessage(); //when input contains done but no number
} else {
processValidDone(input, list, donePos); //when input contains done and specified number
}
} else {
printDoneButEmptyMessage(); //when input contains done but list is empty
}
}
private static void processValidDone(String input, Task[] list, int donePos) {
String itemNumDone = input.substring(donePos + 5, donePos + 6);
int itemNum = Integer.parseInt(itemNumDone);
list[itemNum - 1].setDone();
printDoneMessage(list, itemNum);
}
private static int processTasks(String input, int counter, Task[] list) throws IllegalTaskException{
if ( ( input.contains("deadline") || input.contains("event") ) && !input.contains("/")){
throw new IllegalTaskException();
}
if (input.contains("todo")) {
String description = input.substring(5);
ToDo newTask = new ToDo(description);
list[counter] = newTask;
counter += 1;
printAddedTaskMessage(newTask, counter);
} else if (input.contains("deadline")) {
int donePos = input.indexOf("/"); //finds pos of '/'
String description = input.substring(9,donePos);
if (!input.substring(donePos + 1, donePos + 3).equals("by")) {
throw new IllegalTaskException();
}
String date = input.substring(donePos + 4);
Deadline newTask = new Deadline(description,date);
list[counter] = newTask;
counter += 1;
printAddedTaskMessage(newTask, counter);
} else if (input.contains("event")) {
int donePos = input.indexOf("/"); //finds pos of '/'
String description = input.substring(6,donePos);
if (!input.substring(donePos + 1, donePos + 3).equals("by")) {
throw new IllegalTaskException();
}
String date = input.substring(donePos + 4);
Event newTask = new Event(description,date);
list[counter] = newTask;
counter += 1;
printAddedTaskMessage(newTask, counter);
} else {
System.out.println("Please specify tasks: todo, deadline or event");
System.out.println("Example - type in the following: todo read book");
}
return counter;
}
private static void printDoneMessage(Task[] list, int itemNum) {
System.out.println("--------------------");
System.out.println("Nice! I've marked this task as done:");
System.out.println( itemNum + ".[" + list[itemNum - 1].getStatusIcon() + "] " + list[itemNum - 1].getDescription() );
System.out.println("--------------------");
}
private static void printListMessage(int counter, Task[] list) {
System.out.println("--------------------");
System.out.println("Here are the tasks in your list:");
for(int i = 0; i < counter; i += 1){
printListOfTaskSubMessage(list[i], i);
}
System.out.println("--------------------");
}
private static void printListButEmptyMessage() {
System.out.println("--------------------");
System.out.println("List is empty. Time to get productive!");
System.out.println("--------------------");
}
private static void printDoneButNotSpecificMessage() {
System.out.println("Please specify which task is done.");
}
private static void printDoneButEmptyMessage() {
System.out.println("--------------------");
System.out.println("Unable to tick off list.");
System.out.println("List is empty. Time to get productive!");
System.out.println("--------------------");
}
private static void printAddedTaskMessage(Task task, int i) {
System.out.println("--------------------");
System.out.println("Got it. I've added this task: ");
System.out.println("[" + task.getLetter() + "] "
+ "[" + task.getStatusIcon() + "] "
+ task.getDescription()
+ task.getDate() );
System.out.println("Now you have " + i + " tasks in the list.");
System.out.println("--------------------");
}
private static void printListOfTaskSubMessage(Task task, int i) {
System.out.println(i + 1
+ ".[" + task.getLetter() + "] "
+ "[" + task.getStatusIcon() + "] "
+ task.getDescription()
+ task.getDate() );
}
private static void printGoodbyeMessage() {
System.out.println("--------------------");
System.out.println("Bye. Hope to see you again soon!");
System.out.println(" ");
System.out.println("--------------------");
}
private static void printWelcomeMessage() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("--------------------");
System.out.println("Hello! I'm Duke");
System.out.println("What can I do for you?");
System.out.println(" ");
System.out.println("--------------------");
}
}