|
| 1 | +package seedu.address.logic.commands; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_GRP; |
| 5 | +import static seedu.address.logic.parser.CliSyntax.PREFIX_PATH; |
| 6 | + |
| 7 | +import seedu.address.logic.commands.exceptions.CommandException; |
| 8 | +import seedu.address.model.Model; |
| 9 | +import seedu.address.model.group.Group; |
| 10 | +import seedu.address.model.group.GrpContainsKeywordPredicate; |
| 11 | +import seedu.address.model.group.Lesson; |
| 12 | +import seedu.address.model.group.LsnContainsKeywordPredicate; |
| 13 | + |
| 14 | +public class AddLsnCommand extends Command { |
| 15 | + |
| 16 | + public static final String COMMAND_WORD = "addlsn"; |
| 17 | + public static final String MESSAGE_USAGE = COMMAND_WORD |
| 18 | + + ": Adds a new lesson to a specified tutorial group. " |
| 19 | + + "Parameters: " |
| 20 | + + PREFIX_GRP + "GRP " |
| 21 | + + PREFIX_PATH + "PATH "; |
| 22 | + |
| 23 | + public static final String MESSAGE_SUCCESS = "New lesson for tutorial group %2$s added: %1$s"; |
| 24 | + public static final String MESSAGE_DUPLICATE_LESSON = "This lesson for tutorial group %1$s already exists."; |
| 25 | + public static final String MESSAGE_GROUP_DOES_NOT_EXIST = "Specified Tutorial Group does not exist!"; |
| 26 | + private final GrpContainsKeywordPredicate trgtGrp; |
| 27 | + private final String toAdd; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates an AddGrpCommand to add the specified {@code Group} |
| 31 | + */ |
| 32 | + public AddLsnCommand(String lesson, GrpContainsKeywordPredicate target) { |
| 33 | + requireNonNull(lesson); |
| 34 | + trgtGrp = target; |
| 35 | + toAdd = lesson; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public CommandResult execute(Model model) throws CommandException { |
| 40 | + requireNonNull(model); |
| 41 | + model.updateFilteredGroupList(trgtGrp); |
| 42 | + |
| 43 | + if (model.getFilteredGroupList().isEmpty()) { |
| 44 | + throw new CommandException(MESSAGE_GROUP_DOES_NOT_EXIST); |
| 45 | + } |
| 46 | + |
| 47 | + Group trgtGrp = model.getFilteredGroupList().get(0); |
| 48 | + Lesson toAdd = new Lesson(this.toAdd, trgtGrp.getStudents()); |
| 49 | + |
| 50 | + if (trgtGrp.getLessons().contains(toAdd)) { |
| 51 | + throw new CommandException(String.format(MESSAGE_DUPLICATE_LESSON, toAdd, trgtGrp)); |
| 52 | + } |
| 53 | + |
| 54 | + trgtGrp.getLessons().add(toAdd); |
| 55 | + |
| 56 | + model.updateLessonList(); |
| 57 | + model.updateFilteredLessonList(new LsnContainsKeywordPredicate(this.toAdd)); |
| 58 | + |
| 59 | + return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd, trgtGrp), false, false, true, false); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean equals(Object other) { |
| 64 | + return other == this // short circuit if same object |
| 65 | + || (other instanceof AddLsnCommand // instanceof handles nulls |
| 66 | + && toAdd.equals(((AddLsnCommand) other).toAdd)); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments