Skip to content

Commit 0b979ea

Browse files
committed
Fix bug where user can enter a negative duration for DurationTask
1 parent e042cbc commit 0b979ea

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

src/main/java/popo/parser/Parser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static popo.utils.Messages.MESSAGE_EMPTY_DESCRIPTION;
44
import static popo.utils.Messages.MESSAGE_ENTER_COMMAND;
5+
import static popo.utils.Messages.MESSAGE_ENTER_POSITIVE_DURATION;
56
import static popo.utils.Messages.MESSAGE_FOLLOW_USAGE;
67
import static popo.utils.Messages.MESSAGE_INDICATE_TASK;
78
import static popo.utils.Messages.MESSAGE_INVALID_COMMAND;
@@ -211,6 +212,9 @@ private Command parseArgumentsForDuration(String arguments) throws NoDescription
211212
String durationTaskName = durationInputArr[0].strip();
212213
String durationString = durationInputArr[1].strip();
213214
long amount = Long.parseLong(durationString);
215+
if (amount < 0) {
216+
throw new InvalidDescriptionException(MESSAGE_ENTER_POSITIVE_DURATION);
217+
}
214218
Duration duration = Duration.of(amount, unit);
215219
return new DurationCommand(durationTaskName, duration);
216220
} catch (NumberFormatException ex) {

src/main/java/popo/utils/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class Messages {
77
// Messages to show to user upon a parse error.
88
public static final String MESSAGE_EMPTY_DESCRIPTION = "The description of a task cannot be empty.";
99
public static final String MESSAGE_ENTER_COMMAND = "Please enter a command.";
10+
public static final String MESSAGE_ENTER_POSITIVE_DURATION = "Please enter a positive duration!";
1011
public static final String MESSAGE_FOLLOW_USAGE = "Please follow the usage as shown below:";
1112
public static final String MESSAGE_INDICATE_TASK = "Please indicate a task number.";
1213
public static final String MESSAGE_INVALID_COMMAND = "OOPS!!! I'm sorry, but I don't know what that means :-(";

0 commit comments

Comments
 (0)