This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
forked from nus-cs2103-AY1920S1/addressbook-level3
-
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 #86 from hellopanda128/master
implement edit feature
- Loading branch information
Showing
22 changed files
with
553 additions
and
141 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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PASSWORD; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_USERNAME; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_CALLER; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATETIME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_LOCATION; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_INCIDENTS; | ||
|
||
import java.util.Collections; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
@@ -20,90 +19,95 @@ | |
import seedu.address.commons.util.CollectionUtil; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.incident.CallerNumber; | ||
import seedu.address.model.incident.Description; | ||
import seedu.address.model.incident.Incident; | ||
import seedu.address.model.incident.IncidentDateTime; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Password; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.person.Username; | ||
import seedu.address.model.tag.Tag; | ||
import seedu.address.model.vehicle.District; | ||
|
||
/** | ||
* Edits the details of an existing person in the address book. | ||
* Edits the details of an existing incident in the address book. | ||
*/ | ||
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 " | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the incident identified " | ||
+ "by the index number used in the displayed person list. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_NAME + "NAME] " | ||
+ "[" + PREFIX_PHONE + "PHONE] " | ||
+ "[" + PREFIX_EMAIL + "EMAIL] " | ||
+ "[" + PREFIX_USERNAME + "USERNAME] " | ||
+ "[" + PREFIX_PASSWORD + "PASSWORD] " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "[" + PREFIX_LOCATION + "DISTRICT] " | ||
+ "[" + PREFIX_CALLER + "CALLER NUMBER] " | ||
+ "[" + PREFIX_DATETIME + "DATETIME] " | ||
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION] " | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_PHONE + "91234567 " | ||
+ PREFIX_EMAIL + "[email protected]"; | ||
+ PREFIX_DATETIME + "01/10/2019 20:22 " | ||
+ PREFIX_CALLER + "91302402"; | ||
|
||
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."; | ||
public static final String MESSAGE_DUPLICATE_INCIDENT = "This incident already exists in the address book."; | ||
|
||
public static final String MESSAGE_EDIT_INCIDENT_SUCCESS = "Edited Incident: %1$s"; | ||
|
||
private final Index index; | ||
private final EditPersonDescriptor editPersonDescriptor; | ||
private final EditIncident editIncident; | ||
|
||
|
||
|
||
/** | ||
* @param index of the person in the filtered person list to edit | ||
* @param editPersonDescriptor details to edit the person with | ||
* @param editIncident details to edit the incident with | ||
*/ | ||
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) { | ||
public EditCommand(Index index, EditIncident editIncident) { | ||
requireNonNull(index); | ||
requireNonNull(editPersonDescriptor); | ||
requireNonNull(editIncident); | ||
|
||
this.index = index; | ||
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor); | ||
this.editIncident = new EditIncident(editIncident); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
List<Incident> listOfIncidents = model.getFilteredIncidentList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
if (index.getZeroBased() >= listOfIncidents.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_INCIDENT_INDEX); | ||
} | ||
|
||
Person personToEdit = lastShownList.get(index.getZeroBased()); | ||
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor); | ||
Incident incidentToEdit = listOfIncidents.get(index.getZeroBased()); | ||
Incident editedIncident = createEditedIncident(incidentToEdit, editIncident); | ||
|
||
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_PERSON); | ||
if (!incidentToEdit.isSameIncident(editedIncident) && model.hasIncident(editedIncident)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_INCIDENT); | ||
} | ||
|
||
model.setPerson(personToEdit, editedPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, editedPerson)); | ||
model.setIncident(incidentToEdit, editedIncident); | ||
model.updateFilteredIncidentList(PREDICATE_SHOW_ALL_INCIDENTS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_INCIDENT_SUCCESS, editedIncident)); | ||
} | ||
|
||
|
||
/** | ||
* Creates and returns a {@code Person} with the details of {@code personToEdit} | ||
* edited with {@code editPersonDescriptor}. | ||
*/ | ||
private static Person createEditedPerson(Person personToEdit, EditPersonDescriptor editPersonDescriptor) { | ||
assert personToEdit != null; | ||
private static Incident createEditedIncident(Incident incidentToEdit, EditIncident editIncident) { | ||
assert incidentToEdit != null; | ||
|
||
Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName()); | ||
Phone updatedPhone = editPersonDescriptor.getPhone().orElse(personToEdit.getPhone()); | ||
Email updatedEmail = editPersonDescriptor.getEmail().orElse(personToEdit.getEmail()); | ||
Username updatedUsername = editPersonDescriptor.getUsername().orElse(personToEdit.getUsername()); | ||
Password updatedPassword = editPersonDescriptor.getPassword().orElse(personToEdit.getPassword()); | ||
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags()); | ||
District updateDistrict = editIncident.getDistrict().orElse(incidentToEdit.getDistrict()); | ||
CallerNumber updateCaller = editIncident.getCaller().orElse(incidentToEdit.getCallerNumber()); | ||
IncidentDateTime updateDateTime = editIncident.getDateTime().orElse(incidentToEdit.getDateTime()); | ||
Description updateDesc = editIncident.getDesc().orElse(incidentToEdit.getDesc()); | ||
|
||
return new Person(updatedName, updatedPhone, updatedEmail, updatedTags, updatedUsername, updatedPassword); | ||
return new Incident(updateDistrict, updateDateTime, updateCaller, updateDesc); | ||
} | ||
|
||
@Override | ||
|
@@ -121,9 +125,89 @@ public boolean equals(Object other) { | |
// state check | ||
EditCommand e = (EditCommand) other; | ||
return index.equals(e.index) | ||
&& editPersonDescriptor.equals(e.editPersonDescriptor); | ||
&& editIncident.equals(e.editIncident); | ||
} | ||
|
||
/** | ||
* Stores the details to edit the incident with. Each non-empty field value will replace the corresponding | ||
* field value of the person. | ||
*/ | ||
public static class EditIncident { | ||
private District district; | ||
private IncidentDateTime dateTime; | ||
private CallerNumber caller; | ||
private Description desc; | ||
|
||
public EditIncident() {} | ||
|
||
public EditIncident(EditIncident toCopy) { | ||
setDistrict(toCopy.district); | ||
setDateTime(toCopy.dateTime); | ||
setCaller(toCopy.caller); | ||
setDesc(toCopy.desc); | ||
} | ||
|
||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(district, dateTime, caller, desc); | ||
} | ||
|
||
public void setDistrict(District district) { | ||
this.district = district; | ||
} | ||
|
||
public Optional<District> getDistrict() { | ||
return Optional.ofNullable(this.district); | ||
} | ||
|
||
public void setCaller(CallerNumber caller) { | ||
this.caller = caller; | ||
} | ||
|
||
public Optional<CallerNumber> getCaller() { | ||
return Optional.ofNullable(this.caller); | ||
} | ||
|
||
public void setDateTime(IncidentDateTime dateTime) { | ||
this.dateTime = dateTime; | ||
} | ||
|
||
public Optional<IncidentDateTime> getDateTime() { | ||
return Optional.ofNullable(this.dateTime); | ||
} | ||
|
||
public void setDesc(Description desc) { | ||
this.desc = desc; | ||
} | ||
|
||
public Optional<Description> getDesc() { | ||
return Optional.ofNullable(this.desc); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditIncident)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditIncident e = (EditIncident) other; | ||
|
||
return getDistrict().equals(e.getDistrict()) | ||
&& getCaller().equals(e.getCaller()) | ||
&& getDateTime().equals(e.getDateTime()) | ||
&& getDesc().equals(e.getDesc()); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
/** | ||
* Stores the details to edit the person with. Each non-empty field value will replace the | ||
* corresponding field value of the person. | ||
|
@@ -136,6 +220,7 @@ public static class EditPersonDescriptor { | |
private Password password; | ||
private Set<Tag> tags; | ||
|
||
|
||
public EditPersonDescriptor() {} | ||
|
||
/** | ||
|
@@ -155,7 +240,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) { | |
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(name, phone, email, username, password, tags); | ||
return CollectionUtil.isAnyNonNull(name, phone, email, tags); | ||
} | ||
|
||
public void setName(Name name) { | ||
|
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
Oops, something went wrong.