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 Functionality Tagging #65

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import seedu.address.logic.parser.Prefix;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* Container for user visible messages.
Expand Down Expand Up @@ -48,4 +49,13 @@ public static String format(Person person) {
return builder.toString();
}

/**
* Formats the {@code tag} for display to the user.
*/
public static String format(Tag tag) {
final StringBuilder builder = new StringBuilder();
builder.append(tag.getTagName());
return builder.toString();
}

}
71 changes: 71 additions & 0 deletions src/main/java/seedu/address/logic/commands/CreateTagCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

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.tag.Tag;

/**
* Adds a tag to the address book.
*/
public class CreateTagCommand extends Command {

public static final String COMMAND_WORD = "create-tag";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a tag to the address book. "
+ "Parameters: "
+ PREFIX_TAG + "TAG\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_TAG + "florist";

public static final String MESSAGE_SUCCESS = "New tag added: %1$s";
public static final String MESSAGE_DUPLICATE_PERSON = "This tag already exists in the address book";

private final Tag toAdd;

/**
* Creates an CreateTagCommand to add the specified {@code Tag}
*/
public CreateTagCommand(Tag tag) {
requireNonNull(tag);
toAdd = tag;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasTag(toAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.addTag(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, Messages.format(toAdd)));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof CreateTagCommand)) {
return false;
}

CreateTagCommand otherCreateTagCommand = (CreateTagCommand) other;
return toAdd.equals(otherCreateTagCommand.toAdd);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CreateTagCommand;
import seedu.address.logic.commands.DeleteCommand;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.ExitCommand;
Expand Down Expand Up @@ -77,6 +78,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case CreateTagCommand.COMMAND_WORD:
return new CreateTagCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.address.logic.parser;

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.stream.Stream;

import seedu.address.logic.commands.CreateTagCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Tag;

/**
* Parses input arguments and creates a new AddCommand object
*/
public class CreateTagCommandParser implements Parser<CreateTagCommand> {

/**
* Parses the given {@code String} of arguments in the context of the AddCommand
* and returns an AddCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public CreateTagCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_TAG)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CreateTagCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_TAG);
Tag tag = ParserUtil.parseTag(argMultimap.getValue(PREFIX_TAG).get());
return new CreateTagCommand(tag);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
*/
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) {
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent());
}

}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.TagName;

/**
* Contains utility methods used for parsing strings in the various *Parser classes.
Expand Down Expand Up @@ -107,7 +108,7 @@ public static Tag parseTag(String tag) throws ParseException {
if (!Tag.isValidTagName(trimmedTag)) {
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(trimmedTag);
return new Tag(new TagName(trimmedTag));
}

/**
Expand Down
43 changes: 42 additions & 1 deletion src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.Person;
import seedu.address.model.person.UniquePersonList;
import seedu.address.model.tag.Tag;
import seedu.address.model.tag.UniqueTagList;

/**
* Wraps all data at the address-book level
Expand All @@ -16,16 +18,18 @@
public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final UniqueTagList tags;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
* between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
*
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
* among constructors.
*/
{
persons = new UniquePersonList();
tags = new UniqueTagList();
}

public AddressBook() {}
Expand Down Expand Up @@ -55,8 +59,15 @@ public void resetData(ReadOnlyAddressBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
setTags(newData.getTagList());
}

/**
* 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); }

//// person-level operations

/**
Expand Down Expand Up @@ -94,6 +105,31 @@ public void removePerson(Person key) {
persons.remove(key);
}

/**
* Adds a tag to the address book.
* The tag must not already exist in the address book.
*/
public void addTag(Tag tag) { tags.add(tag); }

/**
* Returns true if a tag with the same name as {@code tag} exists in the address book.
*/
public boolean hasTag(Tag tag) {
requireNonNull(tag);
return tags.contains(tag);
}

/**
* Replaces the given tag {@code target} in the list with {@code editedTag}.
* {@code target} must exist in the address book.
* The person identity of {@code editedTag} must not be the same as another existing tag in the address book.
*/
public void setTag(Tag target, Tag editedTag) {
requireNonNull(editedTag);

tags.setTag(target, editedTag);
}

//// util methods

@Override
Expand All @@ -108,6 +144,9 @@ public ObservableList<Person> getPersonList() {
return persons.asUnmodifiableObservableList();
}

@Override
public ObservableList<Tag> getTagList() { return tags.asUnmodifiableObservableList(); }

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -127,4 +166,6 @@ public boolean equals(Object other) {
public int hashCode() {
return persons.hashCode();
}


}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* The API of the Model component.
*/
public interface Model {
/** {@code Predicate} that always evaluate to true */
Predicate<Person> PREDICATE_SHOW_ALL_PERSONS = unused -> true;
Predicate<Tag> PREDICATE_SHOW_ALL_TAGS = unused -> true;

/**
* Replaces user prefs data with the data in {@code userPrefs}.
Expand Down Expand Up @@ -76,6 +78,8 @@ public interface Model {
*/
void setPerson(Person target, Person editedPerson);

void setTag(Tag target, Tag editedTag);

/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

Expand All @@ -84,4 +88,21 @@ public interface Model {
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredPersonList(Predicate<Person> predicate);

/**
* Returns true if a tag with the same name as (@code tag} exists in the addres book.
*/
boolean hasTag(Tag toAdd);

/**
* Adds the given tag.
* {@code person} must not already exist in the address book.
*/
void addTag(Tag toAdd);

/**
* Updates the filter of the filtered tag list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredTagList(Predicate<Tag> predicate);
}
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* Represents the in-memory model of the address book data.
Expand All @@ -22,6 +23,7 @@ public class ModelManager implements Model {
private final AddressBook addressBook;
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;
private final FilteredList<Tag> filteredTags;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -34,6 +36,7 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs
this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
filteredTags = new FilteredList<>(this.addressBook.getTagList());
}

public ModelManager() {
Expand Down Expand Up @@ -111,6 +114,25 @@ public void setPerson(Person target, Person editedPerson) {
addressBook.setPerson(target, editedPerson);
}

@Override
public void addTag(Tag tag) {
addressBook.addTag(tag);
updateFilteredTagList(PREDICATE_SHOW_ALL_TAGS);
}

@Override
public boolean hasTag(Tag tag) {
requireNonNull(tag);
return addressBook.hasTag(tag);
}

@Override
public void setTag(Tag target, Tag editedTag) {
requireAllNonNull(target, editedTag);

addressBook.setTag(target, editedTag);
}

//=========== Filtered Person List Accessors =============================================================

/**
Expand All @@ -128,6 +150,12 @@ public void updateFilteredPersonList(Predicate<Person> predicate) {
filteredPersons.setPredicate(predicate);
}

@Override
public void updateFilteredTagList(Predicate<Tag> predicate) {
requireNonNull(predicate);
filteredTags.setPredicate(predicate);
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyAddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javafx.collections.ObservableList;
import seedu.address.model.person.Person;
import seedu.address.model.tag.Tag;

/**
* Unmodifiable view of an address book
Expand All @@ -14,4 +15,9 @@ public interface ReadOnlyAddressBook {
*/
ObservableList<Person> getPersonList();

/**
* Returns an unmodifiable view of the tags list.
* This list will not contain any duplicate tags.
*/
ObservableList<Tag> getTagList();
}
Loading
Loading