Skip to content

Commit

Permalink
Fix Exception Handling for Deadline.java and Event.java
Browse files Browse the repository at this point in the history
  • Loading branch information
l5_z committed Mar 7, 2024
1 parent 9257bdd commit 3e1adc5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ public static String[] getDeadline(String input) {
String[] results = new String[Constant.DEADLINE_PARAMETERS];


if (!input.contains("/by")) {
throw new CustomException(Constant.UNSPECIFIED_PARAMETER);
}

int index = input.indexOf("/by");

String label = input.substring(0, index).trim();


String deadline = input.substring(index + Constant.DEADLINE_BY_OFFSET).trim();

if (deadline.isEmpty()) {
throw new CustomException(Constant.UNSPECIFIED_PARAMETER);
}

results[0] = label;
results[1] = deadline;

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static String[] getInterval(String input) {
String[] results = new String[Constant.EVENT_PARAMETERS];


if (!input.contains("/from") || !input.contains("/to")) {
throw new CustomException(Constant.UNSPECIFIED_PARAMETER);
}

int indexFrom = input.indexOf("/from");
int indexTo = input.indexOf("/to");

Expand All @@ -39,6 +43,10 @@ public static String[] getInterval(String input) {
String fromSubstring = input.substring(indexFrom + Constant.EVENT_FROM_OFFSET, indexTo).trim();
String toSubstring = input.substring(indexTo + Constant.EVENT_TO_OFFSET).trim();

if (fromSubstring.isEmpty() || toSubstring.isEmpty()) {
throw new CustomException(Constant.UNSPECIFIED_PARAMETER);
}

results[0] = label;
results[1] = fromSubstring;
results[2] = toSubstring;
Expand Down

0 comments on commit 3e1adc5

Please sign in to comment.