Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AddRelationshipCommand to include checks for number of bioParents relationships (cannot be more than 2) #177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.UUID;

import javafx.collections.ObservableList;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
Expand Down Expand Up @@ -79,6 +80,45 @@ public CommandResult execute(Model model) throws CommandException {
if (isRoleBased) {
RoleBasedRelationship toAdd;
if (relationshipDescriptor.equalsIgnoreCase("Bioparents")) {
ObservableList<Relationship> allRelationships = model.getFilteredRelationshipList();
int originBioParentsCount = 0;
int targetBioParentsCount = 0;
for (Relationship r : allRelationships) {
if (r.getRelationshipDescriptor().equalsIgnoreCase("Bioparents")
&& (r.getPerson1().equals(fullOriginUuid) || r.getPerson2().equals(fullOriginUuid))) {
if (r.getPerson1().equals(fullOriginUuid)) {
RoleBasedRelationship r1 = (RoleBasedRelationship) r;
if (r1.getRole(fullOriginUuid).equals("child")) {
originBioParentsCount++;
}
} else if (r.getPerson2().equals(fullOriginUuid)) {
RoleBasedRelationship r2 = (RoleBasedRelationship) r;
if (r2.getRole(fullOriginUuid).equals("child")) {
originBioParentsCount++;
}
}
}
if (r.getRelationshipDescriptor().equalsIgnoreCase("Bioparents")
&& (r.getPerson1().equals(fullTargetUuid) || r.getPerson2().equals(fullTargetUuid))) {
if (r.getPerson1().equals(fullTargetUuid)) {
RoleBasedRelationship r1 = (RoleBasedRelationship) r;
if (r1.getRole(fullTargetUuid).equals("child")) {
targetBioParentsCount++;
}
} else if (r.getPerson2().equals(fullTargetUuid)) {
RoleBasedRelationship r2 = (RoleBasedRelationship) r;
if (r2.getRole(fullTargetUuid).equals("child")) {
targetBioParentsCount++;
}
}
}
}
if (originBioParentsCount >= 2) {
throw new CommandException("Sorry, " + originUuid + " already has 2 parent relationships");
}
if (targetBioParentsCount >= 2) {
throw new CommandException("Sorry, " + targetUuid + " already has 2 parent relationships");
}
toAdd = new BioParentsRelationship(fullOriginUuid, fullTargetUuid, rolePerson1, rolePerson2);
} else if (relationshipDescriptor.equalsIgnoreCase("Siblings")) {
toAdd = new SiblingRelationship(fullOriginUuid, fullTargetUuid, rolePerson1, rolePerson2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,72 @@ public void execute_hasDescriptorFalseInvalidRolebased_addsRelationship() {
+ "\nrelationtype and add it again.",
exception.getMessage());
}

@Test
public void execute_addThirdBioParentToPerson_throwsCommandException() {
Model model = new ModelManager();
AddressBook typicalPersonsAddressBook = TypicalPersonsUuid.getTypicalAddressBook();
model.setAddressBook(typicalPersonsAddressBook);
String originUuid = "0005";
String targetUuid = "0006";
String role1 = "child";
String role2 = "parent";
String relationshipDescriptor = "bioparents";
Relationship parentRelation2 = new RoleBasedRelationship(
UUID.fromString("00000000-0000-0000-0000-000000000001"),
UUID.fromString("00000000-0000-0000-0000-000000000005"), relationshipDescriptor,
role2, role1);
Relationship parentRelation3 = new RoleBasedRelationship(
UUID.fromString("00000000-0000-0000-0000-000000000005"),
UUID.fromString("00000000-0000-0000-0000-000000000002"), relationshipDescriptor,
role1, role2);
model.addRelationship(parentRelation2);
model.addRelationship(parentRelation3);
AddRelationshipCommand addRelationshipCommand =
new AddRelationshipCommand(originUuid, targetUuid, relationshipDescriptor, role1, role2);
assertCommandFailure(addRelationshipCommand, model,
"Sorry, 0005 already has 2 parent relationships");
}

@Test
public void execute_addPersonAsThirdBioParent_throwsCommandException() {
Model model = new ModelManager();
AddressBook typicalPersonsAddressBook = TypicalPersonsUuid.getTypicalAddressBook();
model.setAddressBook(typicalPersonsAddressBook);
String originUuid = "0006";
String targetUuid = "0005";
String role1 = "parent";
String role2 = "child";
String relationshipDescriptor = "bioparents";
Relationship parentRelation2 = new RoleBasedRelationship(
UUID.fromString("00000000-0000-0000-0000-000000000001"),
UUID.fromString("00000000-0000-0000-0000-000000000005"), relationshipDescriptor,
role1, role2);
Relationship parentRelation3 = new RoleBasedRelationship(
UUID.fromString("00000000-0000-0000-0000-000000000005"),
UUID.fromString("00000000-0000-0000-0000-000000000002"), relationshipDescriptor,
role2, role1);
model.addRelationship(parentRelation2);
model.addRelationship(parentRelation3);
AddRelationshipCommand addRelationshipCommand =
new AddRelationshipCommand(originUuid, targetUuid, relationshipDescriptor, role1, role2);
assertCommandFailure(addRelationshipCommand, model,
"Sorry, 0005 already has 2 parent relationships");
}

@Test
public void execute_addSecondBioParentToPerson_success() {
Model model = new ModelManager();
AddressBook typicalPersonsAddressBook = TypicalPersonsUuid.getTypicalAddressBook();
model.setAddressBook(typicalPersonsAddressBook);
String originUuid = "0005";
String targetUuid = "0006";
String role1 = "child";
String role2 = "parent";
String relationshipDescriptor = "bioparents";
AddRelationshipCommand addRelationshipCommand =
new AddRelationshipCommand(originUuid, targetUuid, relationshipDescriptor, role1, role2);
CommandResult result = Assertions.assertDoesNotThrow(() -> addRelationshipCommand.execute(model));
Assertions.assertEquals(AddRelationshipCommand.MESSAGE_ADD_RELATIONSHIP_SUCCESS, result.getFeedbackToUser());
}
}
Loading