diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 88c2d795f56..cac573c4a71 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -340,4 +340,25 @@ public static Module parseModule(String code, Optional name) throws Pars : Optional.empty(); return new Module(moduleCode, moduleName); } + + /** + * Parses a {@code String args} and returns first word if it is one of mod, lesson, or exam. + * If args is invalid, returns a ParseException with the provided errorMessage. + * Note that we only allow exact lowercase matches for now. + * + * @throws ParseException if the args string is invalid. + */ + public static Type parseFirstArg(String args, String errorMessage) throws ParseException { + String firstArg = args.split(" ", 2)[0]; + switch (firstArg) { + case "mod": + return Type.MOD; + case "lesson": + return Type.LESSON; + case "exam": + return Type.EXAM; + default: + throw new ParseException(errorMessage); + } + } } diff --git a/src/main/java/seedu/address/logic/parser/Type.java b/src/main/java/seedu/address/logic/parser/Type.java new file mode 100644 index 00000000000..51ece51fc2f --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/Type.java @@ -0,0 +1,5 @@ +package seedu.address.logic.parser; + +public enum Type { + MOD, LESSON, EXAM +} diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 6ffb6217379..58e8c21ba28 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -49,6 +49,7 @@ public class ParserUtilTest { private static final String INVALID_LESSON_NAME = " "; private static final String INVALID_MODULE_CODE = "C2030S"; private static final String INVALID_MODULE_NAME = " "; + private static final String INVALID_FIRST_ARG = "shez"; private static final String VALID_NAME = "Rachel Walker"; private static final String VALID_PHONE = "123456"; @@ -67,6 +68,9 @@ public class ParserUtilTest { private static final String VALID_LESSON_NAME = "Lecture"; private static final String VALID_MODULE_CODE = "CS2103T"; private static final String VALID_MODULE_NAME = "Software Engineering"; + private static final String VALID_FIRST_ARG_MOD = "mod"; + private static final String VALID_FIRST_ARG_EXAM = "exam"; + private static final String VALID_FIRST_ARG_LESSON = "lesson"; private static final String WHITESPACE = " \t\r\n"; @@ -485,4 +489,21 @@ public void parseModule_validValues_returnsModule() throws Exception { Module expectedModule = new Module(moduleCode, Optional.of(moduleName)); assertEquals(expectedModule, ParserUtil.parseModule(VALID_MODULE_CODE, Optional.of(VALID_MODULE_NAME))); } + + @Test + public void parseFirstArg_null_throwsNullPointerException() throws Exception { + assertThrows(NullPointerException.class, () -> ParserUtil.parseFirstArg(null, null)); + } + + @Test + public void parseFirstArg_validValues_returnsValidResults() throws Exception { + assertEquals(Type.MOD, ParserUtil.parseFirstArg(VALID_FIRST_ARG_MOD, null)); + assertEquals(Type.LESSON, ParserUtil.parseFirstArg(VALID_FIRST_ARG_LESSON, null)); + assertEquals(Type.EXAM, ParserUtil.parseFirstArg(VALID_FIRST_ARG_EXAM, null)); + } + + @Test + public void parseFirstArg_invalidValue_throwsParseException() throws Exception { + assertThrows(ParseException.class, () -> ParserUtil.parseFirstArg(INVALID_FIRST_ARG, null)); + } }