Skip to content

Commit

Permalink
Merge pull request #144 from yutingzou/command-grade
Browse files Browse the repository at this point in the history
Add AddGrade, EditGrade, DeleteGrade and ListGrade commands
  • Loading branch information
yutingzou committed Mar 28, 2021
2 parents f9b4b08 + d2fbf88 commit 3dc6c8e
Show file tree
Hide file tree
Showing 33 changed files with 879 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
try {
gradeBookOptional = storage.readGradeBook();
if (!gradeBookOptional.isPresent()) {
logger.info(APPOINTMENT_BOOK_NOT_FOUND);
logger.info(GRADE_BOOK_NOT_FOUND);
}
initialGrades =
gradeBookOptional.orElseGet(SampleDataUtil::getSampleGradeBook);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public class Messages {
public static final String MESSAGE_INVALID_APPOINTMENT_DISPLAYED_INDEX =
"The appointment index provided is invalid";
public static final String MESSAGE_APPOINTMENT_LISTED_OVERVIEW = "%1$d appointments listed!";
public static final String MESSAGE_INVALID_GRADE_DISPLAYED_INDEX =
"The grade index provided is invalid";
//public static final String MESSAGE_GRADE_LISTED_OVERVIEW = "%1$d grades listed!";
}
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Person;

/**
Expand Down Expand Up @@ -42,6 +43,11 @@ public interface Logic {
*/
ObservableList<Appointment> getFilteredAppointmentList();

/**
* Returns an unmodifiable view of the filtered list of grades
*/
ObservableList<Grade> getFilteredGradeList();

/**
* Returns the user prefs' address book file path.
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;

Expand Down Expand Up @@ -72,6 +73,11 @@ public ObservableList<Appointment> getFilteredAppointmentList() {
return model.getFilteredAppointmentList();
}

@Override
public ObservableList<Grade> getFilteredGradeList() {
return model.getFilteredGradeList();
}

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
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 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;

/**
* Adds a grade to the grade list.
*/
public class AddGradeCommand extends Command {

public static final String COMMAND_WORD = "add_grade";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a grade to the address book. ";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a grade to the grade book. "
+ "Parameters: "
+ PREFIX_SUBJECT_NAME + "SUBJECT NAME "
+ PREFIX_GRADED_ITEM + "GRADED ITEM "
+ PREFIX_GRADE + "GRADE\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_SUBJECT_NAME + "Science "
+ PREFIX_GRADED_ITEM + "Midterm "
+ PREFIX_GRADE + "A\n";
public static final String MESSAGE_SUCCESS = "New grade added: %1$s";
public static final String MESSAGE_DUPLICATE_GRADE = "This grade already exists in the grade book";

Expand Down Expand Up @@ -43,4 +57,5 @@ public boolean equals(Object other) {
|| (other instanceof AddGradeCommand // instanceof handles nulls
&& toAdd.equals(((AddGradeCommand) other).toAdd));
}

}
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
}
}
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());
}
}
}
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);
}
}
Loading

0 comments on commit 3dc6c8e

Please sign in to comment.