forked from AY2324S2-CS2103T-T09-4/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.
Delete class command (AY2324S2-CS2103T-T09-4#76)
* Add delete_class functionality * Fix checkstyle issues * Fix test case * Add test cases * Fix naming convention
- Loading branch information
1 parent
f995f97
commit 8de04b8
Showing
15 changed files
with
401 additions
and
36 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
85 changes: 85 additions & 0 deletions
85
src/main/java/seedu/address/logic/commands/DeleteClassCommand.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,85 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
|
||
/** | ||
* A class used to handle the deletion of tutorial classes. | ||
*/ | ||
public class DeleteClassCommand extends Command { | ||
public static final String MESSAGE_DELETE_CLASS_SUCCESS = "Removed %1$s %2$s!"; | ||
public static final String MESSAGE_MODULE_NOT_FOUND = "%1$s not in list!"; | ||
public static final String MESSAGE_CLASS_NOT_FOUND = "%1$s %2$s not in list!"; | ||
public static final String COMMAND_WORD = "/delete_class"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes a class with the module code and tutorial class specified\n" | ||
+ "Parameters:" + PREFIX_MODULECODE + "MODULE_CODE (must be a String) " | ||
+ PREFIX_TUTORIALCLASS + "TUTORIAL_CLASS (must be a String)" | ||
+ "Example: " + COMMAND_WORD + PREFIX_MODULECODE + " CS2103T " | ||
+ PREFIX_TUTORIALCLASS + "T09"; | ||
|
||
private final ModuleCode module; | ||
private final TutorialClass tutorialString; | ||
|
||
/** | ||
* @param module of the tutorial class to be added | ||
*/ | ||
public DeleteClassCommand(ModuleCode module, TutorialClass tutorialClass) { | ||
requireAllNonNull(module); | ||
this.module = module; | ||
this.tutorialString = tutorialClass; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
ModuleCode existingModule = model.findModuleFromList(module); | ||
if (existingModule == null) { | ||
String moduleNotFoundMessage = String.format(MESSAGE_MODULE_NOT_FOUND, module); | ||
throw new CommandException(moduleNotFoundMessage); | ||
} | ||
|
||
if (!(existingModule.hasTutorialClass(tutorialString))) { | ||
String classNotFoundMessage = String.format(MESSAGE_CLASS_NOT_FOUND, module, tutorialString); | ||
String tutorialList = existingModule.listTutorialClasses(); | ||
throw new CommandException(classNotFoundMessage + "\n" + tutorialList); | ||
} else { | ||
existingModule.deleteTutorialClass(tutorialString); | ||
} | ||
|
||
return new CommandResult(generateSuccessMessage(module, tutorialString)); | ||
} | ||
|
||
/** | ||
* Generates a command execution success message based on whether the removed from | ||
* {@code personToEdit}. | ||
*/ | ||
private String generateSuccessMessage(ModuleCode module, TutorialClass tutorialString) { | ||
return String.format(MESSAGE_DELETE_CLASS_SUCCESS, module.toString(), tutorialString.toString()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteClassCommand)) { | ||
return false; | ||
} | ||
|
||
DeleteClassCommand e = (DeleteClassCommand) other; | ||
return module.equals(e.module); | ||
} | ||
} | ||
|
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
49 changes: 49 additions & 0 deletions
49
src/main/java/seedu/address/logic/parser/DeleteClassCommandParser.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,49 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULECODE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TUTORIALCLASS; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.DeleteClassCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.module.ModuleCode; | ||
import seedu.address.model.module.TutorialClass; | ||
|
||
|
||
|
||
/** | ||
* Parses input arguments and creates a new {@code RemarkCommand} object | ||
*/ | ||
public class DeleteClassCommandParser implements Parser<DeleteClassCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the {@code RemarkCommand} | ||
* and returns a {@code RemarkCommand} object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public DeleteClassCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_MODULECODE, PREFIX_TUTORIALCLASS) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteClassCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String moduleCode = argMultimap.getValue(PREFIX_MODULECODE).orElse(""); | ||
String tutorialClass = argMultimap.getValue(PREFIX_TUTORIALCLASS).orElse(""); | ||
if (!(ModuleCode.isValidModuleCode(moduleCode))) { | ||
throw new ParseException(ModuleCode.MESSAGE_CONSTRAINTS); | ||
} | ||
if (!(TutorialClass.isValidTutorialClass(tutorialClass))) { | ||
throw new ParseException(TutorialClass.MESSAGE_CONSTRAINTS); | ||
} | ||
return new DeleteClassCommand(new ModuleCode(moduleCode), new TutorialClass(tutorialClass)); | ||
} | ||
|
||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.