Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#82 from rohit0718/update/delet…
Browse files Browse the repository at this point in the history
…e-commands

Add delete commands
  • Loading branch information
luminousleek authored Oct 14, 2021
2 parents 363a8e9 + 2bd5a51 commit cf8e745
Show file tree
Hide file tree
Showing 37 changed files with 843 additions and 195 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ public class Messages {

public static final String MESSAGE_MODULE_DETAILS_LISTED = "The details of %s are listed";
public static final String MESSAGE_MODULE_NOT_FOUND = "%s does not exist in the ModBook!";
public static final String MESSAGE_INVALID_MODULE_DISPLAYED_INDEX = "The module index provided is invalid";
public static final String MESSAGE_INVALID_LESSON_DISPLAYED_INDEX = "The lesson index provided is invalid";
public static final String MESSAGE_INVALID_EXAM_DISPLAYED_INDEX = "The exam index provided is invalid";
}
53 changes: 0 additions & 53 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package seedu.address.logic.commands.delete;

import seedu.address.logic.commands.Command;

public abstract class DeleteCommand extends Command {
public static final String COMMAND_WORD = "delete";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes a mod/lesson/exam from the mod book. "
+ "Please specify what you would like to delete\n"
+ "Example:\n" + COMMAND_WORD + " mod <parameters>\n"
+ COMMAND_WORD + " lesson <parameters>\n"
+ COMMAND_WORD + " exam <parameters>\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package seedu.address.logic.commands.delete;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CODE;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.exam.Exam;

public class DeleteExamCommand extends DeleteCommand {
public static final String MESSAGE_DELETE_EXAM_SUCCESS = "Deleted Exam %s from Module %s";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes an Exam from a "
+ "specified Module from the Mod book."
+ "\nParameters: Index "
+ PREFIX_CODE + "MOD_CODE"
+ "\nExample: " + COMMAND_WORD + " exam 1 "
+ PREFIX_CODE + "CS2103T";
private final Index targetIndex;
private final ModuleCode targetModuleCode;

/**
* Creates an DeleteExamCommand to delete the Exam at specified {@code Index} of the specified {@code Module}.
*/
public DeleteExamCommand(Index targetIndex, ModuleCode targetModuleCode) {
requireAllNonNull(targetIndex, targetModuleCode);
this.targetIndex = targetIndex;
this.targetModuleCode = targetModuleCode;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Module module = model.getModule(targetModuleCode);
List<Exam> exams = module.getExams();
if (targetIndex.getZeroBased() >= exams.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_EXAM_DISPLAYED_INDEX);
}
Exam examToDelete = exams.get(targetIndex.getZeroBased());
model.deleteExam(module, examToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_EXAM_SUCCESS, examToDelete.getName(), targetModuleCode));
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof DeleteExamCommand)) {
return false;
}
return targetIndex.equals(((DeleteExamCommand) other).targetIndex)
&& targetModuleCode.equals(((DeleteExamCommand) other).targetModuleCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package seedu.address.logic.commands.delete;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CODE;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.Module;
import seedu.address.model.module.ModuleCode;
import seedu.address.model.module.lesson.Lesson;

public class DeleteLessonCommand extends DeleteCommand {
public static final String MESSAGE_DELETE_LESSON_SUCCESS = "Deleted Lesson %s from Module %s";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes a Lesson from a "
+ "specified Module from the Mod book."
+ "\nParameters: Index "
+ PREFIX_CODE + "MOD_CODE"
+ "\nExample: " + COMMAND_WORD + " lesson 1 "
+ PREFIX_CODE + "CS1231S";
private final Index targetIndex;
private final ModuleCode targetModuleCode;

/**
* Creates an DeleteLessonCommand to delete the Lesson at specified {@code Index} of the specified {@code Module}.
*/
public DeleteLessonCommand(Index targetIndex, ModuleCode targetModuleCode) {
requireAllNonNull(targetIndex, targetModuleCode);
this.targetIndex = targetIndex;
this.targetModuleCode = targetModuleCode;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Module module = model.getModule(targetModuleCode);
List<Lesson> lessons = module.getLessons();
if (targetIndex.getZeroBased() >= lessons.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_LESSON_DISPLAYED_INDEX);
}
Lesson lessonToDelete = lessons.get(targetIndex.getZeroBased());
model.deleteLesson(module, lessonToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_LESSON_SUCCESS,
lessonToDelete.getName(), targetModuleCode));
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof DeleteLessonCommand)) {
return false;
}
return targetIndex.equals(((DeleteLessonCommand) other).targetIndex)
&& targetModuleCode.equals(((DeleteLessonCommand) other).targetModuleCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package seedu.address.logic.commands.delete;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.module.Module;

public class DeleteModCommand extends DeleteCommand {
public static final String MESSAGE_DELETE_MODULE_SUCCESS = "Deleted Module: %1$s";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes a module from the Mod book. "
+ "\nParameters: Index"
+ "\nExample: " + COMMAND_WORD + " mod 1";
private final Index targetIndex;

/**
* Creates an DeleteModCommand to delete the module at specified {@code Index}
*/
public DeleteModCommand(Index targetIndex) {
requireNonNull(targetIndex);
this.targetIndex = targetIndex;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Module> lastShownList = model.getFilteredModuleList();
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_MODULE_DISPLAYED_INDEX);
}
Module moduleToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deleteModule(moduleToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete));
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof DeleteModCommand)) {
return false;
}
return targetIndex.equals(((DeleteModCommand) other).targetIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.DetailCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.GuiState;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.add.AddCommand;
import seedu.address.logic.commands.delete.DeleteCommand;
import seedu.address.logic.commands.list.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;

Expand Down
76 changes: 69 additions & 7 deletions src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,92 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CODE;
import static seedu.address.logic.parser.ParserUtil.arePrefixesPresent;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.GuiState;
import seedu.address.logic.commands.delete.DeleteCommand;
import seedu.address.logic.commands.delete.DeleteExamCommand;
import seedu.address.logic.commands.delete.DeleteLessonCommand;
import seedu.address.logic.commands.delete.DeleteModCommand;
import seedu.address.logic.parser.exceptions.GuiStateException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.module.ModuleCode;

/**
* Parses input arguments and creates a new DeleteCommand object
*/
public class DeleteCommandParser implements Parser<DeleteCommand> {
public static final String MESSAGE_WRONG_VIEW_DETAILS = "Please execute the \"detail\" command first!";
public static final String MESSAGE_WRONG_VIEW_SUMMARY = "Please execute \"list mod\" first!";

/**
* Parses the given {@code String} of arguments in the context of the DeleteCommand
* and returns a DeleteCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteCommand parse(String args, GuiState guiState) throws ParseException {
try {
Index index = ParserUtil.parseIndex(args);
return new DeleteCommand(index);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE), pe);
Type type = ParserUtil.parseFirstArg(args, String.format(MESSAGE_INVALID_COMMAND_FORMAT,
DeleteCommand.MESSAGE_USAGE));
switch(type) {
case MOD:
return parseDeleteMod(args, guiState);
case LESSON:
return parseDeleteLesson(args, guiState);
case EXAM:
return parseDeleteExam(args, guiState);
default:
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteCommand.MESSAGE_USAGE));
}
}

/**
* Parses the given {@code String} of arguments in the context of the DeleteModCommand
* and returns a DeleteModCommand object for execution.
* @throws ParseException if the user input does not conform the expected format.
*/
public DeleteModCommand parseDeleteMod(String args, GuiState guiState) throws ParseException {
if (guiState != GuiState.SUMMARY) {
throw new GuiStateException(MESSAGE_WRONG_VIEW_SUMMARY);
}
Index index = ParserUtil.parseFirstIndex(args);
return new DeleteModCommand(index);
}

/**
* Parses the given {@code String} of arguments in the context of the DeleteLessonCommand
* and returns a DeleteLessonCommand object for execution.
* @throws ParseException if the user input does not conform the expected format.
*/
public DeleteLessonCommand parseDeleteLesson(String args, GuiState guiState) throws ParseException {
if (guiState != GuiState.DETAILS) {
throw new GuiStateException(MESSAGE_WRONG_VIEW_DETAILS);
}
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_CODE);
if (!arePrefixesPresent(argMultimap, PREFIX_CODE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteLessonCommand.MESSAGE_USAGE));
}
Index index = ParserUtil.parseFirstIndex(args);
ModuleCode modCode = ParserUtil.parseModuleCode(argMultimap.getValue(PREFIX_CODE).get());
return new DeleteLessonCommand(index, modCode);
}

/**
* Parses the given {@code String} of arguments in the context of the DeleteExamCommand
* and returns a DeleteExamCommand object for execution.
* @throws ParseException if the user input does not conform the expected format.
*/
public DeleteExamCommand parseDeleteExam(String args, GuiState guiState) throws ParseException {
if (guiState != GuiState.DETAILS) {
throw new GuiStateException(MESSAGE_WRONG_VIEW_DETAILS);
}
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_CODE);
if (!arePrefixesPresent(argMultimap, PREFIX_CODE)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteExamCommand.MESSAGE_USAGE));
}
Index index = ParserUtil.parseFirstIndex(args);
ModuleCode modCode = ParserUtil.parseModuleCode(argMultimap.getValue(PREFIX_CODE).get());
return new DeleteExamCommand(index, modCode);
}
}
Loading

0 comments on commit cf8e745

Please sign in to comment.