forked from nus-cs2103-AY2021S2/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 #144 from yutingzou/command-grade
Add AddGrade, EditGrade, DeleteGrade and ListGrade commands
- Loading branch information
Showing
33 changed files
with
879 additions
and
49 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
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
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
88 changes: 88 additions & 0 deletions
88
src/main/java/seedu/address/logic/commands/gradecommands/DeleteGradeCommand.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,88 @@ | ||
package seedu.address.logic.commands.gradecommands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.grade.Grade; | ||
|
||
/** | ||
* Deletes a grade identified using it's displayed index from the grade list. | ||
*/ | ||
public class DeleteGradeCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "delete_grade"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the grade identified by the index number used in the displayed grade list.\n" | ||
+ "Parameters: INDEX (must be a positive integer)\n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
|
||
public static final String MESSAGE_DELETE_GRADE_SUCCESS = "Deleted Grade: %1$s"; | ||
public static final String MESSAGE_DELETE_GRADE_FAILURE = "Grade does" | ||
+ " not exists in grade list."; | ||
|
||
private final Index targetIndex; | ||
private final Grade toDelete; | ||
|
||
/** | ||
* Create {@code DeleteGradeCommand} with target index to delete. | ||
* @param targetIndex Target index of appointment to delete. | ||
*/ | ||
public DeleteGradeCommand(Index targetIndex) { | ||
requireNonNull(targetIndex); | ||
this.targetIndex = targetIndex; | ||
this.toDelete = null; | ||
} | ||
|
||
/** | ||
* Create {@code DeleteGradeCommand} with {@code Grade} to delete. | ||
* @param toDelete Grade to delete. | ||
*/ | ||
public DeleteGradeCommand(Grade toDelete) { | ||
requireNonNull(toDelete); | ||
this.targetIndex = null; | ||
this.toDelete = toDelete; | ||
} | ||
|
||
/** | ||
* Deletes grade if exists in grade list (Offer two ways to delete by | ||
* index or by grade) | ||
* @param model {@code Model} which the command should operate on. | ||
* @return Command Result indicating success or failure of delete operation | ||
* @throws CommandException | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
// Delete by grade | ||
if (targetIndex == null) { | ||
if (model.hasGrade(toDelete)) { | ||
model.deleteGrade(toDelete); | ||
return new CommandResult(MESSAGE_DELETE_GRADE_SUCCESS); | ||
} else { | ||
return new CommandResult(MESSAGE_DELETE_GRADE_FAILURE); | ||
} | ||
} else { | ||
// Delete by index | ||
try { | ||
model.removeGradeIndex(targetIndex.getZeroBased()); | ||
return new CommandResult(MESSAGE_DELETE_GRADE_SUCCESS); | ||
} catch (IndexOutOfBoundsException e) { | ||
return new CommandResult(MESSAGE_DELETE_GRADE_FAILURE); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DeleteGradeCommand // instanceof handles nulls | ||
&& targetIndex.equals(((DeleteGradeCommand) other).targetIndex)); // state check | ||
} | ||
} |
189 changes: 189 additions & 0 deletions
189
src/main/java/seedu/address/logic/commands/gradecommands/EditGradeCommand.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,189 @@ | ||
package seedu.address.logic.commands.gradecommands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADED_ITEM; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBJECT_NAME; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_GRADE; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.util.CollectionUtil; | ||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.grade.Grade; | ||
import seedu.address.model.grade.GradeEnum; | ||
import seedu.address.model.grade.GradedItem; | ||
import seedu.address.model.subject.SubjectName; | ||
|
||
public class EditGradeCommand extends Command { | ||
public static final String COMMAND_WORD = "edit_grade"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the grade identified " | ||
+ "by the index number used in the displayed grade list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_SUBJECT_NAME + "SUBJECT] " | ||
+ "[" + PREFIX_GRADED_ITEM + "GRADED ITEM] " | ||
+ "[" + PREFIX_GRADE + "GRADE]\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_SUBJECT_NAME + "Science " | ||
+ PREFIX_GRADED_ITEM + "Midterm" | ||
+ PREFIX_GRADE + "A"; | ||
|
||
public static final String MESSAGE_EDIT_GRADE_SUCCESS = "Edited Grade: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_GRADE = "This grade already exists."; | ||
|
||
private final Index index; | ||
private final EditGradeCommand.EditGradeDescriptor editGradeDescriptor; | ||
|
||
/** | ||
* @param index of the appointment in the filtered appointment list to edit | ||
*/ | ||
public EditGradeCommand( | ||
Index index, | ||
EditGradeCommand.EditGradeDescriptor editGradeDescriptor) { | ||
requireNonNull(index); | ||
requireNonNull(editGradeDescriptor); | ||
|
||
this.index = index; | ||
this.editGradeDescriptor = editGradeDescriptor; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Grade> lastShownList = model.getFilteredGradeList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_GRADE_DISPLAYED_INDEX); | ||
} | ||
|
||
Grade gradeToEdit = lastShownList.get(index.getZeroBased()); | ||
Grade editedGrade = createEditedGrade(gradeToEdit, editGradeDescriptor); | ||
|
||
if (!gradeToEdit.equals(editedGrade) && model.hasGrade(editedGrade)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_GRADE); | ||
} | ||
|
||
model.setGrade(gradeToEdit, editedGrade); | ||
model.updateFilteredGradeList(PREDICATE_SHOW_ALL_GRADE); | ||
return new CommandResult(String.format(MESSAGE_EDIT_GRADE_SUCCESS, editedGrade)); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Grade} with the details of {@code gradeToEdit} | ||
* edited with {@code editGradeDescriptor}. | ||
*/ | ||
private static Grade createEditedGrade( | ||
Grade gradeToEdit, | ||
EditGradeCommand.EditGradeDescriptor editGradeDescriptor) { | ||
assert gradeToEdit != null; | ||
|
||
SubjectName updatedSubjectName = editGradeDescriptor.getSubjectName() | ||
.orElse(gradeToEdit.getSubject()); | ||
GradedItem updatedGradedItem = | ||
editGradeDescriptor.getGradedItem().orElse(gradeToEdit.getGradedItem()); | ||
GradeEnum updatedGrade = | ||
editGradeDescriptor.getGrade().orElse(gradeToEdit.getGrade()); | ||
|
||
return new Grade(updatedSubjectName, updatedGradedItem, updatedGrade); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditGradeCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditGradeCommand e = (EditGradeCommand) other; | ||
return index.equals(e.index) | ||
&& editGradeDescriptor.equals(e.editGradeDescriptor); | ||
} | ||
|
||
|
||
/** | ||
* Stores the details to edit the grade with. Each non-empty field value will replace the | ||
* corresponding field value of the grade. | ||
*/ | ||
public static class EditGradeDescriptor { | ||
private SubjectName subjectName; | ||
private GradedItem gradedItem; | ||
private GradeEnum grade; | ||
|
||
public EditGradeDescriptor() {} | ||
|
||
/** | ||
* Copy constructor. | ||
*/ | ||
public EditGradeDescriptor(EditGradeCommand.EditGradeDescriptor toCopy) { | ||
setSubjectName(toCopy.subjectName); | ||
setGradedItem(toCopy.gradedItem); | ||
setGrade(toCopy.grade); | ||
} | ||
|
||
/** | ||
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(subjectName, gradedItem, grade); | ||
} | ||
|
||
public void setSubjectName(SubjectName subjectName) { | ||
this.subjectName = subjectName; | ||
} | ||
|
||
public Optional<SubjectName> getSubjectName() { | ||
return Optional.ofNullable(subjectName); | ||
} | ||
|
||
public void setGradedItem(GradedItem gradedItem) { | ||
this.gradedItem = gradedItem; | ||
} | ||
|
||
public Optional<GradedItem> getGradedItem() { | ||
return Optional.ofNullable(this.gradedItem); | ||
} | ||
|
||
public void setGrade(GradeEnum grade) { | ||
this.grade = grade; | ||
} | ||
|
||
public Optional<GradeEnum> getGrade() { | ||
return Optional.ofNullable(this.grade); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditGradeCommand.EditGradeDescriptor)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditGradeCommand.EditGradeDescriptor e = (EditGradeCommand.EditGradeDescriptor) other; | ||
|
||
return getSubjectName().equals(e.getSubjectName()) | ||
&& getGradedItem().equals(e.getGradedItem()) | ||
&& getGrade().equals(e.getGrade()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/seedu/address/logic/commands/gradecommands/ListGradeCommand.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,22 @@ | ||
package seedu.address.logic.commands.gradecommands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_GRADE; | ||
|
||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.CommandResult; | ||
import seedu.address.model.Model; | ||
|
||
public class ListGradeCommand extends Command { | ||
public static final String COMMAND_WORD = "list_grades"; | ||
|
||
public static final String MESSAGE_SUCCESS = "Listed all grades"; | ||
|
||
|
||
@Override | ||
public CommandResult execute(Model model) { | ||
requireNonNull(model); | ||
model.updateFilteredGradeList(PREDICATE_SHOW_ALL_GRADE); | ||
return new CommandResult(MESSAGE_SUCCESS); | ||
} | ||
} |
Oops, something went wrong.