Skip to content

Commit 86c23ef

Browse files
committed
add exceptions to handle index out of bound error
1 parent 8882719 commit 86c23ef

File tree

6 files changed

+33
-12
lines changed

6 files changed

+33
-12
lines changed

src/main/java/Duke.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
//package java;
22

3+
import exceptions.*;
34
import parser.Command;
45
import parser.Parser;
56

6-
import exceptions.IllegalDateTimeFormatException;
7-
import exceptions.InvalidStorageFilePathException;
8-
import exceptions.NoCommandException;
9-
import exceptions.StorageOperationException;
10-
import exceptions.NoDescriptionException;
11-
127
import model.TaskList;
138

149
import storage.Storage;
@@ -66,7 +61,10 @@ public String getResponse(String input) throws RuntimeException{
6661
assert commandResult.length() > 0: "The response message is empty";
6762

6863
return commandResult;
69-
} catch (NoDescriptionException | NoCommandException | IllegalDateTimeFormatException e) {
64+
} catch (NoDescriptionException
65+
| NoCommandException
66+
| IllegalDateTimeFormatException
67+
| IllegalPositionException e) {
7068
return e.getMessage();
7169
} catch (IOException e) {
7270
throw new RuntimeException(e);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package exceptions;
2+
3+
public class IllegalPositionException extends Exception {
4+
public IllegalPositionException(String cause) { super(cause); }
5+
}

src/main/java/model/TaskList.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package model;
22

3+
import exceptions.IllegalPositionException;
4+
35
import java.time.LocalDate;
46

57
import java.util.ArrayList;
@@ -65,9 +67,15 @@ public String add(Task task) {
6567
* @param position A position within the bound of the list.
6668
* @return response of the remove action.
6769
*/
68-
public String remove(int position) {
70+
public String remove(int position) throws IllegalPositionException {
6971
assert this.internalList.size() > 0: "task list is empty, cannot delete";
7072

73+
74+
if (position < 0 || position >= internalList.size()) {
75+
throw new IllegalPositionException("Oops!!! The input position "
76+
+ Integer.toString(position)
77+
+ " is out of the boundary!\n");
78+
}
7179
Task deletedTask = internalList.get(position);
7280
internalList.remove(position);
7381
return ECHO_DELETE_TASK
@@ -84,9 +92,14 @@ public String remove(int position) {
8492
* @param position A position within the bound of the list.
8593
* @return response of the mark action.
8694
*/
87-
public String markTaskAsDone(Integer position) {
95+
public String markTaskAsDone(int position) throws IllegalPositionException {
8896
assert this.internalList.size() > 0: "task list is empty, cannot mark";
8997

98+
if (position < 0 || position >= internalList.size()) {
99+
throw new IllegalPositionException("Oops!!! The input position "
100+
+ Integer.toString(position)
101+
+ " is out of the boundary!\n");
102+
}
90103
Task finishedTask = this.internalList.get(position);
91104
finishedTask.markAsDone();
92105
return ECHO_COMPLETE_TASK

src/main/java/parser/Command.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package parser;
22

33
import exceptions.IllegalDateTimeFormatException;
4+
import exceptions.IllegalPositionException;
45
import exceptions.NoDescriptionException;
56
import model.TaskList;
67

@@ -113,5 +114,5 @@ public String findKeyword(Pattern pattern, String input) {
113114
return matcher.group(POSITION_KEYWORD);
114115
}
115116

116-
public abstract String execute() throws NoDescriptionException;
117+
public abstract String execute() throws NoDescriptionException, IllegalPositionException;
117118
}

src/main/java/parser/DeleteCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package parser;
22

3+
import exceptions.IllegalPositionException;
4+
35
import static parser.Parser.DELETE_PATTERN;
46

57
/**
@@ -21,7 +23,7 @@ public class DeleteCommand extends Command {
2123
* @return response from the TaskList class as a string.
2224
*/
2325
@Override
24-
public String execute() {
26+
public String execute() throws IllegalPositionException {
2527
return this.taskList.remove(this.position);
2628
}
2729
}

src/main/java/parser/FinishCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package parser;
22

3+
import exceptions.IllegalPositionException;
4+
35
import static parser.Parser.FINISH_PATTERN;
46

57
/**
@@ -20,7 +22,7 @@ public class FinishCommand extends Command {
2022
* Mark a task at the inputted position as done.
2123
* @return response from the TaskList class as a string.
2224
*/
23-
public String execute() {
25+
public String execute() throws IllegalPositionException {
2426
return this.taskList.markTaskAsDone(position);
2527
}
2628
}

0 commit comments

Comments
 (0)