Skip to content

Commit

Permalink
Merge branch 'master' into filter-command
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica2828 authored Oct 16, 2024
2 parents 61e1760 + 8dbf3fd commit 0b73932
Show file tree
Hide file tree
Showing 28 changed files with 904 additions and 167 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 @@ -15,6 +15,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_STUDENTID = "The person studentID provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Objects;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand All @@ -37,36 +36,36 @@ public class EditCommand extends Command {
public static final String COMMAND_WORD = "edit";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "by the studentId assigned to the corresponding student. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "Parameters: STUDENTID (must be a valid and existing 8-digit Student ID) "
+ "[" + PREFIX_STUDENTID + "STUDENTID] "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_COURSE + " COURSE] "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "Example: " + COMMAND_WORD + " 12345678 "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]";

public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edited Person: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Index index;
private final StudentId studentId;
private final EditPersonDescriptor editPersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param studentId of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
public EditCommand(StudentId studentId, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(studentId);
requireNonNull(editPersonDescriptor);

this.index = index;
this.studentId = studentId;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
}

Expand All @@ -75,11 +74,11 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Person personToEdit = lastShownList.stream()
.filter(person -> person.getStudentId().equals(studentId))
.findFirst()
.orElseThrow(() -> new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_STUDENTID));;

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
Expand Down Expand Up @@ -120,14 +119,14 @@ public boolean equals(Object other) {
}

EditCommand otherEditCommand = (EditCommand) other;
return index.equals(otherEditCommand.index)
return studentId.equals(otherEditCommand.studentId)
&& editPersonDescriptor.equals(otherEditCommand.editPersonDescriptor);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("studentId", studentId)
.add("editPersonDescriptor", editPersonDescriptor)
.toString();
}
Expand Down Expand Up @@ -165,7 +164,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, tag);
return CollectionUtil.isAnyNonNull(studentId, name, phone, email, address, course, tag);
}

public void setStudentId(StudentId studentId) {
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/seedu/address/logic/commands/GradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -36,15 +37,14 @@ public class GradeCommand extends Command {

public static final String MESSAGE_SUCCESS = "New grade added for %1$s";
public static final String MESSAGE_PERSON_NOT_FOUND = "This person does not exist in the address book";

public static final String DUPLICATE_MODULE = "The module %1$s already exists for this student";
public static final String MESSAGE_MODULE_NOT_FOUND = "The student does not take the module %1$s";

private final Module module;
private final StudentId studentId;
private final Grade grade;

/**
* Creates an AddCommand to add the specified {@code Person}
* Creates a GradeCommand to assign a grade to the specified {@code Module}
*/
public GradeCommand(StudentId studentId, Module module, Grade grade) {
requireNonNull(studentId);
Expand Down Expand Up @@ -74,13 +74,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_PERSON_NOT_FOUND);
}

ArrayList<Module> moduleGrades = person.getModuleGrades();
if (moduleGrades.contains(module)) {
throw new CommandException(String.format(DUPLICATE_MODULE, module));
ArrayList<Module> modules = person.getModules();
if (!modules.contains(module)) {
throw new CommandException(String.format(MESSAGE_MODULE_NOT_FOUND, module.value));
}

person.addModuleGrade(module, grade);

Person updatedPerson = person.setModuleGrade(module, grade);
model.setPerson(person, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, module));
}

Expand All @@ -102,7 +103,9 @@ public boolean equals(Object other) {
@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", module)
.add("studentId", studentId)
.add("module", module)
.add("toAdd", grade)
.toString();
}
}
103 changes: 103 additions & 0 deletions src/main/java/seedu/address/logic/commands/ModuleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

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

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Module;
import seedu.address.model.person.Person;
import seedu.address.model.person.StudentId;

/**
* Assigns a course-specific grade to a student.
*/
public class ModuleCommand extends Command {

public static final String COMMAND_WORD = "module";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a module to a student. "
+ "Parameters: "
+ PREFIX_STUDENTID + "ID "
+ PREFIX_MODULE + "MODULE "
+ "\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STUDENTID + "12345678 "
+ PREFIX_MODULE + "CS2103T ";

public static final String MESSAGE_SUCCESS = "New module added for Student %1$s";
public static final String MESSAGE_PERSON_NOT_FOUND = "This person does not exist in the address book";
public static final String MESSAGE_DUPLICATE_MODULE = "The module %1$s already exists for this student";

private final Module module;
private final StudentId studentId;

/**
* Creates a ModuleCommand to add the specified {@code Module}
*/
public ModuleCommand(StudentId studentId, Module module) {
requireNonNull(studentId);
requireNonNull(module);
this.module = module;
this.studentId = studentId;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

List<Person> lastShownList = model.getFilteredPersonList();

Person person = null;

for (Person p : lastShownList) {
if (p.getStudentId().equals(studentId)) {
person = p;
break;
}
}

if (person == null) {
throw new CommandException(MESSAGE_PERSON_NOT_FOUND);
}

ArrayList<Module> modules = person.getModules();
if (modules.contains(module)) {
throw new CommandException(String.format(MESSAGE_DUPLICATE_MODULE, module.value));
}

Person updatedPerson = person.addModule(module);
model.setPerson(person, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, studentId));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

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

ModuleCommand otherModuleCommand = (ModuleCommand) other;
return module.equals(otherModuleCommand.module);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("studentId", studentId)
.add("toAdd", module)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.GradeCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ModuleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -81,6 +82,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ModuleCommand.COMMAND_WORD:
return new ModuleCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.StudentId;


/**
Expand All @@ -32,10 +32,10 @@ public EditCommand parse(String args) throws ParseException {
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_COURSE, PREFIX_TAG);

Index index;
StudentId studentId;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
studentId = ParserUtil.parseStudentId(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}
Expand Down Expand Up @@ -72,22 +72,7 @@ public EditCommand parse(String args) throws ParseException {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
}

return new EditCommand(index, editPersonDescriptor);
return new EditCommand(studentId, editPersonDescriptor);
}

/*/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
/*private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws ParseException {
assert tags != null;
if (tags.isEmpty()) {
return Optional.empty();
}
Collection<String> tagSet = tags.size() == 1 && tags.contains("") ? Collections.emptySet() : tags;
return Optional.of(ParserUtil.parseTags(tagSet));
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import seedu.address.model.person.StudentId;

/**
* Parses input arguments and creates a new AddCommand object
* Parses input arguments and creates a new GradeCommand object
*/
public class GradeCommandParser implements Parser<GradeCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* Parses the given {@code String} of arguments in the context of the GradeCommand
* and returns a GradeCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public GradeCommand parse(String args) throws ParseException {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/logic/parser/ModuleCommandParser.java
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_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID;

import java.util.stream.Stream;

import seedu.address.logic.commands.ModuleCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Module;
import seedu.address.model.person.StudentId;

/**
* Parses input arguments and creates a new ModuleCommand object
*/
public class ModuleCommandParser implements Parser<ModuleCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ModuleCommand
* and returns a ModuleCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ModuleCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_MODULE);

if (!arePrefixesPresent(argMultimap, PREFIX_STUDENTID, PREFIX_MODULE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ModuleCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENTID, PREFIX_MODULE);
StudentId studentId = ParserUtil.parseStudentId(argMultimap.getValue(PREFIX_STUDENTID).get());
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get());

return new ModuleCommand(studentId, module);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
Loading

0 comments on commit 0b73932

Please sign in to comment.