forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #126 from ldinghan/branch-feature-session
Add delete session command
- Loading branch information
Showing
10 changed files
with
305 additions
and
51 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
68 changes: 68 additions & 0 deletions
68
src/main/java/seedu/address/logic/commands/DeleteSessionCommand.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,68 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SESSION; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.session.Session; | ||
import seedu.address.model.session.SessionNumber; | ||
|
||
/** | ||
* Deletes a session identified using it's displayed index from the session list book. | ||
*/ | ||
public class DeleteSessionCommand extends Command { | ||
public static final String COMMAND_WORD = "deletesession"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the session identified by the session number.\n" | ||
+ "Parameters: " | ||
+ PREFIX_SESSION + "SESSION_NUMBER (session must already be created)\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_SESSION + "1"; | ||
|
||
public static final String MESSAGE_DELETE_SESSION_SUCCESS = "Deleted Session: %1$s"; | ||
|
||
private final SessionNumber targetSessionNumber; | ||
|
||
public DeleteSessionCommand(SessionNumber targetSessionNumber) { | ||
this.targetSessionNumber = targetSessionNumber; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
Session sessionToDelete = model.findSessionBySessionNumber(targetSessionNumber); | ||
if (sessionToDelete == null) { | ||
throw new CommandException(Messages.MESSAGE_SESSION_NOT_FOUND); | ||
} | ||
|
||
model.deleteSession(sessionToDelete); | ||
return new CommandResult(String.format(MESSAGE_DELETE_SESSION_SUCCESS, | ||
Messages.format(sessionToDelete))); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteSessionCommand)) { | ||
return false; | ||
} | ||
|
||
DeleteSessionCommand otherDeleteSessionCommand = (DeleteSessionCommand) other; | ||
return targetSessionNumber.equals(otherDeleteSessionCommand.targetSessionNumber); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetSessionNumber", targetSessionNumber) | ||
.toString(); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/logic/parser/DeleteSessionCommandParser.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,48 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SESSION; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.DeleteSessionCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.session.SessionNumber; | ||
|
||
|
||
|
||
/** | ||
* Parses input arguments and creates a new DeleteSessionCommand object | ||
*/ | ||
public class DeleteSessionCommandParser implements Parser<DeleteSessionCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the DeleteSessionCommand | ||
* and returns a DeleteSessionCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
@Override | ||
public DeleteSessionCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_SESSION); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_SESSION) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteSessionCommand.MESSAGE_USAGE)); | ||
} | ||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_SESSION); | ||
SessionNumber targetSessionNumber = ParserUtil.parseSessionNumber(argMultimap.getValue(PREFIX_SESSION).get()); | ||
return new DeleteSessionCommand(targetSessionNumber); | ||
} | ||
|
||
/** | ||
* Checks if the specified prefixes are present in the given ArgumentMultimap. | ||
* | ||
* @param argumentMultimap The ArgumentMultimap to check for prefix presence. | ||
* @param prefixes The prefixes to be checked. | ||
* @return `true` if all specified prefixes are present, `false` otherwise. | ||
*/ | ||
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
86 changes: 86 additions & 0 deletions
86
src/test/java/seedu/address/logic/commands/DeleteSessionCommandTest.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,86 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; | ||
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; | ||
import static seedu.address.testutil.TypicalConsultations.getTypicalConsultationListBook; | ||
import static seedu.address.testutil.TypicalGradedTest.getTypicalGradedTestList; | ||
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; | ||
import static seedu.address.testutil.TypicalSessions.getTypicalSessionList; | ||
import static seedu.address.testutil.TypicalTasks.getTypicalTaskList; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.Messages; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.ModelManager; | ||
import seedu.address.model.UserPrefs; | ||
import seedu.address.model.session.Session; | ||
import seedu.address.model.session.SessionNumber; | ||
import seedu.address.testutil.TypicalSessions; | ||
|
||
public class DeleteSessionCommandTest { | ||
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalTaskList(), | ||
getTypicalSessionList(), getTypicalConsultationListBook(), getTypicalGradedTestList()); | ||
|
||
@Test | ||
public void execute_validSessionNumber_success() { | ||
Session sessionToDelete = model.findSessionBySessionNumber(TypicalSessions.SESSION_TYPICAL1.getSessionNumber()); | ||
DeleteSessionCommand deleteSessionCommand = new DeleteSessionCommand( | ||
TypicalSessions.SESSION_TYPICAL1.getSessionNumber()); | ||
|
||
String expectedMessage = String.format(DeleteSessionCommand.MESSAGE_DELETE_SESSION_SUCCESS, | ||
Messages.format(sessionToDelete)); | ||
|
||
ModelManager expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalTaskList(), | ||
getTypicalSessionList(), getTypicalConsultationListBook(), getTypicalGradedTestList()); | ||
|
||
expectedModel.deleteSession(sessionToDelete); | ||
|
||
assertCommandSuccess(deleteSessionCommand, model, expectedMessage, expectedModel); | ||
} | ||
|
||
@Test | ||
public void execute_invalidSessionNumber_throwsCommandException() { | ||
SessionNumber targetSessionNumber = new SessionNumber("12312"); | ||
DeleteSessionCommand deleteSessionCommand = new DeleteSessionCommand(targetSessionNumber); | ||
|
||
assertCommandFailure(deleteSessionCommand, model, Messages.MESSAGE_SESSION_NOT_FOUND); | ||
} | ||
|
||
@Test | ||
public void equals() { | ||
DeleteSessionCommand deleteFirstCommand = new DeleteSessionCommand( | ||
TypicalSessions.SESSION_TYPICAL1.getSessionNumber()); | ||
DeleteSessionCommand deleteSecondCommand = new DeleteSessionCommand( | ||
TypicalSessions.SESSION_TYPICAL2.getSessionNumber()); | ||
|
||
// same object -> returns true | ||
assertEquals(deleteFirstCommand, deleteFirstCommand); | ||
|
||
// same values -> returns true | ||
DeleteSessionCommand deleteFirstCommandCopy = new DeleteSessionCommand( | ||
TypicalSessions.SESSION_TYPICAL1.getSessionNumber()); | ||
assertEquals(deleteFirstCommand, deleteFirstCommandCopy); | ||
|
||
// different types -> returns false | ||
assertFalse(deleteFirstCommand.equals(1)); | ||
|
||
// null -> returns false | ||
assertNotEquals(null, deleteFirstCommand); | ||
|
||
// different person -> returns false | ||
assertNotEquals(deleteFirstCommand, deleteSecondCommand); | ||
} | ||
|
||
@Test | ||
public void toStringMethod() { | ||
SessionNumber targetSessionNumber = new SessionNumber("2"); | ||
DeleteSessionCommand deleteSessionCommand = new DeleteSessionCommand(targetSessionNumber); | ||
String expected = DeleteSessionCommand.class.getCanonicalName() | ||
+ "{targetSessionNumber=" + targetSessionNumber + "}"; | ||
assertEquals(expected, deleteSessionCommand.toString()); | ||
} | ||
} |
Oops, something went wrong.