Skip to content

Commit

Permalink
Merge pull request #126 from ldinghan/branch-feature-session
Browse files Browse the repository at this point in the history
Add delete session command
  • Loading branch information
ldinghan authored Oct 30, 2023
2 parents 6a0c74e + c9366a6 commit 5ae46ca
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Messages {
public static final String MESSAGE_INVALID_DATE_TIME = "The date or time provided is invalid";
public static final String MESSAGE_INVALID_CONSULTATION_DISPLAYED_INDEX =
"The consultation index provided is invalid";
public static final String MESSAGE_SESSION_NOT_FOUND = "The session number provided is invalid";
public static final String MESSAGE_INVALID_TASK_DISPLAYED_INDEX = "The task index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_TASKS_LISTED_OVERVIEW = "%1$d tasks listed!";
Expand Down
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();
}
}
30 changes: 19 additions & 11 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import seedu.address.logic.commands.DeleteCommentCommand;
import seedu.address.logic.commands.DeleteConsultationCommand;
import seedu.address.logic.commands.DeleteGradeCommand;
import seedu.address.logic.commands.DeleteSessionCommand;
import seedu.address.logic.commands.DeleteTaskCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommentCommand;
Expand Down Expand Up @@ -109,6 +110,24 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

// SESSIONS

case CreateSessionCommand.COMMAND_WORD:
return new CreateSessionCommandParser().parse(arguments);

case UpdateSessionRemarkCommand.COMMAND_WORD:
return new UpdateSessionRemarkCommandParser().parse(arguments);

case DeleteSessionCommand.COMMAND_WORD:
return new DeleteSessionCommandParser().parse(arguments);

case TakeAttendanceCommand.COMMAND_WORD:
return new TakeAttendanceCommandParser().parse(arguments);

case ViewAttendanceCommand.COMMAND_WORD:
return new ViewAttendanceCommandParser().parse(arguments);


// CONSULTATIONS

case CreateConsultCommand.COMMAND_WORD:
Expand All @@ -117,9 +136,6 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteConsultationCommand.COMMAND_WORD:
return new DeleteConsultationCommandParser().parse(arguments);

case CreateSessionCommand.COMMAND_WORD:
return new CreateSessionCommandParser().parse(arguments);

case AddToConsultCommand.COMMAND_WORD:
return new AddToConsultCommandParser().parse(arguments);

Expand All @@ -143,14 +159,6 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteCommentCommand.COMMAND_WORD:
return new DeleteCommentCommandParser().parse(arguments);

case TakeAttendanceCommand.COMMAND_WORD:
return new TakeAttendanceCommandParser().parse(arguments);

case UpdateSessionRemarkCommand.COMMAND_WORD:
return new UpdateSessionRemarkCommandParser().parse(arguments);

case ViewAttendanceCommand.COMMAND_WORD:
return new ViewAttendanceCommandParser().parse(arguments);

case ViewAssignmentsCommand.COMMAND_WORD:
return new ViewAssignmentsCommandParser().parse(arguments);
Expand Down
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());
}
}
7 changes: 6 additions & 1 deletion src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ public interface Model {
Session findSessionBySessionNumber(SessionNumber sessionNumber);

/**
* Adds the given sesssion.
* Adds the given session.
*/
void addSession(Session session);

/**
* Deletes the given session.
*/
void deleteSession(Session session);

/**
* Returns true if a session with the same identity as {@code session} exists in the session list.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ public Session findSessionBySessionNumber(SessionNumber sessionNumber) {
public void addSession(Session session) {
requireNonNull(session);
sessionList.addSession(session);
updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS);
updateFilteredSessionList(PREDICATE_SHOW_ALL_SESSIONS);
}

@Override
public void deleteSession(Session session) {
requireNonNull(session);
sessionList.removeSession(session);
updateFilteredSessionList(PREDICATE_SHOW_ALL_SESSIONS);
}

@Override
Expand Down
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());
}
}
Loading

0 comments on commit 5ae46ca

Please sign in to comment.