From 3fac1dc4c8b71bda5e4e8200c3d72ac03040cac4 Mon Sep 17 00:00:00 2001 From: yk Wei Date: Wed, 3 Nov 2021 23:50:36 +0800 Subject: [PATCH 1/7] Fix error message to fit desired usage for indexing of integers larger than max int --- src/main/java/seedu/address/logic/Logic.java | 3 +- .../seedu/address/logic/LogicManager.java | 4 +- .../logic/parser/AddLessonCommandParser.java | 4 ++ .../parser/ClearRemarkCommandParser.java | 4 ++ .../logic/parser/DeleteCommandParser.java | 4 ++ .../parser/DeleteLessonCommandParser.java | 15 ++++- .../logic/parser/EditCommandParser.java | 4 ++ .../logic/parser/GetCommandParser.java | 4 ++ .../seedu/address/logic/parser/Parser.java | 3 +- .../address/logic/parser/ParserUtil.java | 58 +++++++++++++++++-- .../logic/parser/PaymentCommandParser.java | 4 ++ .../logic/parser/RemarkCommandParser.java | 4 ++ .../address/logic/parser/TrackOParser.java | 3 +- .../exceptions/IndexOutOfBoundsException.java | 15 +++++ .../java/seedu/address/ui/MainWindow.java | 7 +++ .../seedu/address/logic/LogicManagerTest.java | 9 ++- .../logic/parser/CommandParserTestUtil.java | 5 ++ .../parser/PaymentCommandParserTest.java | 3 +- 18 files changed, 140 insertions(+), 13 deletions(-) create mode 100644 src/main/java/seedu/address/logic/parser/exceptions/IndexOutOfBoundsException.java diff --git a/src/main/java/seedu/address/logic/Logic.java b/src/main/java/seedu/address/logic/Logic.java index c8790def6f8..aa2a5580ba7 100644 --- a/src/main/java/seedu/address/logic/Logic.java +++ b/src/main/java/seedu/address/logic/Logic.java @@ -6,6 +6,7 @@ import seedu.address.commons.core.GuiSettings; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.ReadOnlyTrackO; import seedu.address.model.tutee.Tutee; @@ -21,7 +22,7 @@ public interface Logic { * @throws CommandException If an error occurs during command execution. * @throws ParseException If an error occurs during parsing. */ - CommandResult execute(String commandText) throws CommandException, ParseException; + CommandResult execute(String commandText) throws CommandException, ParseException, IndexOutOfBoundsException; /** * Returns Track-O. diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index 5510b9ea21b..7abd859428a 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -11,6 +11,7 @@ import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.logic.parser.TrackOParser; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.Model; import seedu.address.model.ReadOnlyTrackO; @@ -38,7 +39,8 @@ public LogicManager(Model model, Storage storage) { } @Override - public CommandResult execute(String commandText) throws CommandException, ParseException { + public CommandResult execute(String commandText) throws CommandException, ParseException, + IndexOutOfBoundsException { logger.info("----------------[USER COMMAND][" + commandText + "]"); CommandResult commandResult; diff --git a/src/main/java/seedu/address/logic/parser/AddLessonCommandParser.java b/src/main/java/seedu/address/logic/parser/AddLessonCommandParser.java index f7210e952a4..e3ed1fbb41d 100644 --- a/src/main/java/seedu/address/logic/parser/AddLessonCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddLessonCommandParser.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_DAY_OF_WEEK; import static seedu.address.logic.parser.CliSyntax.PREFIX_END_TIME; import static seedu.address.logic.parser.CliSyntax.PREFIX_HOURLY_RATE; @@ -14,6 +15,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.AddLessonCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.lesson.Subject; @@ -39,6 +41,8 @@ public AddLessonCommand parse(String args) throws ParseException { index = ParserUtil.parseIndex(argMultimap.getPreamble()); } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddLessonCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } if (!arePrefixesPresent(argMultimap, PREFIX_SUBJECT, PREFIX_DAY_OF_WEEK, diff --git a/src/main/java/seedu/address/logic/parser/ClearRemarkCommandParser.java b/src/main/java/seedu/address/logic/parser/ClearRemarkCommandParser.java index 09bec8fe959..af45ee05271 100644 --- a/src/main/java/seedu/address/logic/parser/ClearRemarkCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/ClearRemarkCommandParser.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.ClearRemarkCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -23,6 +25,8 @@ public ClearRemarkCommand parse(String args) throws ParseException { } catch (ParseException pe) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearRemarkCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } } diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index 522b93081cc..6e497868846 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.DeleteCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -23,6 +25,8 @@ public DeleteCommand parse(String args) throws ParseException { } catch (ParseException pe) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } } diff --git a/src/main/java/seedu/address/logic/parser/DeleteLessonCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteLessonCommandParser.java index c55c8663c8e..c7c890cd4a9 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteLessonCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteLessonCommandParser.java @@ -2,10 +2,13 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_LESSON_INDEX; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_LESSON; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.DeleteLessonCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -18,7 +21,7 @@ public class DeleteLessonCommandParser implements Parser { * and returns an DeleteLessonCommand object for execution. * @throws ParseException if the user input does not conform the expected format */ - public DeleteLessonCommand parse(String args) throws ParseException { + public DeleteLessonCommand parse(String args) throws ParseException, IndexOutOfBoundsException { requireNonNull(args); ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_LESSON); @@ -30,14 +33,20 @@ public DeleteLessonCommand parse(String args) throws ParseException { } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteLessonCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } if (argMultimap.getValue(PREFIX_LESSON).isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteLessonCommand.MESSAGE_USAGE)); } + Index lessonIndex; - Index lessonIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_LESSON).get()); - + try { + lessonIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_LESSON).get()); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_LESSON_INDEX, ie); + } return new DeleteLessonCommand(tuteeIndex, lessonIndex); } diff --git a/src/main/java/seedu/address/logic/parser/EditCommandParser.java b/src/main/java/seedu/address/logic/parser/EditCommandParser.java index 82a46a21c1f..4ff9742d125 100644 --- a/src/main/java/seedu/address/logic/parser/EditCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/EditCommandParser.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; import static seedu.address.logic.parser.CliSyntax.PREFIX_LEVEL; import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; @@ -17,6 +18,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.EditCommand.EditTuteeDescriptor; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.tag.Tag; @@ -42,6 +44,8 @@ public EditCommand parse(String args) throws ParseException { index = ParserUtil.parseIndex(argMultimap.getPreamble()); } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } EditTuteeDescriptor editTuteeDescriptor = new EditCommand.EditTuteeDescriptor(); diff --git a/src/main/java/seedu/address/logic/parser/GetCommandParser.java b/src/main/java/seedu/address/logic/parser/GetCommandParser.java index 8d020c4df17..97c4d117e3f 100644 --- a/src/main/java/seedu/address/logic/parser/GetCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/GetCommandParser.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.GetCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -23,6 +25,8 @@ public GetCommand parse(String args) throws ParseException { } catch (ParseException pe) { throw new ParseException( String.format(MESSAGE_INVALID_COMMAND_FORMAT, GetCommand.MESSAGE_USAGE), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } } diff --git a/src/main/java/seedu/address/logic/parser/Parser.java b/src/main/java/seedu/address/logic/parser/Parser.java index d6551ad8e3f..c9d9ecff364 100644 --- a/src/main/java/seedu/address/logic/parser/Parser.java +++ b/src/main/java/seedu/address/logic/parser/Parser.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import seedu.address.logic.commands.Command; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -12,5 +13,5 @@ public interface Parser { * Parses {@code userInput} into a command and returns it. * @throws ParseException if {@code userInput} does not conform the expected format */ - T parse(String userInput) throws ParseException; + T parse(String userInput) throws ParseException, IndexOutOfBoundsException; } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index f76261a5d76..78963bb4a66 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static java.util.Objects.requireNonNull; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_LESSON_INDEX; import java.time.DateTimeException; import java.time.DayOfWeek; @@ -16,7 +17,7 @@ import seedu.address.commons.core.Messages; import seedu.address.commons.core.index.Index; -import seedu.address.commons.util.StringUtil; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.lesson.Lesson; import seedu.address.model.lesson.Subject; @@ -36,16 +37,59 @@ public class ParserUtil { public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer."; + public static final String MESSAGE_INDEX_OUT_OF_BOUNDS = "The index provided is invalid"; + + /** + * Returns true if string has length less than or equal to 9 after trimming leading zeroes. + * + * @param trimmedIndex trimmedIndex + * @return whether index is within the acceptable range + */ + private static boolean isWithinIndexRange(String trimmedIndex) { + int currIndex = 0; + for (int i = 0; i < trimmedIndex.length(); i++) { + char c = trimmedIndex.charAt(i); + if (c != '0') { + break; + } else { + currIndex += 1; + } + } + String removedLeadingZeros = trimmedIndex.substring(currIndex); + return !(removedLeadingZeros.length() > 9); + } + + /** + * Returns true is string contains all digits from 0 to 9 + * @param s + * @return + */ + private static boolean isAllDigits(String s) { + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (!(c >= '0' && c <= '9')) { + return false; + } + } + return true; + } + /** * Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be * trimmed. * @throws ParseException if the specified index is invalid (not non-zero unsigned integer). */ - public static Index parseIndex(String oneBasedIndex) throws ParseException { + public static Index parseIndex(String oneBasedIndex) throws ParseException, IndexOutOfBoundsException { String trimmedIndex = oneBasedIndex.trim(); - if (!StringUtil.isNonZeroUnsignedInteger(trimmedIndex)) { + + if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0")) { throw new ParseException(MESSAGE_INVALID_INDEX); } + + if (!isWithinIndexRange(trimmedIndex)) { + throw new IndexOutOfBoundsException(MESSAGE_INDEX_OUT_OF_BOUNDS); + } + return Index.fromOneBased(Integer.parseInt(trimmedIndex)); } @@ -298,7 +342,13 @@ public static Index parseLessonIndex(String lessonIndex) throws ParseException { throw new ParseException(Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); } - Index parsedLessonIndex = ParserUtil.parseIndex(trimmedLessonIndex); + Index parsedLessonIndex; + + try { + parsedLessonIndex = ParserUtil.parseIndex(trimmedLessonIndex); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_LESSON_INDEX, ie); + } return parsedLessonIndex; } diff --git a/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java b/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java index 4d090cf3788..8d7624b2019 100644 --- a/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java @@ -2,6 +2,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_LESSON; import static seedu.address.logic.parser.CliSyntax.PREFIX_PAYMENT_AMOUNT; import static seedu.address.logic.parser.CliSyntax.PREFIX_PAYMENT_DATE; @@ -16,6 +17,7 @@ import seedu.address.logic.commands.paymentcommand.PaymentReceiveCommand; import seedu.address.logic.commands.paymentcommand.PaymentSetAmountCommand; import seedu.address.logic.commands.paymentcommand.PaymentSetDateCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.tutee.Payment; @@ -42,6 +44,8 @@ public PaymentCommand parse(String args) throws ParseException { } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, PaymentCommand.MESSAGE_USAGE_ALL), pe); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } if (argMultimap.getValue(PREFIX_LESSON).isPresent()) { diff --git a/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java b/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java index 136f2bed067..5ed37496860 100644 --- a/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/RemarkCommandParser.java @@ -2,11 +2,13 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; import seedu.address.commons.core.index.Index; import seedu.address.commons.exceptions.IllegalValueException; import seedu.address.logic.commands.RemarkCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.tutee.Remark; @@ -28,6 +30,8 @@ public RemarkCommand parse(String args) throws ParseException { index = ParserUtil.parseIndex(argMultimap.getPreamble()); } catch (IllegalValueException ive) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE), ive); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } String remark = argMultimap.getValue(PREFIX_REMARK).orElse(""); diff --git a/src/main/java/seedu/address/logic/parser/TrackOParser.java b/src/main/java/seedu/address/logic/parser/TrackOParser.java index 6be77220c27..6fa48d71581 100644 --- a/src/main/java/seedu/address/logic/parser/TrackOParser.java +++ b/src/main/java/seedu/address/logic/parser/TrackOParser.java @@ -22,6 +22,7 @@ import seedu.address.logic.commands.RemarkCommand; import seedu.address.logic.commands.ScheduleCommand; import seedu.address.logic.commands.paymentcommand.PaymentCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -41,7 +42,7 @@ public class TrackOParser { * @return the command based on the user input * @throws ParseException if the user input does not conform the expected format */ - public Command parseCommand(String userInput) throws ParseException { + public Command parseCommand(String userInput) throws ParseException, IndexOutOfBoundsException { final Matcher matcher = BASIC_COMMAND_FORMAT.matcher(userInput.trim()); if (!matcher.matches()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE)); diff --git a/src/main/java/seedu/address/logic/parser/exceptions/IndexOutOfBoundsException.java b/src/main/java/seedu/address/logic/parser/exceptions/IndexOutOfBoundsException.java new file mode 100644 index 00000000000..36fe8491c80 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/exceptions/IndexOutOfBoundsException.java @@ -0,0 +1,15 @@ +package seedu.address.logic.parser.exceptions; + +/** + * Represents a parse error encountered when index is out of range. + */ +public class IndexOutOfBoundsException extends Exception { + + public IndexOutOfBoundsException(String message) { + super(message); + } + + public IndexOutOfBoundsException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/seedu/address/ui/MainWindow.java b/src/main/java/seedu/address/ui/MainWindow.java index e671e588b24..02a10e6142c 100644 --- a/src/main/java/seedu/address/ui/MainWindow.java +++ b/src/main/java/seedu/address/ui/MainWindow.java @@ -1,5 +1,7 @@ package seedu.address.ui; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; + import java.util.logging.Logger; import javafx.event.ActionEvent; @@ -15,6 +17,7 @@ import seedu.address.logic.Logic; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -191,6 +194,10 @@ private CommandResult executeCommand(String commandText) throws CommandException logger.info("Invalid command: " + commandText); resultDisplay.setFeedbackToUser(e.getMessage()); throw e; + } catch (IndexOutOfBoundsException ie) { + logger.info("Invalid index: " + commandText); + resultDisplay.setFeedbackToUser(ie.getMessage()); + throw new ParseException(MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX, ie); } } } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 3a00fdf03f1..776b096c691 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -22,6 +22,7 @@ import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.Model; import seedu.address.model.ModelManager; @@ -107,7 +108,13 @@ public void getFilteredTuteeList_modifyList_throwsUnsupportedOperationException( */ private void assertCommandSuccess(String inputCommand, String expectedMessage, Model expectedModel) throws CommandException, ParseException { - CommandResult result = logic.execute(inputCommand); + + CommandResult result; + try { + result = logic.execute(inputCommand); + } catch (IndexOutOfBoundsException ie) { + throw new ParseException(ie.getMessage()); + } assertEquals(expectedMessage, result.getFeedbackToUser()); assertEquals(expectedModel, model); } diff --git a/src/test/java/seedu/address/logic/parser/CommandParserTestUtil.java b/src/test/java/seedu/address/logic/parser/CommandParserTestUtil.java index e4c33515768..a42ca549403 100644 --- a/src/test/java/seedu/address/logic/parser/CommandParserTestUtil.java +++ b/src/test/java/seedu/address/logic/parser/CommandParserTestUtil.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import seedu.address.logic.commands.Command; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; /** @@ -20,6 +21,8 @@ public static void assertParseSuccess(Parser parser, String userInput, Command e assertEquals(expectedCommand, command); } catch (ParseException pe) { throw new IllegalArgumentException("Invalid userInput.", pe); + } catch (IndexOutOfBoundsException ie) { + throw new IllegalArgumentException("Invalid userInput.", ie); } } @@ -33,6 +36,8 @@ public static void assertParseFailure(Parser parser, String userInput, String ex throw new AssertionError("The expected ParseException was not thrown."); } catch (ParseException pe) { assertEquals(expectedMessage, pe.getMessage()); + } catch (IndexOutOfBoundsException ie) { + assertEquals(expectedMessage, ie.getMessage()); } } } diff --git a/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java index 4bab0b88f66..3098f6c768b 100644 --- a/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java @@ -30,6 +30,7 @@ import seedu.address.logic.commands.paymentcommand.PaymentReceiveCommand; import seedu.address.logic.commands.paymentcommand.PaymentSetAmountCommand; import seedu.address.logic.commands.paymentcommand.PaymentSetDateCommand; +import seedu.address.logic.parser.exceptions.IndexOutOfBoundsException; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.tutee.Payment; @@ -126,7 +127,7 @@ public void parse_receivePaymentCommand_success() throws ParseException { } @Test - public void parse_addPaymentCommand_success() throws ParseException { + public void parse_addPaymentCommand_success() throws ParseException, IndexOutOfBoundsException { Index targetIndex = INDEX_SECOND_TUTEE; Index lessonIndex = ParserUtil.parseIndex(VALID_LESSON_INDEX_AMY); String userInput = targetIndex.getOneBased() + PAYMENT_LESSON_INDEX_AMY; From af2dcf9b8cffca218ba4b280aa9119cc81afdfc5 Mon Sep 17 00:00:00 2001 From: yk Wei Date: Wed, 3 Nov 2021 23:59:30 +0800 Subject: [PATCH 2/7] Fix case when input string is empty --- src/main/java/seedu/address/logic/parser/ParserUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 78963bb4a66..325ac90d747 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -82,7 +82,7 @@ private static boolean isAllDigits(String s) { public static Index parseIndex(String oneBasedIndex) throws ParseException, IndexOutOfBoundsException { String trimmedIndex = oneBasedIndex.trim(); - if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0")) { + if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0") || trimmedIndex.equals("")) { throw new ParseException(MESSAGE_INVALID_INDEX); } From 1eb8d595a532674c3955c339e292aed6b3671698 Mon Sep 17 00:00:00 2001 From: yk Wei Date: Thu, 4 Nov 2021 00:54:22 +0800 Subject: [PATCH 3/7] Add defensive programming to ensure that string is not null --- src/main/java/seedu/address/logic/parser/ParserUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 325ac90d747..a7b47b0e138 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -81,7 +81,7 @@ private static boolean isAllDigits(String s) { */ public static Index parseIndex(String oneBasedIndex) throws ParseException, IndexOutOfBoundsException { String trimmedIndex = oneBasedIndex.trim(); - + requireNonNull(trimmedIndex); if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0") || trimmedIndex.equals("")) { throw new ParseException(MESSAGE_INVALID_INDEX); } From e570d8025caeb1eb4b379e0ae0e769ea7a78793c Mon Sep 17 00:00:00 2001 From: yk Wei Date: Thu, 4 Nov 2021 01:02:56 +0800 Subject: [PATCH 4/7] Refactor code and reduce redundancies --- .../seedu/address/commons/core/Messages.java | 4 ++-- .../seedu/address/logic/parser/ParserUtil.java | 18 ++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/Messages.java b/src/main/java/seedu/address/commons/core/Messages.java index 2511f53f35e..0ff6a2420cd 100644 --- a/src/main/java/seedu/address/commons/core/Messages.java +++ b/src/main/java/seedu/address/commons/core/Messages.java @@ -7,8 +7,8 @@ public class Messages { public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command"; public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s"; - public static final String MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX = "The tutee index provided is invalid"; + public static final String MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX = "The tutee index provided is invalid."; public static final String MESSAGE_TUTEES_LISTED_OVERVIEW = "%1$d tutee(s) listed!"; - public static final String MESSAGE_INVALID_LESSON_INDEX = "The lesson index provided is invalid"; + public static final String MESSAGE_INVALID_LESSON_INDEX = "The lesson index provided is invalid."; } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index a7b47b0e138..daa9555d45d 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -37,7 +37,7 @@ public class ParserUtil { public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer."; - public static final String MESSAGE_INDEX_OUT_OF_BOUNDS = "The index provided is invalid"; + public static final String MESSAGE_INDEX_OUT_OF_BOUNDS = "The index provided is invalid."; /** * Returns true if string has length less than or equal to 9 after trimming leading zeroes. @@ -60,18 +60,12 @@ private static boolean isWithinIndexRange(String trimmedIndex) { } /** - * Returns true is string contains all digits from 0 to 9 - * @param s - * @return + * Returns true if string contains only digits from 0 to 9 + * @param s string to check + * @return true if string includes only digits from 0 to 9 */ private static boolean isAllDigits(String s) { - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (!(c >= '0' && c <= '9')) { - return false; - } - } - return true; + return s.matches("^\\d+$"); } /** @@ -82,7 +76,7 @@ private static boolean isAllDigits(String s) { public static Index parseIndex(String oneBasedIndex) throws ParseException, IndexOutOfBoundsException { String trimmedIndex = oneBasedIndex.trim(); requireNonNull(trimmedIndex); - if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0") || trimmedIndex.equals("")) { + if (!isAllDigits(trimmedIndex) || trimmedIndex.equals("0")) { throw new ParseException(MESSAGE_INVALID_INDEX); } From 49e97775b65c2eaa485212eb8ca363d27c2e69b1 Mon Sep 17 00:00:00 2001 From: yk Wei Date: Fri, 5 Nov 2021 16:48:09 +0800 Subject: [PATCH 5/7] Add test cases for integer out of bounds index --- .../seedu/address/logic/parser/PaymentCommandParser.java | 3 +++ .../address/logic/parser/AddLessonCommandParserTest.java | 7 +++++++ .../address/logic/parser/ClearRemarkCommandParserTest.java | 5 +++++ .../address/logic/parser/DeleteCommandParserTest.java | 4 ++++ .../logic/parser/DeleteLessonCommandParserTest.java | 4 ++++ .../seedu/address/logic/parser/EditCommandParserTest.java | 3 +++ .../seedu/address/logic/parser/GetCommandParserTest.java | 4 ++++ .../address/logic/parser/PaymentCommandParserTest.java | 6 ++++++ .../address/logic/parser/RemarkCommandParserTest.java | 5 +++++ src/test/java/seedu/address/testutil/TypicalIndexes.java | 3 ++- 10 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java b/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java index 8d7624b2019..2f0cf6261b1 100644 --- a/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/PaymentCommandParser.java @@ -57,6 +57,7 @@ public PaymentCommand parse(String args) throws ParseException { Index lessonIndex = ParserUtil.parseLessonIndex(argMultimap.getValue(PREFIX_LESSON).get()); return new PaymentAddCommand(index, lessonIndex); } + if (argMultimap.getValue(PREFIX_PAYMENT_AMOUNT).isPresent()) { if (anyPrefixesPresent(argMultimap, PREFIX_LESSON , PREFIX_PAYMENT_DATE, PREFIX_PAYMENT_RECEIVED_DATE)) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, @@ -67,6 +68,7 @@ public PaymentCommand parse(String args) throws ParseException { String paymentValueToSetWithDecimals = String.format("%.2f", paymentValueToSetValue); return new PaymentSetAmountCommand(index, paymentValueToSetWithDecimals); } + if (argMultimap.getValue(PREFIX_PAYMENT_DATE).isPresent()) { if (anyPrefixesPresent(argMultimap, PREFIX_PAYMENT_AMOUNT, PREFIX_LESSON, PREFIX_PAYMENT_RECEIVED_DATE)) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, @@ -80,6 +82,7 @@ public PaymentCommand parse(String args) throws ParseException { } return new PaymentSetDateCommand(index, paymentPayByDateToSet); } + if (argMultimap.getValue(PREFIX_PAYMENT_RECEIVED_DATE).isPresent()) { if (anyPrefixesPresent(argMultimap, PREFIX_PAYMENT_AMOUNT, PREFIX_PAYMENT_DATE, PREFIX_LESSON)) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, diff --git a/src/test/java/seedu/address/logic/parser/AddLessonCommandParserTest.java b/src/test/java/seedu/address/logic/parser/AddLessonCommandParserTest.java index 5c531d28751..fa558599f11 100644 --- a/src/test/java/seedu/address/logic/parser/AddLessonCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddLessonCommandParserTest.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.commands.CommandTestUtil.INVALID_LESSON_DAY_OF_WEEK_DESC; import static seedu.address.logic.commands.CommandTestUtil.INVALID_LESSON_END_TIME_DESC; import static seedu.address.logic.commands.CommandTestUtil.INVALID_LESSON_HOURLY_RATE_DESC; @@ -23,6 +24,7 @@ import static seedu.address.logic.commands.CommandTestUtil.VALID_LESSON_SUBJECT_BOB; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_TUTEE; import java.time.DayOfWeek; @@ -89,10 +91,14 @@ public void parse_invalidPreamble_failure() { "1 i/ string" + LESSON_SUBJECT_DESC_AMY + LESSON_DAY_OF_WEEK_DESC_AMY + LESSON_START_TIME_DESC_AMY + LESSON_END_TIME_DESC_AMY + LESSON_HOURLY_RATE_DESC_AMY, MESSAGE_INVALID_FORMAT); + + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + } @Test public void parse_invalidValue_failure() { + assertParseFailure(parser, "1" + INVALID_LESSON_SUBJECT_DESC + LESSON_DAY_OF_WEEK_DESC_AMY + LESSON_START_TIME_DESC_AMY + LESSON_END_TIME_DESC_AMY + LESSON_HOURLY_RATE_DESC_AMY, @@ -123,6 +129,7 @@ public void parse_invalidValue_failure() { "1" + INVALID_LESSON_SUBJECT_DESC + INVALID_LESSON_DAY_OF_WEEK_DESC + INVALID_LESSON_START_TIME_DESC + LESSON_END_TIME_DESC_AMY + LESSON_HOURLY_RATE_DESC_AMY, Subject.MESSAGE_CONSTRAINTS); // invalid subject + } @Test diff --git a/src/test/java/seedu/address/logic/parser/ClearRemarkCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ClearRemarkCommandParserTest.java index 92da715963c..477a9d9b873 100644 --- a/src/test/java/seedu/address/logic/parser/ClearRemarkCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ClearRemarkCommandParserTest.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import org.junit.jupiter.api.Test; @@ -27,6 +29,9 @@ public void parse_validArgs_returnsGetCommand() { @Test public void parse_invalidArgs_throwsParseException() { + + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, ClearRemarkCommand.MESSAGE_USAGE)); } diff --git a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java index c44a0dc9d4d..dc9069744b6 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteCommandParserTest.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import org.junit.jupiter.api.Test; @@ -27,6 +29,8 @@ public void parse_validArgs_returnsDeleteCommand() { @Test public void parse_invalidArgs_throwsParseException() { + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE)); } } diff --git a/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java index a98f36e492d..589441a1cb4 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java @@ -1,12 +1,14 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.commands.CommandTestUtil.INVALID_LESSON_INDEX; import static seedu.address.logic.commands.CommandTestUtil.LESSON_INDEX; import static seedu.address.logic.commands.CommandTestUtil.VALID_LESSON_INDEX; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.logic.parser.ParserUtil.MESSAGE_INVALID_INDEX; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_TUTEE; import org.junit.jupiter.api.Test; @@ -52,6 +54,8 @@ public void parse_invalidPreamble_failure() { @Test public void parse_invalidValue_failure() { + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "1" + INVALID_LESSON_INDEX, MESSAGE_INVALID_INDEX); // invalid subject } diff --git a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java index 0f019661778..7a741e0fba5 100644 --- a/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/EditCommandParserTest.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.ADDRESS_DESC_BOB; import static seedu.address.logic.commands.CommandTestUtil.INVALID_ADDRESS_DESC; @@ -33,6 +34,7 @@ import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_TUTEE; import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_TUTEE; @@ -87,6 +89,7 @@ public void parse_invalidPreamble_failure() { @Test public void parse_invalidValue_failure() { + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); assertParseFailure(parser, "1" + INVALID_NAME_DESC, Name.MESSAGE_CONSTRAINTS); // invalid name assertParseFailure(parser, "1" + INVALID_PHONE_DESC, Phone.MESSAGE_CONSTRAINTS); // invalid phone assertParseFailure(parser, "1" + INVALID_SCHOOL_DESC, School.MESSAGE_CONSTRAINTS); // invalid school diff --git a/src/test/java/seedu/address/logic/parser/GetCommandParserTest.java b/src/test/java/seedu/address/logic/parser/GetCommandParserTest.java index 26a75a68ce3..b6210dbe38d 100644 --- a/src/test/java/seedu/address/logic/parser/GetCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/GetCommandParserTest.java @@ -1,9 +1,11 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import org.junit.jupiter.api.Test; @@ -27,6 +29,8 @@ public void parse_validArgs_returnsGetCommand() { @Test public void parse_invalidArgs_throwsParseException() { + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, GetCommand.MESSAGE_USAGE)); } } diff --git a/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java index 3098f6c768b..b0729c52bf9 100644 --- a/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/PaymentCommandParserTest.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.commands.CommandTestUtil.INVALID_PAYMENT_AMOUNT_DESC; import static seedu.address.logic.commands.CommandTestUtil.INVALID_PAYMENT_DATE_DESC; import static seedu.address.logic.commands.CommandTestUtil.INVALID_PAYMENT_RECV_DATE_DESC; @@ -17,6 +18,7 @@ import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_TUTEE; import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_TUTEE; @@ -50,6 +52,7 @@ public void parse_missingParts_failure() { @Test public void parse_invalidPreamble_failure() { + // negative index assertParseFailure(parser, "-5" , MESSAGE_INVALID_FORMAT); @@ -65,6 +68,9 @@ public void parse_invalidPreamble_failure() { @Test public void parse_invalidValue_failure() { + // integer out of bounds + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "1" + INVALID_PAYMENT_AMOUNT_DESC, Payment.MESSAGE_CONSTRAINTS); // invalid payment amount // assertParseFailure(parser, "1" + INVALID_LESSON_INDEX_DESC, diff --git a/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java b/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java index e35e865db71..8575f74d129 100644 --- a/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/RemarkCommandParserTest.java @@ -1,10 +1,12 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_TUTEE; +import static seedu.address.testutil.TypicalIndexes.INDEX_OUT_OF_BOUNDS; import org.junit.jupiter.api.Test; @@ -33,6 +35,9 @@ public void parse_indexSpecified_success() { @Test public void parse_missingCompulsoryField_failure() { + + assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemarkCommand.MESSAGE_USAGE); // no parameters diff --git a/src/test/java/seedu/address/testutil/TypicalIndexes.java b/src/test/java/seedu/address/testutil/TypicalIndexes.java index 7e634e64bd7..dbe61953f1c 100644 --- a/src/test/java/seedu/address/testutil/TypicalIndexes.java +++ b/src/test/java/seedu/address/testutil/TypicalIndexes.java @@ -10,8 +10,9 @@ public class TypicalIndexes { public static final Index INDEX_SECOND_TUTEE = Index.fromOneBased(2); public static final Index INDEX_THIRD_TUTEE = Index.fromOneBased(3); - public static final Index INDEX_FIRST_LESSON = Index.fromOneBased(1); public static final Index INDEX_SECOND_LESSON = Index.fromOneBased(2); public static final Index INDEX_THIRD_LESSON = Index.fromOneBased(3); + + public static final String INDEX_OUT_OF_BOUNDS = "2147483648"; } From 471a5d762d4567ca52261f09fa681ecc9e401fee Mon Sep 17 00:00:00 2001 From: yk Wei Date: Fri, 5 Nov 2021 16:56:26 +0800 Subject: [PATCH 6/7] Add invalid lesson index testing for deletelesson --- .../address/logic/parser/DeleteLessonCommandParserTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java b/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java index 589441a1cb4..4d9ab07144d 100644 --- a/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/DeleteLessonCommandParserTest.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import static seedu.address.commons.core.Messages.MESSAGE_INVALID_LESSON_INDEX; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX; import static seedu.address.logic.commands.CommandTestUtil.INVALID_LESSON_INDEX; import static seedu.address.logic.commands.CommandTestUtil.LESSON_INDEX; @@ -56,6 +57,9 @@ public void parse_invalidPreamble_failure() { public void parse_invalidValue_failure() { assertParseFailure(parser, INDEX_OUT_OF_BOUNDS, MESSAGE_INVALID_TUTEE_DISPLAYED_INDEX); + assertParseFailure(parser, "1" + LESSON_INDEX + INDEX_OUT_OF_BOUNDS, + MESSAGE_INVALID_LESSON_INDEX); + assertParseFailure(parser, "1" + INVALID_LESSON_INDEX, MESSAGE_INVALID_INDEX); // invalid subject } From 349b95c47bfae9c505d5ea4fed4d4611a95513e5 Mon Sep 17 00:00:00 2001 From: yk Wei Date: Fri, 5 Nov 2021 17:26:50 +0800 Subject: [PATCH 7/7] Change regex for detecting leading zeros --- .../seedu/address/logic/parser/ParserUtil.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index daa9555d45d..6190c4df3e1 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -46,17 +46,9 @@ public class ParserUtil { * @return whether index is within the acceptable range */ private static boolean isWithinIndexRange(String trimmedIndex) { - int currIndex = 0; - for (int i = 0; i < trimmedIndex.length(); i++) { - char c = trimmedIndex.charAt(i); - if (c != '0') { - break; - } else { - currIndex += 1; - } - } - String removedLeadingZeros = trimmedIndex.substring(currIndex); - return !(removedLeadingZeros.length() > 9); + String strPattern = "^0+(?!$)"; + trimmedIndex = trimmedIndex.replaceAll(strPattern, ""); + return !(trimmedIndex.length() > 9); } /**