Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message to fit desired usage for indexing of integers larger than max int #164

Merged
merged 9 commits into from
Nov 7, 2021
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.";

}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -18,7 +21,7 @@ public class DeleteLessonCommandParser implements Parser<DeleteLessonCommand> {
* 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);
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -12,5 +13,5 @@ public interface Parser<T extends Command> {
* 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;
}
45 changes: 40 additions & 5 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -36,17 +37,45 @@ 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) {
String strPattern = "^0+(?!$)";
trimmedIndex = trimmedIndex.replaceAll(strPattern, "");
return !(trimmedIndex.length() > 9);
}

/**
* 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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a regex check suffices here. return s.matches("^\\d+$")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! Have replaced the existing function with this regex.

return s.matches("^\\d+$");
}

/**
* 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)) {
requireNonNull(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));
}

Expand Down Expand Up @@ -299,7 +328,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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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()) {
Expand All @@ -53,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,
Expand All @@ -63,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,
Expand All @@ -76,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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("");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/TrackOParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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));
Expand Down
Loading