Skip to content

Commit 61ae4cc

Browse files
committed
Fix exceptions bug in isValidCommand() and clean up code
1 parent e810e76 commit 61ae4cc

File tree

4 files changed

+116
-100
lines changed

4 files changed

+116
-100
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ bin/
1616

1717
/text-ui-test/ACTUAL.TXT
1818
text-ui-test/EXPECTED-UNIX.TXT
19+
*.class

src/main/java/burger/Burger.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Burger {
88
static final String CHATBOT_NAME = "Burger";
99
static final String HORIZONTAL_LINE = "---------------------------------";
1010

11-
public static void main(String[] args) throws BurgerException {
11+
public static void main(String[] args) {
1212
welcomeMessage();
1313
Scanner input = new Scanner(System.in);
1414
List myList = new List();
@@ -22,7 +22,7 @@ public static void main(String[] args) throws BurgerException {
2222
isPolling = false;
2323
break;
2424
case "list":
25-
myList.printTodoList();
25+
myList.printTaskList();
2626
break;
2727
default:
2828
wakeUp();
@@ -43,18 +43,14 @@ private static void welcomeMessage() {
4343
public static void printLine() {
4444
System.out.println(HORIZONTAL_LINE);
4545
}
46+
4647
/**
47-
* Prints error message when user provides invalid input
48+
* Prints error message when user provides unknown input
4849
*/
4950
private static void wakeUp() {
50-
try {
51-
throw new BurgerException();
52-
} catch (BurgerException e) {
53-
printLine();
54-
System.out.println("Wake Up!!! Key in something that makes sense!");
55-
printLine();
56-
}
57-
51+
printLine();
52+
System.out.println("Wake Up!!! Key in something that makes sense!");
53+
printLine();
5854
}
5955

6056
public static void goodbye() {

src/main/java/burger/BurgerException.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,30 @@
33
import static burger.Burger.printLine;
44

55
public class BurgerException extends Exception{
6-
public BurgerException() {}
6+
public static void printInvalidDeadlineInputMessage() {
7+
printLine();
8+
System.out.println("Oi, the input is wrong!");
9+
System.out.println("Format: deadline {deadline task} /by {date}");
10+
printLine();
11+
}
12+
13+
public static void printInvalidEventInputMessage() {
14+
printLine();
15+
System.out.println("Oi, the input is wrong!");
16+
System.out.println("Format: event {event task} /from {date} /to {date}");
17+
printLine();
18+
}
19+
20+
public static void printEmptyDescription() {
21+
printLine();
22+
System.out.println("UwU~ Where is your description?");
23+
System.out.println("Format: {command} {description}");
24+
printLine();
25+
}
26+
27+
public static void printOutOfIndexMessage() {
28+
printLine();
29+
System.out.println("Tsk... Out of index, do you know how to count?");
30+
printLine();
31+
}
732
}

src/main/java/burger/list/List.java

Lines changed: 82 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package burger.list;
22

33
import java.util.ArrayList;
4-
import java.util.IllegalFormatException;
54

65
import burger.BurgerException;
76
import burger.task.Deadline;
@@ -10,46 +9,55 @@
109
import burger.task.Todo;
1110

1211
import static burger.Burger.printLine;
12+
import static burger.BurgerException.*;
1313

1414
public class List {
1515

16-
private final ArrayList<Task> todoList;
16+
private final ArrayList<Task> taskList;
1717

1818
public int totalTasks;
1919
public List() {
20-
todoList = new ArrayList<>();
20+
taskList = new ArrayList<>();
2121
totalTasks = 0;
2222
}
2323
/**
2424
* Returns boolean value base on whether the input matches one of the commands.
2525
*
26+
*
2627
* @param textArray user input in array type.
2728
* @return true if it's a valid command and false otherwise.
29+
* @throws ArrayIndexOutOfBoundsException when the description after the command is empty.
2830
*/
2931
public boolean isValidCommand(String[] textArray) {
3032
boolean isValid = true;
31-
String command = textArray[0];
32-
switch (command) { // assume inputs for commands are correct
33-
case "mark":
34-
// fallthrough
35-
case "unmark":
36-
int idx = Integer.parseInt(textArray[1]) - 1;
37-
getMark(idx, command);
38-
break;
39-
case "todo":
40-
addTodo(textArray);
41-
break;
42-
case "deadline":
43-
addDeadline(textArray);
44-
break;
45-
case "event":
46-
addEvent(textArray);
47-
break;
48-
default:
49-
isValid = false;
50-
break;
33+
try {
34+
String command = textArray[0];
35+
switch (command) {
36+
case "mark":
37+
// fallthrough
38+
case "unmark":
39+
int idx = Integer.parseInt(textArray[1]) - 1;
40+
getMark(idx, command);
41+
break;
42+
case "todo":
43+
addTodo(textArray);
44+
break;
45+
case "deadline":
46+
addDeadline(textArray);
47+
break;
48+
case "event":
49+
addEvent(textArray);
50+
break;
51+
default:
52+
isValid = false;
53+
break;
54+
}
55+
return isValid;
56+
} catch (ArrayIndexOutOfBoundsException e) {
57+
printEmptyDescription();
58+
return isValid;
5159
}
52-
return isValid;
60+
5361
}
5462

5563
/**
@@ -58,15 +66,12 @@ public boolean isValidCommand(String[] textArray) {
5866
*
5967
* @param idx index of task.
6068
* @return task.
61-
* @throws ArrayIndexOutOfBoundsException idx is out of bounds.
69+
* @throws IndexOutOfBoundsException idx is out of bounds.
6270
*/
6371
public Task getTask(int idx) {
6472
try {
65-
return this.todoList.get(idx);
66-
} catch (ArrayIndexOutOfBoundsException e) {
67-
printLine();
68-
System.out.println("UwU? Do you know how to count?");
69-
printLine();
73+
return this.taskList.get(idx);
74+
} catch (IndexOutOfBoundsException e) {
7075
return null;
7176
}
7277
}
@@ -76,68 +81,56 @@ public Task getTask(int idx) {
7681
* @param todo the todo input by the user.
7782
*/
7883
public void addTodo(String[] todo) {
79-
try {
80-
if (todo.length == 1) {
81-
throw new BurgerException();
82-
}
83-
StringBuilder todoText = new StringBuilder(todo[1]);
84-
for (int i = 2; i < todo.length; i++) {
85-
todoText.append(' ').append(todo[i]);
86-
}
87-
Task newTodo = new Todo(todoText.toString());
88-
addTask(newTodo);
89-
} catch (BurgerException e) {
90-
printEmptyDescription();
84+
StringBuilder todoText = new StringBuilder(todo[1]);
85+
for (int i = 2; i < todo.length; i++) {
86+
todoText.append(' ').append(todo[i]);
9187
}
88+
Task newTodo = new Todo(todoText.toString());
89+
addTask(newTodo);
9290
}
9391

9492
/**
9593
* Adds a new event to the list.
9694
*
9795
* @param event the event input by the user.
98-
* @throws ArrayIndexOutOfBoundsException if text is empty after the "event" command.
99-
* @throws IllegalFormatException if event format is not followed.
96+
* @throws ArrayIndexOutOfBoundsException if deadline format is not followed.
10097
*/
10198
public void addEvent(String[] event) {
10299
try {
103-
if (event.length == 1) {
104-
throw new BurgerException();
105-
}
106100
StringBuilder eventText = new StringBuilder(event[1]);
107101
int i = 2;
108-
while (!event[i].startsWith("/")) {
102+
while (!event[i].startsWith("/from")) {
109103
eventText.append(' ').append(event[i]);
110104
i++;
111105
}
112106
eventText.append(' ').append("(from: ");
113107
i++;
114-
while (!event[i].startsWith("/")) {
115-
eventText.append(' ').append(event[i]);
108+
while (!event[i].startsWith("/to")) {
109+
eventText.append(event[i]).append(' ');
116110
i++;
117111
}
112+
eventText.append("to: ");
118113
i++;
119-
eventText.append(' ').append("to: ").append(event[i]).append(')');
114+
while (i < event.length - 1) {
115+
eventText.append(event[i]).append(' ');
116+
i++;
117+
}
118+
eventText.append(event[i]).append(')');
120119
Task newEvent = new Event(eventText.toString());
121120
addTask(newEvent);
122121
} catch (ArrayIndexOutOfBoundsException e) {
123-
printInvalidInputMessage();
124-
} catch (BurgerException e) {
125-
printEmptyDescription();
122+
printInvalidEventInputMessage();
126123
}
127124
}
128125

129126
/**
130127
* Adds a new deadline to the list.
131128
*
132129
* @param deadline the deadline input by the user.
133-
* @throws ArrayIndexOutOfBoundsException if text is empty after the "deadline" command.
134-
* @throws IllegalFormatException if deadline format is not followed.
130+
* @throws ArrayIndexOutOfBoundsException if deadline format is not followed.
135131
*/
136132
public void addDeadline(String[] deadline) {
137133
try {
138-
if (deadline.length == 1) {
139-
throw new BurgerException();
140-
}
141134
StringBuilder deadlineText = new StringBuilder(deadline[1]);
142135
int i = 2;
143136
while (!deadline[i].startsWith("/")) {
@@ -154,34 +147,20 @@ public void addDeadline(String[] deadline) {
154147
Task newDeadline = new Deadline(deadlineText.toString());
155148
addTask(newDeadline);
156149
} catch (ArrayIndexOutOfBoundsException e) {
157-
printInvalidInputMessage();
158-
} catch (BurgerException e) {
159-
printEmptyDescription();
150+
printInvalidDeadlineInputMessage();
160151
}
161152
}
162153

163-
private static void printInvalidInputMessage() {
164-
printLine();
165-
System.out.println("Oi, the input is wrong!");
166-
printLine();
167-
}
168-
169-
private void printEmptyDescription() {
170-
printLine();
171-
System.out.println("UwU~ Where is your description?");
172-
printLine();
173-
}
174-
175154
/**
176155
* Adds task to the list and prints out a message.
177156
*
178157
* @param task Task that is being added.
179158
*/
180159
public void addTask(Task task) {
181-
this.todoList.add(task);
160+
this.taskList.add(task);
182161
printLine();
183162
System.out.println("Got it. I've added this task:");
184-
this.todoList.get(totalTasks).printTask();
163+
this.taskList.get(totalTasks).printTask();
185164
this.totalTasks++;
186165
System.out.println();
187166
System.out.println("Now you have " + totalTasks + " tasks in the list.");
@@ -192,36 +171,51 @@ public void addTask(Task task) {
192171
*
193172
* @param idx the index of the task.
194173
* @param command Command to "mark" or "unmark" the task.
174+
* @throws BurgerException if index input is out of the range of list of tasks.
195175
*/
196176
public void getMark(int idx, String command) {
197-
Task currTask = getTask(idx);
198-
if (command.equals("mark")) {
199-
System.out.println("Nice! I've marked this task as done:");
200-
currTask.markDone();
201-
} else {
202-
System.out.println("OK, I've marked this task as not done yet:");
203-
currTask.unmarkDone();
177+
try {
178+
Task currTask = getTask(idx);
179+
if (currTask == null) {
180+
throw new BurgerException();
181+
}
182+
if (command.equals("mark")) {
183+
System.out.println("Nice! I've marked this task as done:");
184+
currTask.markDone();
185+
} else {
186+
System.out.println("OK, I've marked this task as not done yet:");
187+
currTask.unmarkDone();
188+
}
204189
printTaskWithLine(idx);
190+
} catch (BurgerException e) {
191+
printOutOfIndexMessage();
205192
}
206-
printTaskWithLine(idx);
193+
207194
}
195+
208196
/**
209197
* Prints the task with a horizontal line.
210198
*
211199
* @param idx Index of the task.
212200
*/
213201
public void printTaskWithLine(int idx) {
214-
this.todoList.get(idx).printTask();
202+
this.taskList.get(idx).printTask();
215203
System.out.println();
216204
printLine();
217205
}
218206

219-
public void printTodoList() {
207+
/**
208+
* Prints task list.
209+
*/
210+
public void printTaskList() {
220211
printLine();
221212
System.out.println("Here are the tasks in your list:");
222-
for (int i = 0; i < todoList.size(); i++) {
213+
if (taskList.isEmpty()) {
214+
System.out.println("Don't be lazy... start doing something");
215+
}
216+
for (int i = 0; i < taskList.size(); i++) {
223217
System.out.print(i+1 + ". ");
224-
todoList.get(i).printTask();
218+
taskList.get(i).printTask();
225219
System.out.println();
226220
}
227221
printLine();

0 commit comments

Comments
 (0)