Skip to content

Commit

Permalink
Merge pull request #102 from HanB1n/branch-force
Browse files Browse the repository at this point in the history
Add functionality Force
  • Loading branch information
dasha3412 authored Oct 17, 2024
2 parents bc42d4f + f50bc26 commit 38f0e0b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class Messages {
+ "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
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 @@ -57,7 +57,8 @@ public void assignWeddingZeroWeddings_fail() {
HashSet<Wedding> weddingsToAdd = new HashSet<>(Arrays.asList(unseenWedding));
AssignWeddingCommand assignWeddingCommand = new AssignWeddingCommand(
INDEX_FIRST_PERSON, weddingsToAdd);
String expectedMessage = Messages.MESSAGE_WEDDING_NOT_FOUND;
String expectedMessage = Messages.MESSAGE_WEDDING_NOT_FOUND + "\n"
+ Messages.MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT;
CommandTestUtil.assertCommandFailure(assignWeddingCommand, model, expectedMessage);
}

Expand Down Expand Up @@ -91,4 +92,19 @@ public void differentWeddings_fail() {
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 @@ -33,7 +33,8 @@ public void execute_validDeleteWeddingCommand_success() {
assertCommandSuccess(deleteWeddingCommand, model, expectedMessage, expectedModel);
}

@Test void execute_invalidNotFoundDeleteWeddingCommand() {
@Test
public void execute_invalidNotFoundDeleteWeddingCommand() {
Wedding weddingToDelete = model.getFilteredWeddingList().get(0);

String expectedMessage = String.format(DeleteWeddingCommand.MESSAGE_DELETE_WEDDING_FAILURE_NOT_FOUND,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,20 @@ public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, "1 w/ w/Jeslyn's_Wedding",
WeddingName.MESSAGE_CONSTRAINTS);
}

@Test
public void parse_validArgsWithForce_returnsAssignWeddingCommand() {
Index targetIndex = Index.fromOneBased(1);

// Expected weddings
Wedding wedding1 = new Wedding(new WeddingName("Jeslyn's Wedding"));
Wedding wedding2 = new Wedding(new WeddingName("Wedding April 17th 2025"));

AssignWeddingCommand expectedCommand = new AssignWeddingCommand(targetIndex,
new HashSet<>(Arrays.asList(wedding1, wedding2)), true);

String userInput = "1 w/Jeslyn's Wedding w/Wedding April 17th 2025 f/";

assertParseSuccess(parser, userInput, expectedCommand);
}
}

0 comments on commit 38f0e0b

Please sign in to comment.