forked from nus-cs2103-AY2122S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2103-AY2122S1#82 from rohit0718/update/delet…
…e-commands Add delete commands
- Loading branch information
Showing
37 changed files
with
843 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
src/main/java/seedu/address/logic/commands/DeleteCommand.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
src/main/java/seedu/address/logic/commands/delete/DeleteCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/seedu/address/logic/commands/delete/DeleteExamCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/address/logic/commands/delete/DeleteLessonCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/seedu/address/logic/commands/delete/DeleteModCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 69 additions & 7 deletions
76
src/main/java/seedu/address/logic/parser/DeleteCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.