forked from AY2122S1-CS2103T-W15-1/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.
Adding DeleteAllMarks Functionality.
- Loading branch information
1 parent
4650780
commit 59e6af6
Showing
7 changed files
with
230 additions
and
2 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/main/java/seedu/address/logic/commands/DeleteAllMarkCommand.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,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); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/java/seedu/address/logic/parser/DeleteAllMarkCommandParser.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,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); | ||
} | ||
|
||
} |
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
90 changes: 90 additions & 0 deletions
90
src/test/java/seedu/address/logic/commands/DeleteAllMarkCommandTest.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,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))); | ||
} | ||
|
||
} |
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