Skip to content

Commit

Permalink
Adding DeleteAllMarks Functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliussneezer04 committed Oct 26, 2021
1 parent 4650780 commit 59e6af6
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS;

import java.util.ArrayList;
import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.student.Student;
import seedu.address.model.student.StudentMark;

/**
* Deletes all marks of an existing student in the ClassMATE.
*/
public class DeleteAllMarkCommand extends Command {

public static final String COMMAND_WORD = "deleteam";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Deletes All of Student's Mark\n"
+ "Example: " + COMMAND_WORD + " 1 ";

public static final String MESSAGE_DELETE_ALL_MARK_STUDENT_SUCCESS = "Deleted All Marks of Student: %1$s";
public static final String MESSAGE_NO_MARKS = "No Marks to Delete!";
public static final String MESSAGE_DUPLICATE_STUDENT = "This student already exists in the ClassMATE.";

private final Index index;

/**
* @param index of the student in the filtered student list to edit
*/
public DeleteAllMarkCommand(Index index) {
requireNonNull(index);

this.index = index;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Student> lastShownList = model.getFilteredStudentList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX);
}

Student studentToEdit = lastShownList.get(index.getZeroBased());
Student editedStudent = deleteAllStudentMark(studentToEdit);

if (!studentToEdit.isSameStudent(editedStudent) && model.hasStudent(editedStudent)) {
throw new CommandException(MESSAGE_DUPLICATE_STUDENT);
}

model.setStudent(studentToEdit, editedStudent);
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS);
return new CommandResult(String.format(MESSAGE_DELETE_ALL_MARK_STUDENT_SUCCESS, editedStudent));
}

/**
* Creates and returns a {@code Student} with all the marks deleted.
*/
private static Student deleteAllStudentMark(Student studentToEdit) throws CommandException {
assert studentToEdit != null;
if (studentToEdit.getMarks().isEmpty()) {
throw new CommandException(MESSAGE_NO_MARKS);
}
List<StudentMark> updatedMarks = new ArrayList<>();
return new Student(
studentToEdit.getName(),
studentToEdit.getPhone(),
studentToEdit.getEmail(),
studentToEdit.getAddress(),
studentToEdit.getClassCode(),
studentToEdit.getTags(),
updatedMarks);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof DeleteAllMarkCommand)) {
return false;
}

// state check
DeleteAllMarkCommand e = (DeleteAllMarkCommand) other;
return index.equals(e.index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import seedu.address.model.student.StudentMark;

/**
* Edits the details of an existing student in the ClassMATE.
* Deletes the latest mark of an existing student in the ClassMATE.
*/
public class DeleteLastMarkCommand extends Command {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/parser/ClassmateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.logic.commands.AddStudentCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteAllMarkCommand;
import seedu.address.logic.commands.DeleteClassCommand;
import seedu.address.logic.commands.DeleteGroupCommand;
import seedu.address.logic.commands.DeleteLastMarkCommand;
Expand Down Expand Up @@ -82,6 +83,9 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteLastMarkCommand.COMMAND_WORD:
return new DeleteLastMarkCommandParser().parse(arguments);

case DeleteAllMarkCommand.COMMAND_WORD:
return new DeleteAllMarkCommandParser().parse(arguments);

case ViewClassCommand.COMMAND_WORD:
return new ViewClassCommandParser().parse(arguments);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.DeleteAllMarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DeleteLastMarkCommand object
*/
public class DeleteAllMarkCommandParser implements Parser<DeleteAllMarkCommand> {

/**
* Parses the given {@code String} of arguments in the context of the DeleteLastMarkCommand
* and returns an DeleteAllMarkCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteAllMarkCommand parse(String args) throws ParseException {
requireNonNull(args);
Index index;
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args);

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteAllMarkCommand.MESSAGE_USAGE), pe);
}

return new DeleteAllMarkCommand(index);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class DeleteLastMarkCommandParser implements Parser<DeleteLastMarkCommand

/**
* Parses the given {@code String} of arguments in the context of the DeleteLastMarkCommand
* and returns an DeleteLastMarkCommadnd object for execution.
* and returns an DeleteLastMarkCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DeleteLastMarkCommand parse(String args) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showStudentAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_STUDENT;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_STUDENT;
import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_STUDENT;
import static seedu.address.testutil.TypicalStudents.ALICE;
import static seedu.address.testutil.TypicalStudents.getTypicalClassmate;

import org.junit.jupiter.api.Test;

import seedu.address.model.Classmate;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.student.Student;
import seedu.address.testutil.StudentBuilder;

/**
* Contains integration tests (interaction with the Model) and unit tests for DeleteAllMarkCommand.
*/
public class DeleteAllMarkCommandTest {

private Model model = new ModelManager(getTypicalClassmate(), new UserPrefs());

@Test
public void execute_indexSpecifiedUnfilteredList_success() {
DeleteAllMarkCommand deleteAllMarkCommand = new DeleteAllMarkCommand(INDEX_FIRST_STUDENT);
Student finalStudent = new StudentBuilder(ALICE).build();

String expectedMessage = String
.format(DeleteAllMarkCommand.MESSAGE_DELETE_ALL_MARK_STUDENT_SUCCESS, finalStudent);
expectedMessage = expectedMessage.substring(0, expectedMessage.length() - 18);

Model expectedModel = new ModelManager(new Classmate(model.getClassmate()), new UserPrefs());

assertCommandSuccess(deleteAllMarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() {
showStudentAtIndex(model, INDEX_FIRST_STUDENT);

Student studentInFilteredList = model.getFilteredStudentList().get(INDEX_FIRST_STUDENT.getZeroBased());
Student editedStudent = new StudentBuilder(studentInFilteredList).build();
DeleteAllMarkCommand deleteAllMarkCommand = new DeleteAllMarkCommand(INDEX_FIRST_STUDENT);

String expectedMessage =
String.format(DeleteAllMarkCommand.MESSAGE_DELETE_ALL_MARK_STUDENT_SUCCESS, editedStudent);
expectedMessage = expectedMessage.substring(0, expectedMessage.length() - 18);

Model expectedModel = new ModelManager(new Classmate(model.getClassmate()), new UserPrefs());
expectedModel.setStudent(model.getFilteredStudentList().get(0), editedStudent);

assertCommandSuccess(deleteAllMarkCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_deleteEmptyMarksFilteredList_failure() {
DeleteAllMarkCommand deleteAllMarkCommand = new DeleteAllMarkCommand(INDEX_THIRD_STUDENT);

assertCommandFailure(deleteAllMarkCommand, model, DeleteAllMarkCommand.MESSAGE_NO_MARKS);
}

@Test
public void equals() {
final DeleteAllMarkCommand standardCommand = new DeleteAllMarkCommand(INDEX_FIRST_STUDENT);

// same values -> returns true
DeleteAllMarkCommand commandWithSameValues = new DeleteAllMarkCommand(INDEX_FIRST_STUDENT);
assertTrue(standardCommand.equals(commandWithSameValues));

// same object -> returns true
assertTrue(standardCommand.equals(standardCommand));

// null -> returns false
assertFalse(standardCommand.equals(null));

// different types -> returns false
assertFalse(standardCommand.equals(new ClearCommand()));

// different index -> returns false
assertFalse(standardCommand.equals(new DeleteAllMarkCommand(INDEX_SECOND_STUDENT)));
}

}
1 change: 1 addition & 0 deletions src/test/java/seedu/address/testutil/TypicalIndexes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TypicalIndexes {
public static final Index INDEX_FIRST_STUDENT = Index.fromOneBased(1);
public static final Index INDEX_SECOND_STUDENT = Index.fromOneBased(2);
public static final Index INDEX_THIRD_STUDENT = Index.fromOneBased(3);
public static final Index INDEX_FOURTH_STUDENT = Index.fromOneBased(4);
public static final Index INDEX_FIRST_TUTORIALCLASS = Index.fromOneBased(1);
public static final Index INDEX_SECOND_TUTORIALCLASS = Index.fromOneBased(2);
}

0 comments on commit 59e6af6

Please sign in to comment.