forked from AY2425S1-CS2103T-F15-2/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.
Merge branch 'master' into filter-command
- Loading branch information
Showing
28 changed files
with
904 additions
and
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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)) { | ||
|
@@ -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(); | ||
} | ||
|
@@ -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) { | ||
|
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
103 changes: 103 additions & 0 deletions
103
src/main/java/seedu/address/logic/commands/ModuleCommand.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,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(); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/logic/parser/ModuleCommandParser.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,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()); | ||
} | ||
|
||
} |
Oops, something went wrong.