Skip to content

Commit

Permalink
Merge pull request #122 from HanB1n/branch-force
Browse files Browse the repository at this point in the history
Add Force functionality for Tags and Weddings
  • Loading branch information
dasha3412 authored Oct 24, 2024
2 parents 247a5d3 + b050595 commit a66e90c
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public class Messages {
+ "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.";
public static final String MESSAGE_FORCE_DELETE_WEDDING = "Use f/ to force the deletion of wedding."
+ " This will unassign all people for the Wedding object.";
public static final String MESSAGE_FORCE_TAG_TO_CONTACT = "Use f/ to force the tagging of contacts."
+ " This will create the require Tags.";
public static final String MESSAGE_FORCE_DELETE_TAG = "Use f/ to force the deletion of tags."
+ " This will unassign all contacts with the Tag.";

/**
* Returns an error message indicating the duplicate prefixes.
Expand Down
52 changes: 47 additions & 5 deletions src/main/java/seedu/address/logic/commands/DeleteTagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -22,16 +24,31 @@ public class DeleteTagCommand extends Command {
+ "Parameters: " + PREFIX_TAG + "TAG (must exist in the AddressBook)\n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_TAG + "florist";

public static final String MESSAGE_DELETE_TAG_SUCCESS = "Deleted Tag: %1$s";
public static final String MESSAGE_DELETE_TAG_FAILURE_STILL_TAGGED = "The Tag: %1$s is still used";
public static final String MESSAGE_DELETE_TAG_FAILURE_NOT_FOUND = "The Tag: %1$s does not exist";
public static final String MESSAGE_DELETE_TAG_SUCCESS = "Deleted Tag: %1$s.";
public static final String MESSAGE_DELETE_TAG_FAILURE_STILL_TAGGED = "The Tag: %1$s is still used.";
public static final String MESSAGE_DELETE_TAG_FAILURE_NOT_FOUND = "The Tag: %1$s does not exist.";

private final Tag targetTag;
private boolean force = false;

/**
* Initialises a DeleteTagCommand object with default force is false
* @param targetTag A tag object as the target
*/
public DeleteTagCommand(Tag targetTag) {
this.targetTag = targetTag;
}

/**
* Initialises a DeleteTagCommand object with a specific force
* @param targetTag A Tag object as the target
* @param force A boolean representing if the command should be forced
*/
public DeleteTagCommand(Tag targetTag, boolean force) {
this.targetTag = targetTag;
this.force = force;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand All @@ -43,11 +60,36 @@ public CommandResult execute(Model model) throws CommandException {
model.deleteTag(tag);
return new CommandResult(String.format(MESSAGE_DELETE_TAG_SUCCESS, Messages.format(targetTag)));
} else {
throw new CommandException(
String.format(MESSAGE_DELETE_TAG_FAILURE_STILL_TAGGED, Messages.format(targetTag)));
if (this.force) {
for (Person person : model.getFilteredPersonList()) {
HashSet<Tag> personTags = new HashSet<>(person.getTags());
if (personTags.contains(tag)) {
personTags.remove(tag);
Person newPerson = new Person(
person.getName(),
person.getPhone(),
person.getEmail(),
person.getAddress(),
personTags,
person.getWeddings(),
person.getTasks()
);
model.setPerson(person, newPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
}
}
model.deleteTag(tag);
return new CommandResult(String.format(MESSAGE_DELETE_TAG_SUCCESS, Messages.format(targetTag)));
} else {
throw new CommandException(
String.format(MESSAGE_DELETE_TAG_FAILURE_STILL_TAGGED, Messages.format(targetTag))
+ "\n"
+ Messages.MESSAGE_FORCE_DELETE_TAG);
}
}
}
}
model.updateFilteredTagList(Model.PREDICATE_SHOW_ALL_TAGS);
throw new CommandException(String.format(MESSAGE_DELETE_TAG_FAILURE_NOT_FOUND, Messages.format(targetTag)));
}

Expand Down
25 changes: 23 additions & 2 deletions src/main/java/seedu/address/logic/commands/TagCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
Expand All @@ -34,7 +35,7 @@ public class TagCommand extends Command {

private final Index index;
private final HashSet<Tag> tagsToAdd;

private boolean force = false;

/**
* Constructs a {@code TagCommand} to add tags to a person.
Expand All @@ -49,6 +50,21 @@ public TagCommand(Index index, HashSet<Tag> tagsToAdd) {
this.tagsToAdd = tagsToAdd;
}

/**
* Constructs a {@code TagCommand} to add tags to a person.
*
* @param index The index of the person in the person list.
* @param tagsToAdd The list of tags to be added.
* @param force A boolean representing if the command should be forced.
*/
public TagCommand(Index index, HashSet<Tag> tagsToAdd, boolean force) {
requireNonNull(index);
requireNonNull(tagsToAdd);
this.index = index;
this.tagsToAdd = tagsToAdd;
this.force = force;
}

/**
* Generates a command execution success message showing the added tags and the person.
*
Expand All @@ -74,7 +90,12 @@ public CommandResult execute(Model model) throws CommandException {

for (Tag tag : tagsToAdd) {
if (!model.hasTag(tag)) {
throw new CommandException(MESSAGE_TAG_NOT_FOUND);
if (this.force) {
CreateTagCommand createTagCommand = new CreateTagCommand(tag);
createTagCommand.execute(model);
} else {
throw new CommandException(MESSAGE_TAG_NOT_FOUND + "\n" + Messages.MESSAGE_FORCE_TAG_TO_CONTACT);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public CommandResult execute(Model model) throws CommandException {
MESSAGE_WEDDING_NOT_FOUND + "\n" + MESSAGE_FORCE_ASSIGN_WEDDING_TO_CONTACT);
}
}
wedding.increasePeopleCount();
}

Set<Wedding> updatedWeddings = new HashSet<>(personToEdit.getWeddings());
Expand Down Expand Up @@ -125,6 +126,7 @@ public boolean equals(Object other) {
}

AssignWeddingCommand otherCommand = (AssignWeddingCommand) other;
return index.equals(otherCommand.index) && weddingsToAdd.equals(((AssignWeddingCommand) other).weddingsToAdd);
return index.equals(otherCommand.index) && weddingsToAdd.equals(((AssignWeddingCommand) other).weddingsToAdd)
&& this.force == otherCommand.force;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_WEDDING;

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

import seedu.address.commons.util.ToStringBuilder;
Expand All @@ -11,6 +12,7 @@
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.wedding.Wedding;

/**
Expand All @@ -30,11 +32,26 @@ public class DeleteWeddingCommand extends Command {
public static final String MESSAGE_DELETE_WEDDING_FAILURE_NOT_FOUND = "The Wedding: %1$s does not exist";

private final Wedding targetWedding;
private boolean force = false;

/**
* Initialises a DeleteWeddingCommand object with default force is false
* @param wedding A wedding object as the target
*/
public DeleteWeddingCommand(Wedding wedding) {
targetWedding = wedding;
}

/**
* Initialises a DeleteWeddingCommand object with a specific force
* @param wedding A wedding object as the target
* @param force A boolean representing if the command should be forced
*/
public DeleteWeddingCommand(Wedding wedding, boolean force) {
this.targetWedding = wedding;
this.force = force;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
Expand All @@ -47,11 +64,39 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_DELETE_WEDDING_SUCCESS,
Messages.format(targetWedding)));
} else {
throw new CommandException(
String.format(MESSAGE_DELETE_WEDDING_FAILURE_STILL_USED, Messages.format(targetWedding)));
if (this.force) {
for (Person person : model.getFilteredPersonList()) {
HashSet<Wedding> personWeddings = new HashSet<>(person.getWeddings());
if (personWeddings.contains(wedding)) {
personWeddings.remove(wedding);
Person newPerson = new Person(
person.getName(),
person.getPhone(),
person.getEmail(),
person.getAddress(),
person.getTags(),
personWeddings,
person.getTasks()
);
model.setPerson(person, newPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
}
}
model.deleteWedding(wedding);
return new CommandResult(String.format(MESSAGE_DELETE_WEDDING_SUCCESS,
Messages.format(targetWedding)));
} else {
throw new CommandException(
String.format(MESSAGE_DELETE_WEDDING_FAILURE_STILL_USED,
Messages.format(targetWedding))
+ ".\n"
+ Messages.MESSAGE_FORCE_DELETE_WEDDING
);
}
}
}
}
model.updateFilteredWeddingList(Model.PREDICATE_SHOW_ALL_WEDDINGS);
throw new CommandException(String.format(MESSAGE_DELETE_WEDDING_FAILURE_NOT_FOUND,
Messages.format(targetWedding)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public CommandResult execute(Model model) throws CommandException {
if (!updatedWeddings.containsAll(weddingsToRemove)) {
throw new CommandException(MESSAGE_WEDDING_NOT_FOUND_IN_CONTACT);
}
for (Wedding wedding : updatedWeddings) {
if (weddingsToRemove.contains(wedding)) {
wedding.decreasePeopleCount();
}
}
updatedWeddings.removeAll(weddingsToRemove);

Person editedPerson = new Person(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser;

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_TAG;

import java.util.stream.Stream;
Expand All @@ -20,7 +21,7 @@ public class DeleteTagCommandParser implements Parser<DeleteTagCommand> {
*/
public DeleteTagCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TAG);
ArgumentTokenizer.tokenize(args, PREFIX_TAG, PREFIX_FORCE);

if (!arePrefixesPresent(argMultimap, PREFIX_TAG)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -29,7 +30,11 @@ public DeleteTagCommand parse(String args) throws ParseException {

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TAG);
Tag tag = ParserUtil.parseTag(argMultimap.getValue(PREFIX_TAG).get());
return new DeleteTagCommand(tag);
if (arePrefixesPresent(argMultimap, PREFIX_FORCE)) {
return new DeleteTagCommand(tag, true);
} else {
return new DeleteTagCommand(tag);
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/logic/parser/TagCommandParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package seedu.address.logic.parser;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_FORCE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;
import static seedu.address.logic.parser.wedding.CreateWeddingCommandParser.arePrefixesPresent;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -28,10 +30,14 @@ public class TagCommandParser implements Parser<TagCommand> {
public TagCommand parse(String args) throws ParseException {
requireNonNull(args);

ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TAG, PREFIX_FORCE);
Pair<Index, Set<Tag>> indexAndTags = TaggingCommandParserUtil.parseIndexAndTags(argMultimap,
TagCommand.MESSAGE_USAGE);

return new TagCommand(indexAndTags.getKey(), new HashSet<>(indexAndTags.getValue()));
if (arePrefixesPresent(argMultimap, PREFIX_FORCE)) {
return new TagCommand(indexAndTags.getKey(), new HashSet<>(indexAndTags.getValue()), true);
} else {
return new TagCommand(indexAndTags.getKey(), new HashSet<>(indexAndTags.getValue()));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.address.logic.parser.wedding;

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.stream.Stream;
Expand All @@ -25,7 +26,7 @@ public class DeleteWeddingCommandParser implements Parser<DeleteWeddingCommand>
*/
public DeleteWeddingCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_WEDDING);
ArgumentTokenizer.tokenize(args, PREFIX_WEDDING, PREFIX_FORCE);

if (!arePrefixesPresent(argMultimap, PREFIX_WEDDING)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -34,7 +35,11 @@ public DeleteWeddingCommand parse(String args) throws ParseException {

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_WEDDING);
Wedding wedding = ParserUtil.parseWedding(argMultimap.getValue(PREFIX_WEDDING).get());
return new DeleteWeddingCommand(wedding);
if (arePrefixesPresent(argMultimap, PREFIX_FORCE)) {
return new DeleteWeddingCommand(wedding, true);
} else {
return new DeleteWeddingCommand(wedding);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,11 @@ public void initialiseTags() {
for (Person person : persons) {
Set<Tag> tagForPerson = person.getTags();
for (Tag tag : tagForPerson) {
tag.increaseTaggedCount();
if (!this.hasTag(tag)) {
this.addTag(tag);
} else {
this.setTag(tag, tag);
}
}
}
Expand Down Expand Up @@ -290,6 +293,7 @@ public void initialiseWeddings() {
if (!this.hasWedding(wedding)) {
this.addWedding(wedding);
}
wedding.increasePeopleCount();
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public void decreaseTaggedCount() {
taggedCount--;
}

public int getTagCount() {
return taggedCount;
}
/**
* Returns true if the tag can be deleted.
* The tag can be deleted if TaggedCount is 0.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public Person toModelType() throws IllegalValueException {
final List<Task> personTasks = new ArrayList<>();

for (JsonAdaptedTag tag : tags) {
personTags.add(tag.toModelType());
Tag toAdd = tag.toModelType();
personTags.add(toAdd);
}

for (JsonAdaptedTask task : tasks) {
Expand Down
Loading

0 comments on commit a66e90c

Please sign in to comment.