From 281655d7be5b8b762d7394c871b6c4e788001058 Mon Sep 17 00:00:00 2001 From: Lennoxtr Date: Sat, 18 Mar 2023 12:52:17 +0800 Subject: [PATCH] Update parsing of Tags to show user which tag has incorrect format --- .../address/logic/commands/TagCommand.java | 42 ++++++++++--------- .../address/logic/commands/UntagCommand.java | 20 +++++---- .../address/logic/parser/ParserUtil.java | 15 ++++--- .../java/seedu/address/model/tag/Tag.java | 2 +- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/TagCommand.java b/src/main/java/seedu/address/logic/commands/TagCommand.java index 803b97a707e..069f369dd0e 100644 --- a/src/main/java/seedu/address/logic/commands/TagCommand.java +++ b/src/main/java/seedu/address/logic/commands/TagCommand.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Set; +import seedu.address.commons.core.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.lecture.Lecture; @@ -26,14 +27,9 @@ public class TagCommand extends Command { public static final String COMMAND_WORD = "tag"; - //TODO: MODIFY THIS public static final String MESSAGE_USAGE = COMMAND_WORD + ": Tag a specified video, module, or lecture "; - //TODO: MODIFY THIS - public static final String MESSAGE_SUCCESS = "Item tagged"; - public static final String MESSAGE_MODULE_NOT_FOUND = "Module doesn't exist in Le Tracker"; - public static final String MESSAGE_LECTURE_NOT_FOUND = "Lecture doesn't exist in this module"; - public static final String MESSAGE_VIDEO_NOT_FOUND = "Video doesn't exist in this lecture"; + public static final String MESSAGE_SUCCESS = "%1$s tagged"; private final Set tags; @@ -95,18 +91,17 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); if (this.isTaggingMod) { - tagModule(model); + return tagModule(model); } else if (this.isTaggingLec) { - tagLecture(model); - } else if (this.isTaggingVid) { - tagVideo(model); + return tagLecture(model); + } else { + return tagVideo(model); } - return new CommandResult(MESSAGE_SUCCESS); } - private void tagModule(Model model) throws CommandException { + private CommandResult tagModule(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { - throw new CommandException(MESSAGE_MODULE_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } ReadOnlyModule taggingModule = model.getModule(this.moduleCode); @@ -122,15 +117,17 @@ private void tagModule(Model model) throws CommandException { Module taggedModule = new Module(taggingModule.getCode(), taggingModule.getName(), newTags, currentLectureList); model.setModule(taggingModule, taggedModule); + return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode)); } - private void tagLecture(Model model) throws CommandException { + private CommandResult tagLecture(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { - throw new CommandException(MESSAGE_MODULE_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } if (!model.hasLecture(this.moduleCode, this.lectureName)) { - throw new CommandException(MESSAGE_LECTURE_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName, + moduleCode)); } ReadOnlyModule targetModule = model.getModule(this.moduleCode); @@ -143,22 +140,26 @@ private void tagLecture(Model model) throws CommandException { Lecture taggedLecture = new Lecture(taggingLecture.getName(), newTags, taggingLecture.getVideoList()); model.setLecture(targetModule, taggingLecture, taggedLecture); + return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName)); } - private void tagVideo(Model model) throws CommandException { + private CommandResult tagVideo(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { - throw new CommandException(MESSAGE_MODULE_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } if (!model.hasLecture(this.moduleCode, this.lectureName)) { - throw new CommandException(MESSAGE_LECTURE_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_LECTURE_DOES_NOT_EXIST, this.lectureName, + moduleCode)); } ReadOnlyModule targetModule = model.getModule(this.moduleCode); ReadOnlyLecture targetLecture = targetModule.getLecture(this.lectureName); if (!model.hasVideo(targetLecture, this.videoName)) { - throw new CommandException(MESSAGE_VIDEO_NOT_FOUND); + throw new CommandException(String.format(Messages.MESSAGE_VIDEO_DOES_NOT_EXIST, this.videoName, + this.lectureName, + this.moduleCode)); } Video taggingVideo = targetLecture.getVideo(this.videoName); @@ -170,5 +171,6 @@ private void tagVideo(Model model) throws CommandException { Video taggedVideo = new Video(taggingVideo.getName(), taggingVideo.hasWatched(), newTags); model.setVideo(targetLecture, taggingVideo, taggedVideo); + return new CommandResult(String.format(MESSAGE_SUCCESS, videoName)); } } diff --git a/src/main/java/seedu/address/logic/commands/UntagCommand.java b/src/main/java/seedu/address/logic/commands/UntagCommand.java index 15fcd8ca278..85e83a12817 100644 --- a/src/main/java/seedu/address/logic/commands/UntagCommand.java +++ b/src/main/java/seedu/address/logic/commands/UntagCommand.java @@ -31,7 +31,7 @@ public class UntagCommand extends Command { //TODO: MODIFY THIS public static final String MESSAGE_USAGE = COMMAND_WORD + ": Untag a specified video, module, or lecture "; //TODO: MODIFY THIS - public static final String MESSAGE_SUCCESS = "Item untagged"; + public static final String MESSAGE_SUCCESS = "%1$s untagged"; private final Tag tag; private final VideoName videoName; private final LectureName lectureName; @@ -93,16 +93,15 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); if (this.isUntaggingMod) { - untagModule(model); + return untagModule(model); } else if (this.isUntaggingLec) { - untagLecture(model); - } else if (this.isUntaggingLec) { - untagVideo(model); + return untagLecture(model); + } else { + return untagVideo(model); } - return new CommandResult(MESSAGE_SUCCESS); } - private void untagModule(Model model) throws CommandException { + private CommandResult untagModule(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } @@ -126,9 +125,10 @@ private void untagModule(Model model) throws CommandException { Module untaggedModule = new Module(untaggingModule.getCode(), untaggingModule.getName(), newTags, currentLectureList); model.setModule(untaggingModule, untaggedModule); + return new CommandResult(String.format(MESSAGE_SUCCESS, moduleCode)); } - private void untagLecture(Model model) throws CommandException { + private CommandResult untagLecture(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } @@ -155,9 +155,10 @@ private void untagLecture(Model model) throws CommandException { Lecture untaggedLecture = new Lecture(untaggingLecture.getName(), newTags, untaggingLecture.getVideoList()); model.setLecture(targetModule, untaggingLecture, untaggedLecture); + return new CommandResult(String.format(MESSAGE_SUCCESS, lectureName)); } - private void untagVideo(Model model) throws CommandException { + private CommandResult untagVideo(Model model) throws CommandException { if (!model.hasModule(moduleCode)) { throw new CommandException(String.format(Messages.MESSAGE_MODULE_DOES_NOT_EXIST, moduleCode)); } @@ -191,5 +192,6 @@ private void untagVideo(Model model) throws CommandException { Video taggedVideo = new Video(untaggingVideo.getName(), untaggingVideo.hasWatched(), newTags); model.setVideo(targetLecture, untaggingVideo, taggedVideo); + return new CommandResult(String.format(MESSAGE_SUCCESS, videoName)); } } diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index c44086995d7..d2d43efb55a 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -115,7 +115,7 @@ public static Tag parseSingleTag(String tag) throws ParseException { requireNonNull(tag); String trimmedTag = tag.trim(); if (!Tag.isValidTagName(trimmedTag)) { - throw new ParseException(Tag.MESSAGE_CONSTRAINTS); + throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS, trimmedTag)); } return new Tag(trimmedTag); } @@ -135,11 +135,14 @@ public static Set parseMultiTags(String tags) throws ParseException { String[] arrayOfTags = tags.split(","); - for (String tagName : arrayOfTags) { - tagName = tagName.trim(); - if (!Tag.isValidTagName(tagName)) { - throw new ParseException(Tag.MESSAGE_CONSTRAINTS); - } + List listOfUnvalidTagName = Arrays.stream(arrayOfTags) + .map(tag -> tag.trim()) + .filter(trimmedTag -> !Tag.isValidTagName(trimmedTag)) + .collect(Collectors.toList()); + + if (listOfUnvalidTagName.size() > 0) { + throw new ParseException(String.format(Tag.MESSAGE_CONSTRAINTS, + String.join(", ", listOfUnvalidTagName))); } List listOfTags = Arrays.stream(arrayOfTags) diff --git a/src/main/java/seedu/address/model/tag/Tag.java b/src/main/java/seedu/address/model/tag/Tag.java index 95c5037429c..7a41d6f6be6 100644 --- a/src/main/java/seedu/address/model/tag/Tag.java +++ b/src/main/java/seedu/address/model/tag/Tag.java @@ -9,7 +9,7 @@ */ public class Tag { - public static final String MESSAGE_CONSTRAINTS = "Tags names should be alphanumeric"; + public static final String MESSAGE_CONSTRAINTS = "Tag %1$s should be alphanumeric"; public static final String VALIDATION_REGEX = "\\p{Alnum}+"; public final String tagName;