Skip to content

Commit 4cb7ea9

Browse files
Implement more error handling
1 parent de1d1a1 commit 4cb7ea9

File tree

9 files changed

+42
-29
lines changed

9 files changed

+42
-29
lines changed

src/main/java/helperbot/storage/data/HelperBot.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
T,0,read book
2-
D,0,return book,2025-10-30,21:30
2+
D,0,return book,2025-10-21,
33
D,0,ca1,2025-09-12,
44
E,0,ca7,2025-10-11,21:30,2025-10-17,21:30
55
E,0,ca2,2025-09-12,,2025-10-12,

src/main/java/helperbot/task/Deadline.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class Deadline extends Task {
2222
* @param byDate the date of the deadline
2323
* @param byTime the time of the deadline
2424
*/
25-
public Deadline(String description, LocalDate byDate, LocalTime byTime) {
25+
public Deadline(String description, LocalDate byDate, LocalTime byTime) throws HelperBotArgumentException {
2626
super(description);
2727
this.byDate = byDate;
2828
this.byTime = byTime;
@@ -51,7 +51,8 @@ public static Deadline fromUserInput(String message) throws HelperBotArgumentExc
5151
} catch (IndexOutOfBoundsException e) {
5252
throw new HelperBotArgumentException("Wrong format for Deadline");
5353
} catch (DateTimeParseException e) {
54-
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /by");
54+
throw new HelperBotArgumentException("Invalid date or time. "
55+
+ "Please enter date and time in YYYY-MM-DD hh:mm after /by");
5556
}
5657
}
5758

@@ -128,12 +129,16 @@ private static Deadline getDeadline(String[] message) throws HelperBotFileExcept
128129
if (message.length == 5) {
129130
byTime = LocalTime.parse(message[4]);
130131
}
131-
Deadline deadline = new Deadline(message[2], byDate, byTime);
132-
if (message[1].equals("1")) {
133-
deadline.markAsDone();
134-
} else if (!message[1].equals("0")) {
135-
throw new HelperBotFileException("Invalid status " + message[0] + " for Task");
132+
try {
133+
Deadline deadline = new Deadline(message[2], byDate, byTime);
134+
if (message[1].equals("1")) {
135+
deadline.markAsDone();
136+
} else if (!message[1].equals("0")) {
137+
throw new HelperBotFileException("Invalid status " + message[0] + " for Task");
138+
}
139+
return deadline;
140+
} catch (HelperBotArgumentException e) {
141+
throw new HelperBotFileException("Empty description.");
136142
}
137-
return deadline;
138143
}
139144
}

src/main/java/helperbot/task/Event.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public class Event extends Task {
2626
* @param toDate The end date of the event.
2727
* @param toTime The end time of the event.
2828
*/
29-
public Event(String description, LocalDate fromDate, LocalTime fromTime, LocalDate toDate, LocalTime toTime) {
29+
public Event(String description, LocalDate fromDate, LocalTime fromTime, LocalDate toDate,
30+
LocalTime toTime) throws HelperBotArgumentException {
3031
super(description);
3132
this.fromDate = fromDate;
3233
this.fromTime = fromTime;
@@ -67,7 +68,8 @@ public static Event fromUserInput(String message) throws HelperBotArgumentExcept
6768
} catch (IndexOutOfBoundsException e) {
6869
throw new HelperBotArgumentException("Wrong format for Event");
6970
} catch (DateTimeParseException e) {
70-
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /from and /to");
71+
throw new HelperBotArgumentException("Invalid date or time. "
72+
+ "Please enter date and time in YYYY-MM-DD hh:mm after /from and /to");
7173
}
7274
}
7375

@@ -165,13 +167,17 @@ private static Event getEvent(String[] message) throws HelperBotFileException {
165167
if (Event.isToBeforeFrom(fromDate, fromTime, toDate, toTime)) {
166168
throw new HelperBotFileException("From datetime must be before to datetime");
167169
}
168-
Event event = new Event(message[2], fromDate, fromTime, toDate, toTime);
169-
if (message[1].equals("1")) {
170-
event.markAsDone();
171-
} else if (!message[1].equals("0")) {
172-
throw new HelperBotFileException("Invalid status " + message[0] + " for Task");
170+
try {
171+
Event event = new Event(message[2], fromDate, fromTime, toDate, toTime);
172+
if (message[1].equals("1")) {
173+
event.markAsDone();
174+
} else if (!message[1].equals("0")) {
175+
throw new HelperBotFileException("Invalid status " + message[0] + " for Task");
176+
}
177+
return event;
178+
} catch (HelperBotArgumentException e) {
179+
throw new HelperBotFileException("Empty description.");
173180
}
174-
return event;
175181
}
176182

177183
private static boolean isToBeforeFrom(LocalDate fromDate, LocalTime fromTime,

src/main/java/helperbot/task/Task.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public class Task {
1616
* Generates a <code>Task</code>.
1717
* @param description The description of the task.
1818
*/
19-
public Task(String description) {
19+
public Task(String description) throws HelperBotArgumentException {
20+
if (description.isEmpty()) {
21+
throw new HelperBotArgumentException("Empty description.");
22+
}
2023
this.description = description;
2124
this.isDone = false;
2225
}

src/main/java/helperbot/task/ToDo.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ToDo extends Task {
1212
* Generates a <code>ToDo</code>
1313
* @param description the name of the task.
1414
*/
15-
public ToDo(String description) {
15+
public ToDo(String description) throws HelperBotArgumentException {
1616
super(description);
1717
}
1818

@@ -25,9 +25,6 @@ public ToDo(String description) {
2525
public static ToDo fromUserInput(String message) throws HelperBotArgumentException {
2626
try {
2727
String detail = message.substring(5).trim();
28-
if (detail.isEmpty()) {
29-
throw new HelperBotArgumentException("Empty description.");
30-
}
3128
return new ToDo(detail);
3229
} catch (IndexOutOfBoundsException e) {
3330
throw new HelperBotArgumentException("Wrong format for ToDo.");
@@ -51,6 +48,8 @@ public static ToDo of(String[] message) throws HelperBotFileException {
5148
return toDo;
5249
} catch (IndexOutOfBoundsException e) {
5350
throw new HelperBotFileException("Incomplete data for Task.");
51+
} catch (HelperBotArgumentException e) {
52+
throw new HelperBotFileException("Empty description.");
5453
}
5554
}
5655

src/test/java/helperbot/task/DeadlineTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ void of_validDataWithoutTime_success() throws HelperBotFileException {
8080
}
8181

8282
@Test
83-
void toSaveFormat_withoutByTime_success() {
83+
void toSaveFormat_withoutByTime_success() throws HelperBotArgumentException {
8484
Deadline deadline = new Deadline("deadline1", LocalDate.parse("2025-09-12"), null);
8585
assertEquals("D,0,deadline1,2025-09-12,", deadline.toSavaFormat());
8686
}
8787

8888
@Test
89-
void toSaveFormat_havaByTime_success() {
89+
void toSaveFormat_havaByTime_success() throws HelperBotArgumentException {
9090
Deadline deadline = new Deadline("deadline1", LocalDate.parse("2025-09-12"),
9191
LocalTime.parse("21:30"));
9292
assertEquals("D,0,deadline1,2025-09-12,21:30", deadline.toSavaFormat());

src/test/java/helperbot/task/EventTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ void of_fromAfterTo_exceptionThrown() {
117117
}
118118

119119
@Test
120-
void toSaveFormat_eventWithTime_correctFormat() {
120+
void toSaveFormat_eventWithTime_correctFormat() throws HelperBotArgumentException {
121121
Event event = new Event("go to the gym", LocalDate.of(2025, 11, 10),
122122
LocalTime.of(17, 30),
123123
LocalDate.of(2025, 11, 10), LocalTime.of(19, 0));
124124
assertEquals("E,0,go to the gym,2025-11-10,17:30,2025-11-10,19:00", event.toSavaFormat());
125125
}
126126

127127
@Test
128-
void toSaveFormat_eventWithoutTime_correctFormat() {
128+
void toSaveFormat_eventWithoutTime_correctFormat() throws HelperBotArgumentException {
129129
Event event = new Event("take a trip", LocalDate.of(2025, 12, 20),
130130
null,
131131
LocalDate.of(2025, 12, 25), null);

src/test/java/helperbot/task/ToDoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void of_incompleteData_exceptionThrown() {
6262
}
6363

6464
@Test
65-
void toSaveFormat_validTask_correctFormat() {
65+
void toSaveFormat_validTask_correctFormat() throws HelperBotArgumentException {
6666
ToDo toDo = new ToDo("take out the trash");
6767
assertEquals("T,0,take out the trash", toDo.toSavaFormat());
6868
}

src/test/java/helperbot/ui/ResponseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void getGreetMessage_validOutput_success() {
3232
}
3333

3434
@Test
35-
void getMarkCommandResponse_validTask_correctOutput() {
35+
void getMarkCommandResponse_validTask_correctOutput() throws HelperBotArgumentException {
3636
Task mockTask = new Task("test task") {
3737
@Override
3838
public String toString() {
@@ -46,7 +46,7 @@ public String toString() {
4646
}
4747

4848
@Test
49-
void getUnmarkCommandResponse_validTask_correctOutput() {
49+
void getUnmarkCommandResponse_validTask_correctOutput() throws HelperBotArgumentException {
5050
Task mockTask = new Task("another test task") {
5151
@Override
5252
public String toString() {

0 commit comments

Comments
 (0)