Skip to content

Commit

Permalink
Merge pull request #96 from ybchen97/edit-policy-updates-person
Browse files Browse the repository at this point in the history
Edit policy updates person
  • Loading branch information
chaitanyabaranwal committed Oct 14, 2019
2 parents 07ada91 + afd9297 commit 6cbebbf
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_POLICY_INDEX;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Address;
import seedu.address.model.person.DateOfBirth;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;

import seedu.address.model.policy.Coverage;
import seedu.address.model.policy.Description;
import seedu.address.model.policy.EndAge;
import seedu.address.model.policy.Policy;
import seedu.address.model.policy.PolicyName;
import seedu.address.model.policy.Price;
import seedu.address.model.policy.StartAge;
import seedu.address.model.tag.Tag;

/**
* Command to assign a new policy to a person.
Expand Down Expand Up @@ -86,50 +70,14 @@ public CommandResult execute(Model model) throws CommandException {
person.getName(), policy.getName()));
}

Person assignedPerson = createAssignedPerson(person, policy);
Person assignedPerson = Person.createPersonWithPolicy(person, Policy.createCopy(policy));

model.setPerson(person, assignedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_ASSIGN_POLICY_SUCCESS,
policy.getName(), assignedPerson.getName()));
}

/**
* Creates and returns a {@code Policy} with the details of {@code policyToCopy}.
*/
private static Policy copyPolicy(Policy policyToCopy) {
requireNonNull(policyToCopy);

PolicyName name = policyToCopy.getName();
Description description = policyToCopy.getDescription();
Coverage coverage = policyToCopy.getCoverage();
Price price = policyToCopy.getPrice();
StartAge startAge = policyToCopy.getStartAge();
EndAge endAge = policyToCopy.getEndAge();
Set<Tag> criteria = policyToCopy.getCriteria();
Set<Tag> tags = policyToCopy.getTags();

return new Policy(name, description, coverage, price, startAge, endAge, criteria, tags);
}

/**
* Creates a returns a {@code Person} with the details of {@code person}
* with an additional policy {@code policy}.
*/
private static Person createAssignedPerson(Person person, Policy policy) {
Name name = person.getName();
Nric nric = person.getNric();
Phone phone = person.getPhone();
Email email = person.getEmail();
Address address = person.getAddress();
DateOfBirth dateOfBirth = person.getDateOfBirth();
Set<Policy> updatedPolicies = new HashSet<Policy>(person.getPolicies());
updatedPolicies.add(copyPolicy(policy));
Set<Tag> tags = person.getTags();

return new Person(name, nric, phone, email, address, dateOfBirth, updatedPolicies, tags);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.policy.Policy;

/**
Expand Down Expand Up @@ -41,6 +42,15 @@ public CommandResult execute(Model model) throws CommandException {

Policy policyToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePolicy(policyToDelete);

// Update persons with the edited policy
for (Person p : model.getAddressBook().getPersonList()) {
if (p.hasPolicy(policyToDelete)) {
Person editedPerson = Person.createPersonWithoutPolicy(p, policyToDelete);
model.setPerson(p, editedPerson);
}
}

return new CommandResult(String.format(MESSAGE_DELETE_POLICY_SUCCESS, policyToDelete));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.policy.Coverage;
import seedu.address.model.policy.Description;
import seedu.address.model.policy.EndAge;
Expand Down Expand Up @@ -88,6 +89,16 @@ public CommandResult execute(Model model) throws CommandException {

model.setPolicy(policyToEdit, editedPolicy);
model.updateFilteredPolicyList(PREDICATE_SHOW_ALL_POLICIES);

// Update persons with the edited policy
for (Person p : model.getAddressBook().getPersonList()) {
if (p.hasPolicy(policyToEdit)) {
Person policyRemoved = Person.createPersonWithoutPolicy(p, policyToEdit);
Person editedPerson = Person.createPersonWithPolicy(policyRemoved, editedPolicy);
model.setPerson(p, editedPerson);
}
}

return new CommandResult(String.format(MESSAGE_EDIT_POLICY_SUCCESS, editedPolicy));
}

Expand All @@ -98,7 +109,6 @@ public CommandResult execute(Model model) throws CommandException {
private static Policy createEditedPolicy(Policy policyToEdit, EditPolicyDescriptor editPolicyDescriptor) {
assert policyToEdit != null;

// TODO: Edit policy details
PolicyName updatedName = editPolicyDescriptor.getName().orElse(policyToEdit.getName());
Description updatedDescription = editPolicyDescriptor.getDescription().orElse(policyToEdit.getDescription());
Coverage updatedCoverage = editPolicyDescriptor.getCoverage().orElse(policyToEdit.getCoverage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_POLICY_INDEX;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Address;
import seedu.address.model.person.DateOfBirth;
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;

import seedu.address.model.policy.Policy;
import seedu.address.model.tag.Tag;

/**
* Command to assign a new policy to a person.
Expand Down Expand Up @@ -80,32 +70,14 @@ public CommandResult execute(Model model) throws CommandException {
person.getName(), policy.getName()));
}

Person assignedPerson = createUnassignedPerson(person, policy);
Person assignedPerson = Person.createPersonWithoutPolicy(person, policy);

model.setPerson(person, assignedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_UNASSIGN_POLICY_SUCCESS,
policy.getName(), assignedPerson.getName()));
}

/**
* Creates a returns a {@code Person} with the details of {@code person}
* with an additional policy {@code policy}.
*/
private static Person createUnassignedPerson(Person person, Policy policy) {
Name name = person.getName();
Nric nric = person.getNric();
Phone phone = person.getPhone();
Email email = person.getEmail();
Address address = person.getAddress();
DateOfBirth dateOfBirth = person.getDateOfBirth();
Set<Policy> updatedPolicies = new HashSet<Policy>(person.getPolicies());
updatedPolicies.remove(policy);
Set<Tag> tags = person.getTags();

return new Person(name, nric, phone, email, address, dateOfBirth, updatedPolicies, tags);
}

@Override
public boolean equals(Object other) {
// short circuit if same object
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public ObservableList<Policy> getFilteredPolicyList() {
return filteredPolicies;
}

// TODO: Show list of policies instead.
@Override
public void updateFilteredPolicyList(Predicate<Policy> predicate) {
requireNonNull(predicate);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,48 @@ public Person(Name name, Nric nric, Phone phone, Email email, Address address, D
this.tags.addAll(tags);
}

/**
* Creates a new copy of the person {@code toCopy} with the policy {@code toAdd}.
* @param toCopy Person to be copied
* @param toAdd Policy to be added to the copied person
* @return Copy of the person with the added policy
*/
public static Person createPersonWithPolicy(Person toCopy, Policy toAdd) {
Set<Policy> updatedPolicies = new HashSet<>(toCopy.getPolicies());
updatedPolicies.add(toAdd);
return new Person(
toCopy.getName(),
toCopy.getNric(),
toCopy.getPhone(),
toCopy.getEmail(),
toCopy.getAddress(),
toCopy.getDateOfBirth(),
updatedPolicies,
new HashSet<>(toCopy.getTags())
);
}

/**
* Creates a new copy of the person {@code toCopy} without the policy {@code toAdd}.
* @param toCopy Person to be copied
* @param toRemove Policy to be removed from the copied person
* @return Copy of the person with policy removed
*/
public static Person createPersonWithoutPolicy(Person toCopy, Policy toRemove) {
Set<Policy> updatedPolicies = new HashSet<>(toCopy.getPolicies());
updatedPolicies.remove(toRemove);
return new Person(
toCopy.getName(),
toCopy.getNric(),
toCopy.getPhone(),
toCopy.getEmail(),
toCopy.getAddress(),
toCopy.getDateOfBirth(),
updatedPolicies,
new HashSet<>(toCopy.getTags())
);
}

public Name getName() {
return name;
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/model/policy/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ public Policy(PolicyName name, Description description, Coverage coverage, Price
this.tags.addAll(tags);
}

/**
* Creates a copy of the policy {@code toCopy}.
*/
public static Policy createCopy(Policy toCopy) {
return new Policy(
toCopy.getName(),
toCopy.getDescription(),
toCopy.getCoverage(),
toCopy.getPrice(),
toCopy.getStartAge(),
toCopy.getEndAge(),
new HashSet<>(toCopy.getCriteria()),
new HashSet<>(toCopy.getTags())
);
}

public PolicyName getName() {
return name;
}
Expand Down

0 comments on commit 6cbebbf

Please sign in to comment.