Skip to content

Commit

Permalink
Delete class command (AY2324S2-CS2103T-T09-4#76)
Browse files Browse the repository at this point in the history
* Add delete_class functionality

* Fix checkstyle issues

* Fix test case

* Add test cases

* Fix naming convention
  • Loading branch information
whelan-low authored Mar 19, 2024
1 parent f995f97 commit 8de04b8
Show file tree
Hide file tree
Showing 15 changed files with 401 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* A class that handles the /add_class command execution.
*/
public class AddClassCommand extends Command {
public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added %1$s %2$s";
public static final String MESSAGE_ADD_CLASS_SUCCESS = "Added %1$s %2$s";
public static final String MESSAGE_DUPLICATE_CLASS = "%1$s %2$s already added!";
public static final String COMMAND_WORD = "/add_class";

Expand Down Expand Up @@ -60,7 +60,7 @@ public CommandResult execute(Model model) throws CommandException {
* {@code personToEdit}.
*/
private String generateSuccessMessage(ModuleCode module, TutorialClass tutorialString) {
return String.format(MESSAGE_ADD_REMARK_SUCCESS, module.toString(), tutorialString.toString());
return String.format(MESSAGE_ADD_CLASS_SUCCESS, module.toString(), tutorialString.toString());
}

@Override
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteClassCommand.java
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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteClassCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -70,6 +71,9 @@ public Command parseCommand(String userInput) throws ParseException {
case ClearCommand.COMMAND_WORD:
return new ClearCommand();

case DeleteClassCommand.COMMAND_WORD:
return new DeleteClassCommandParser().parse(arguments);

case DeleteStudentCommand.COMMAND_WORD:
return new DeleteStudentCommandParser().parse(arguments);

Expand Down
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());
}
}
56 changes: 42 additions & 14 deletions src/main/java/seedu/address/model/module/ModuleCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ModuleCode {
*/
public static final String VALIDATION_REGEX = "^[A-Z]{2,3}\\d{4}[A-Z]?$";

public final String value;
public final String moduleCode;
private final ArrayList<TutorialClass> tutorialClasses;

/**
Expand All @@ -31,7 +31,7 @@ public class ModuleCode {
public ModuleCode(String moduleCode) {
requireAllNonNull(moduleCode);
checkArgument(isValidModuleCode(moduleCode), MESSAGE_CONSTRAINTS);
this.value = moduleCode;
this.moduleCode = moduleCode;
this.tutorialClasses = new ArrayList<TutorialClass>();
}

Expand All @@ -45,7 +45,7 @@ public ModuleCode(String moduleCode) {
public ModuleCode(String moduleCode, String tutorialClass) {
requireAllNonNull(moduleCode);
checkArgument(isValidModuleCode(moduleCode), MESSAGE_CONSTRAINTS);
this.value = moduleCode;
this.moduleCode = moduleCode;
this.tutorialClasses = new ArrayList<TutorialClass>();
tutorialClasses.add(new TutorialClass(tutorialClass));
}
Expand All @@ -60,7 +60,7 @@ public ModuleCode(String moduleCode, String tutorialClass) {
public ModuleCode(String moduleCode, ArrayList<TutorialClass> tutorialClasses) {
requireAllNonNull(moduleCode);
checkArgument(isValidModuleCode(moduleCode), MESSAGE_CONSTRAINTS);
this.value = moduleCode;
this.moduleCode = moduleCode;
this.tutorialClasses = tutorialClasses;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public ModuleCode getModule() {

@Override
public String toString() {
return value;
return moduleCode;
}

@Override
Expand All @@ -105,35 +105,63 @@ public boolean equals(Object other) {
}

ModuleCode otherModuleCode = (ModuleCode) other;
return value.equals(otherModuleCode.value);
return moduleCode.equals(otherModuleCode.moduleCode);
}

@Override
public int hashCode() {
return value.hashCode();
return moduleCode.hashCode();
}

/**
* Checks if the given tutorial class is already in the list.
*
* @param tutorialString name of the tutorial class to be checked
* @param tutorialClass name of the tutorial class to be checked
* @return true if the class name is in the list. False otherwise.
*/
public boolean hasTutorialClass(TutorialClass tutorialString) {
for (TutorialClass tutorialClass : tutorialClasses) {
if (tutorialString.equals(tutorialClass)) {
public boolean hasTutorialClass(TutorialClass tutorialClass) {
for (TutorialClass tutorialClassInModule : tutorialClasses) {
if (tutorialClass.equals(tutorialClassInModule)) {
return true;
}
}
return false;
}

/**
* List all the tutorial classes under this module.
*
* @return String of tutorial classes under this module.
*/
public String listTutorialClasses() {
if (tutorialClasses.size() == 0) {
return String.format("Tutorials in %s: None!", moduleCode);
} else {
StringBuilder tutorialsString = new StringBuilder(String.format("Tutorials in %s:", moduleCode));
for (TutorialClass tutorialClass : tutorialClasses) {
tutorialsString.append(" ");
tutorialsString.append(tutorialClass.toString());
}
return tutorialsString.toString().trim();
}
}

/**
* Adds an empty tutorial with the given name into the module.
*
* @param tutorialString name of tutorial class to be added.
* @param tutorialClass name of tutorial class to be added.
*/
public void addTutorialClass(TutorialClass tutorialClass) {
tutorialClasses.add(tutorialClass);
}

/**
* Deletes a tutorial with the given name from the module.
* The tutorial has to exist to be used in this function.
*
* @param tutorialClass name of tutorial class to be deleted.
*/
public void addTutorialClass(TutorialClass tutorialString) {
tutorialClasses.add(tutorialString);
public void deleteTutorialClass(TutorialClass tutorialClass) {
tutorialClasses.remove(tutorialClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ModuleContainsKeywordPredicate(String keyword) {

@Override
public boolean test(ModuleCode moduleCode) {
return StringUtil.containsPartialWordIgnoreCase(moduleCode.getModule().value, keyword);
return StringUtil.containsPartialWordIgnoreCase(moduleCode.getModule().moduleCode, keyword);
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/seedu/address/model/module/TutorialClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TutorialClass {
*/
public static final String VALIDATION_REGEX = "^[A-Z]\\d{2}$";

public final String value;
public final String tutorialName;
private final ArrayList<Person> students;

/**
Expand All @@ -33,7 +33,7 @@ public class TutorialClass {
public TutorialClass(String tutorialClass) {
requireAllNonNull(tutorialClass);
checkArgument(isValidTutorialClass(tutorialClass), MESSAGE_CONSTRAINTS);
this.value = tutorialClass;
this.tutorialName = tutorialClass;
this.students = new ArrayList<>();
}

Expand All @@ -46,7 +46,7 @@ public TutorialClass(String tutorialClass) {
public TutorialClass(String tutorialClass, ArrayList<Person> students) {
requireAllNonNull(tutorialClass);
checkArgument(isValidTutorialClass(tutorialClass), MESSAGE_CONSTRAINTS);
this.value = tutorialClass;
this.tutorialName = tutorialClass;
this.students = students;
}

Expand All @@ -65,7 +65,7 @@ public ArrayList<Person> getStudents() {

@Override
public String toString() {
return value;
return tutorialName;
}

@Override
Expand All @@ -80,11 +80,11 @@ public boolean equals(Object other) {
}

TutorialClass otherTutorialClass = (TutorialClass) other;
return value.equals(otherTutorialClass.value);
return tutorialName.equals(otherTutorialClass.tutorialName);
}

@Override
public int hashCode() {
return value.hashCode();
return tutorialName.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public TutorialContainsKeywordPredicate(String keyword) {

@Override
public boolean test(TutorialClass tutorialClass) {
return StringUtil.containsPartialWordIgnoreCase(tutorialClass.getTutorialClass().value, keyword);
return StringUtil.containsPartialWordIgnoreCase(tutorialClass.getTutorialClass().tutorialName, keyword);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.AddClassCommand.MESSAGE_ADD_REMARK_SUCCESS;
import static seedu.address.logic.commands.AddClassCommand.MESSAGE_ADD_CLASS_SUCCESS;
import static seedu.address.logic.commands.AddClassCommand.MESSAGE_DUPLICATE_CLASS;
import static seedu.address.logic.commands.CommandTestUtil.VALID_MODULE_AMY;
import static seedu.address.logic.commands.CommandTestUtil.VALID_MODULE_BOB;
Expand Down Expand Up @@ -34,7 +34,7 @@ public void execute_success() {

assertCommandSuccess(new AddClassCommand(new ModuleCode(VALID_MODULE_AMY),
new TutorialClass(VALID_TUTORIAL_AMY)), model,
String.format(MESSAGE_ADD_REMARK_SUCCESS, VALID_MODULE_AMY, VALID_TUTORIAL_AMY), model);
String.format(MESSAGE_ADD_CLASS_SUCCESS, VALID_MODULE_AMY, VALID_TUTORIAL_AMY), model);
}

@Test
Expand Down Expand Up @@ -64,7 +64,7 @@ public void execute_duplicateModuleDifferentTutorial_success() {

assertCommandSuccess(new AddClassCommand(new ModuleCode(VALID_MODULE_AMY),
new TutorialClass(VALID_TUTORIAL_BOB)), actualModel,
String.format(MESSAGE_ADD_REMARK_SUCCESS, VALID_MODULE_AMY, VALID_TUTORIAL_BOB), expectedModel);
String.format(MESSAGE_ADD_CLASS_SUCCESS, VALID_MODULE_AMY, VALID_TUTORIAL_BOB), expectedModel);

ModuleCode moduleFromList = actualModel.findModuleFromList(module);
assertEquals(moduleFromList.getTutorialClasses().get(1).toString(), VALID_TUTORIAL_BOB);
Expand Down
Loading

0 comments on commit 8de04b8

Please sign in to comment.