Skip to content
This repository has been archived by the owner on Apr 13, 2020. It is now read-only.

Commit

Permalink
Merge pull request #86 from hellopanda128/master
Browse files Browse the repository at this point in the history
implement edit feature
  • Loading branch information
meiannn authored Oct 16, 2019
2 parents 412b402 + bc98421 commit ca0af94
Show file tree
Hide file tree
Showing 22 changed files with 553 additions and 141 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class Messages {
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_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_INVALID_INCIDENT_INDEX = "The incident index provided is invalid";
public static final String MESSAGE_INCIDENTS_LISTED_OVERVIEW = "%1$d incidents listed!";

}
175 changes: 130 additions & 45 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
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;
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -136,6 +220,7 @@ public static class EditPersonDescriptor {
private Password password;
private Set<Tag> tags;


public EditPersonDescriptor() {}

/**
Expand All @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class CliSyntax {
public static final Prefix PREFIX_ID = new Prefix("id/");
public static final Prefix PREFIX_DESC = new Prefix("desc/");
public static final Prefix PREFIX_CALLER_NUMBER = new Prefix("c/");

public static final Prefix PREFIX_CALLER = new Prefix("c/");
public static final Prefix PREFIX_DATETIME = new Prefix("dt/");
public static final Prefix PREFIX_LOCATION = new Prefix("l/");
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
}
37 changes: 24 additions & 13 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
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.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 java.util.Collection;
import java.util.Collections;
Expand All @@ -16,7 +14,7 @@

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.commands.EditCommand.EditIncident;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

Expand All @@ -33,8 +31,7 @@ public class EditCommandParser implements Parser<EditCommand> {
public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_USERNAME,
PREFIX_PASSWORD, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_CALLER, PREFIX_LOCATION, PREFIX_DATETIME, PREFIX_DESCRIPTION);

Index index;

Expand All @@ -44,7 +41,8 @@ public EditCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe);
}

EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
EditIncident editIncident = new EditIncident();
/*EditPersonDescriptor editPersonDescriptor = new EditPersonDescriptor();
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()));
}
Expand All @@ -59,14 +57,27 @@ public EditCommand parse(String args) throws ParseException {
}
if (argMultimap.getValue(PREFIX_PASSWORD).isPresent()) {
editPersonDescriptor.setPassword(ParserUtil.parsePassword(argMultimap.getValue(PREFIX_PASSWORD).get()));
} */
if (argMultimap.getValue(PREFIX_CALLER).isPresent()) {
editIncident.setCaller(ParserUtil.parseCallerNumber(argMultimap.getValue(PREFIX_CALLER).get()));
}
if (argMultimap.getValue(PREFIX_LOCATION).isPresent()) {
editIncident.setDistrict(ParserUtil.parseLocation(argMultimap.getValue(PREFIX_LOCATION).get()));
}
if (argMultimap.getValue(PREFIX_DATETIME).isPresent()) {
editIncident.setDateTime(ParserUtil.parseDateTime(argMultimap.getValue(PREFIX_DATETIME).get()));
}
if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
editIncident.setDesc(ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

/*parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
}
}*/

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

/**
Expand Down
Loading

0 comments on commit ca0af94

Please sign in to comment.