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

Add functionality Force #102

Merged
merged 16 commits into from
Oct 17, 2024
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
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ public class Messages {
public static final String MESSAGE_TAG_NOT_FOUND_IN_CONTACT = "Some tags were not found in the person's tag list.";
public static final String MESSAGE_ADD_TAG_SUCCESS = "Added tag(s) %1$s to %2$s.";
public static final String MESSAGE_ADD_WEDDING_SUCCESS = "Added wedding(s) %1$s to %2$s.";
public static final String MESSAGE_REMOVE_WEDDING_SUCCESS = "Removed wedding(s) %1$s from %2$s.";
public static final String MESSAGE_WEDDING_NOT_FOUND = "One or more specified weddings do not exist in "
+ "the Wedlinker.";
public static final String MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT = "Some weddings were not found in "
+ "the person's wedding list.";
public static final String MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT = "Use f/ to force the assignment of wedding(s)."
+ " This will create the required Wedding objects.";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static seedu.address.logic.Messages.MESSAGE_ADD_WEDDING_SUCCESS;
import static seedu.address.logic.Messages.MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT;
import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.logic.Messages.MESSAGE_WEDDING_NOT_FOUND;

Expand Down Expand Up @@ -33,6 +34,7 @@ public class AssignWeddingCommand extends Command {

private final Index index;
private final HashSet<Wedding> weddingsToAdd;
private boolean force = false;

/**
* Constructs a {@code AssignWedding} Command to add weddings to a person.
Expand All @@ -44,6 +46,18 @@ public AssignWeddingCommand(Index index, HashSet<Wedding> weddingsToAdd) {
this.weddingsToAdd = weddingsToAdd;
}

/**
* Constructs a {@code AssignWedding} Command to add weddings to a person with the force flag.
* @param index The index of the person in the person list.
* @param weddingsToAdd The list of weddings to be added.
* @param force Whether the command should force the assignment by creating the Wedding object.
*/
public AssignWeddingCommand(Index index, HashSet<Wedding> weddingsToAdd, boolean force) {
this.index = index;
this.weddingsToAdd = weddingsToAdd;
this.force = force;
}

/**
* Generates a command execution success message showing the added weddings and the person.
*
Expand All @@ -69,7 +83,13 @@ public CommandResult execute(Model model) throws CommandException {

for (Wedding wedding : weddingsToAdd) {
if (!model.hasWedding(wedding)) {
throw new CommandException(MESSAGE_WEDDING_NOT_FOUND);
if (this.force) {
CreateWeddingCommand newWeddingCommand = new CreateWeddingCommand(wedding);
newWeddingCommand.execute(model);
} else {
throw new CommandException(
MESSAGE_WEDDING_NOT_FOUND + "\n" + MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.commands;

import static seedu.address.logic.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static seedu.address.logic.Messages.MESSAGE_REMOVE_WEDDING_SUCCESS;
import static seedu.address.logic.Messages.MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT;

import java.util.HashSet;
Expand All @@ -19,7 +20,6 @@
*/
public class UnassignWeddingCommand extends Command {
public static final String COMMAND_WORD = "unassign-wedding";
public static final String MESSAGE_REMOVE_WEDDING_SUCCESS = "Removed wedding(s) %1$s from %2$s.";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Removes one or multiple weddings from the person identified "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FORCE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_WEDDING;

import java.util.HashSet;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class AssignWeddingCommandParser implements Parser<AssignWeddingCommand>
public AssignWeddingCommand parse(String args) throws ParseException {
requireNonNull(args);

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_WEDDING);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_WEDDING, PREFIX_FORCE);
Index index;

if (!arePrefixesPresent(argMultimap, PREFIX_WEDDING)) {
Expand Down Expand Up @@ -63,7 +64,12 @@ public AssignWeddingCommand parse(String args) throws ParseException {
.map(Wedding::new)
.collect(Collectors.toList()));

return new AssignWeddingCommand(index, weddings);
if (arePrefixesPresent(argMultimap, PREFIX_FORCE)) {
return new AssignWeddingCommand(index, weddings, true);
} else {
return new AssignWeddingCommand(index, weddings);
}

}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_TAG = new Prefix("t/");
public static final Prefix PREFIX_WEDDING = new Prefix("w/");
public static final Prefix PREFIX_FORCE = new Prefix("f/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public EditCommand parse(String args) throws ParseException {
if (argMultimap.getValue(PREFIX_ADDRESS).isPresent()) {
editPersonDescriptor.setAddress(ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).orElse("")));
}

parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);

parseWeddingsForEdit(argMultimap.getAllValues(PREFIX_WEDDING)).ifPresent(editPersonDescriptor::setWeddings);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ public void setPersons(List<Person> persons) {
this.persons.setPersons(persons);
}

/**
* Replaces the contents of the tag list with {@code tags}.
* {@code tags} must not contain duplicate tags.
*/
public void setTags(List<Tag> tags) {
this.tags.setTags(tags);
}

/**
* Replaces the contents of the wedding list with {@code weddings}.
* {@code weddings} must not contain duplicate tags.
Expand Down Expand Up @@ -189,6 +181,14 @@ public void setTag(Tag target, Tag editedTag) {
tags.setTag(target, editedTag);
}

/**
* Replaces the contents of the tag list with {@code tags}.
* {@code tags} must not contain duplicate tags.
*/
public void setTags(List<Tag> tags) {
this.tags.setTags(tags);
}

/**
* Removes {@code key} from this {@code AddressBook}.
* {@code key} must exist in the address book.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON;
import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;
import static seedu.address.testutil.TypicalWeddings.AMY_WEDDING;
import static seedu.address.testutil.TypicalWeddings.BOB_WEDDING;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.junit.jupiter.api.Test;

import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.Person;
import seedu.address.model.wedding.Wedding;
import seedu.address.model.wedding.WeddingName;

public class AssignWeddingCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
public void assignWedding_success() {
Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
HashSet<Wedding> weddingsToAdd = new HashSet<>(Arrays.asList(AMY_WEDDING));

AssignWeddingCommand assignWeddingCommand = new AssignWeddingCommand(
INDEX_FIRST_PERSON, weddingsToAdd);

String expectedMessage = String.format(Messages.MESSAGE_ADD_WEDDING_SUCCESS, "Amy's Wedding",
personToEdit.getName().toString());

Model expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs());
Set<Wedding> updatedWeddings = new HashSet<>(personToEdit.getWeddings());
updatedWeddings.addAll(weddingsToAdd);
Person editedPerson = new Person(
personToEdit.getName(),
personToEdit.getPhone(),
personToEdit.getEmail(),
personToEdit.getAddress(),
personToEdit.getTags(),
updatedWeddings);
expectedModel.setPerson(personToEdit, editedPerson);

CommandTestUtil.assertCommandSuccess(assignWeddingCommand, model, expectedMessage, expectedModel);
}

@Test
public void assignWeddingZeroWeddings_fail() {
Wedding unseenWedding = new Wedding(new WeddingName("UNKNOWN WEDDING"));
HashSet<Wedding> weddingsToAdd = new HashSet<>(Arrays.asList(unseenWedding));
AssignWeddingCommand assignWeddingCommand = new AssignWeddingCommand(
INDEX_FIRST_PERSON, weddingsToAdd);
String expectedMessage = Messages.MESSAGE_WEDDING_NOT_FOUND + "\n"
+ Messages.MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT;
CommandTestUtil.assertCommandFailure(assignWeddingCommand, model, expectedMessage);
}

@Test
public void sameCommandTest_success() {
HashSet<Wedding> weddingsToRemove = new HashSet<>(Arrays.asList(BOB_WEDDING));
AssignWeddingCommand command1 = new AssignWeddingCommand(INDEX_FIRST_PERSON, weddingsToRemove);
assertTrue(command1.equals(command1));
}

@Test
public void differentCommandType_fail() {
HashSet<Wedding> weddingsToRemove = new HashSet<>();
AssignWeddingCommand command1 = new AssignWeddingCommand(INDEX_FIRST_PERSON, weddingsToRemove);
assertFalse(command1.equals(null));
}

@Test
public void differentIndex_fail() {
HashSet<Wedding> weddingsToRemove = new HashSet<>();
AssignWeddingCommand command1 = new AssignWeddingCommand(INDEX_FIRST_PERSON, weddingsToRemove);
AssignWeddingCommand command2 = new AssignWeddingCommand(INDEX_SECOND_PERSON, weddingsToRemove);
assertFalse(command1.equals(command2));
}

@Test
public void differentWeddings_fail() {
HashSet<Wedding> weddingsToRemove1 = new HashSet<>();
HashSet<Wedding> weddingsToRemove2 = new HashSet<>(Arrays.asList(AMY_WEDDING));
AssignWeddingCommand command1 = new AssignWeddingCommand(INDEX_FIRST_PERSON, weddingsToRemove1);
AssignWeddingCommand command2 = new AssignWeddingCommand(INDEX_SECOND_PERSON, weddingsToRemove2);
assertFalse(command1.equals(command2));
}

@Test
public void forceAssignWedding_success() {
Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Wedding unseenWedding = new Wedding(new WeddingName("UNKNOWN WEDDING"));
HashSet<Wedding> weddingsToAdd = new HashSet<>(Arrays.asList(unseenWedding));
AssignWeddingCommand assignWeddingCommand = new AssignWeddingCommand(
INDEX_FIRST_PERSON, weddingsToAdd, true);
String expectedMessage = String.format(
Messages.MESSAGE_ADD_WEDDING_SUCCESS,
unseenWedding.getWeddingName().toString(),
personToEdit.getName().toString());
CommandTestUtil.assertCommandSuccess(assignWeddingCommand, model, expectedMessage, model);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagName;
import seedu.address.testutil.EditPersonDescriptorBuilder;

/**
Expand Down Expand Up @@ -96,8 +94,6 @@ public class CommandTestUtil {
public static final EditCommand.EditPersonDescriptor DESC_AMY;
public static final EditCommand.EditPersonDescriptor DESC_BOB;

public static final Tag TEST_TAG_FLORIST = new Tag(new TagName(VALID_TAG_FLORIST));

static {
DESC_AMY = new EditPersonDescriptorBuilder().withName(VALID_NAME_AMY)
.withPhone(VALID_PHONE_AMY).withEmail(VALID_EMAIL_AMY).withAddress(VALID_ADDRESS_AMY)
Expand Down
Loading