Skip to content

Commit

Permalink
Support adding a module tag #80
Browse files Browse the repository at this point in the history
  • Loading branch information
gremmyz authored Mar 15, 2023
2 parents 911c8fa + eaf3054 commit 0041ed7
Show file tree
Hide file tree
Showing 30 changed files with 500 additions and 76 deletions.
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCMED_INSTAGRAM;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class AddCommand extends Command {
+ "[" + PREFIX_SOCMED_TELEGRAM + "TELEGRAM] "
+ "[" + PREFIX_SOCMED_WHATSAPP + "WHATSAPP] "
+ "[" + PREFIX_BIRTHDAY + "BIRTHDAY] "
+ "[" + PREFIX_MODULE + "MODULE]... "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
Expand All @@ -42,6 +44,8 @@ public class AddCommand extends Command {
+ PREFIX_SOCMED_INSTAGRAM + "john.doe "
+ PREFIX_SOCMED_WHATSAPP + "98765432 "
+ PREFIX_BIRTHDAY + "01/01/1990 "
+ PREFIX_MODULE + "CS2103T "
+ PREFIX_MODULE + "CS2101 "
+ PREFIX_TAG + "friends "
+ PREFIX_TAG + "owesMoney";

Expand Down
38 changes: 31 additions & 7 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCMED_INSTAGRAM;
Expand All @@ -30,6 +31,7 @@
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.socialmedia.SocialMedia;
import seedu.address.model.tag.Module;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -47,10 +49,11 @@ public class EditCommand extends Command {
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_SOCMED_INSTAGRAM + "INSTAGRAM] "
+ "[" + PREFIX_SOCMED_TELEGRAM + "TELEGRAM] "
+ "[" + PREFIX_SOCMED_WHATSAPP + "WHATSAPP] "
+ "[" + PREFIX_BIRTHDAY + "BIRTHDAY] "
+ "[" + PREFIX_SOCMED_INSTAGRAM + "INSTAGRAM] "
+ "[" + PREFIX_SOCMED_TELEGRAM + "TELEGRAM] "
+ "[" + PREFIX_SOCMED_WHATSAPP + "WHATSAPP] "
+ "[" + PREFIX_BIRTHDAY + "BIRTHDAY] "
+ "[" + PREFIX_MODULE + "MODULE]... "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PHONE + "91234567 "
Expand Down Expand Up @@ -105,8 +108,9 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript

Name updatedName = editPersonDescriptor.getName().orElse(personToEdit.getName());
Set<Tag> updatedTags = editPersonDescriptor.getTags().orElse(personToEdit.getTags());
Set<Module> updatedModules = editPersonDescriptor.getModules().orElse(personToEdit.getModules());

Person p = new Person(updatedName, updatedTags);
Person p = new Person(updatedName, updatedTags, updatedModules);

if (editPersonDescriptor.getPhone().isPresent()) {
p.setPhone(editPersonDescriptor.getPhone().get());
Expand Down Expand Up @@ -182,8 +186,8 @@ public static class EditPersonDescriptor {
private Address address;
private SocialMedia socialMedia;
private Set<Tag> tags;

private Birthday birthday;
private Set<Module> modules;

public EditPersonDescriptor() {
}
Expand All @@ -199,6 +203,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
setAddress(toCopy.address);
setSocialMedia(toCopy.socialMedia);
setTags(toCopy.tags);
setModules(toCopy.modules);

setBirthday(toCopy.birthday);
}
Expand All @@ -207,7 +212,7 @@ public EditPersonDescriptor(EditPersonDescriptor toCopy) {
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(name, phone, email, address, socialMedia, tags, birthday);
return CollectionUtil.isAnyNonNull(name, phone, email, address, socialMedia, tags, birthday, modules);
}

public void setName(Name name) {
Expand Down Expand Up @@ -278,6 +283,25 @@ public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}


/**
* Sets {@code modules} to this object's {@code modules}.
* A defensive copy of {@code modules} is used internally.
*/
public void setModules(Set<Module> modules) {
this.modules = (modules != null) ? new HashSet<>(modules) : null;
}

/**
* Returns an unmodifiable modules set, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code modules} is null.
*/
public Optional<Set<Module>> getModules() {
return (modules != null) ? Optional.of(Collections.unmodifiableSet(modules)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
// short circuit if same object
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCMED;
Expand All @@ -21,6 +22,7 @@
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.socialmedia.SocialMedia;
import seedu.address.model.tag.Module;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -35,9 +37,8 @@ public class AddCommandParser implements Parser<AddCommand> {
* @throws ParseException if the user input does not conform the expected format
*/
public AddCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS,
PREFIX_BIRTHDAY, PREFIX_SOCMED, PREFIX_TAG);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_BIRTHDAY, PREFIX_SOCMED, PREFIX_TAG, PREFIX_MODULE);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME)
|| !argMultimap.getPreamble().isEmpty()) {
Expand All @@ -46,8 +47,9 @@ public AddCommand parse(String args) throws ParseException {

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
Set<Module> moduleList = ParserUtil.parseModules(argMultimap.getAllValues(PREFIX_MODULE));

Person person = new Person(name, tagList);
Person person = new Person(name, tagList, moduleList);

if (argMultimap.getValue(PREFIX_PHONE).isPresent()) {
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ 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_BIRTHDAY = new Prefix("b/");
public static final Prefix PREFIX_MODULE = new Prefix("mod/");
public static final Prefix PREFIX_SOCMED = new Prefix("sm/");
public static final Prefix PREFIX_SOCMED_INSTAGRAM = new Prefix("ig/");
public static final Prefix PREFIX_SOCMED_TELEGRAM = new Prefix("tg/");
public static final Prefix PREFIX_SOCMED_WHATSAPP = new Prefix("wa/");

}
21 changes: 20 additions & 1 deletion src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_BIRTHDAY;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SOCMED;
Expand All @@ -19,6 +20,7 @@
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditPersonDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.tag.Module;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -36,7 +38,7 @@ public class EditCommandParser implements Parser<EditCommand> {
public EditCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_SOCMED, PREFIX_ADDRESS, PREFIX_TAG, PREFIX_BIRTHDAY);
PREFIX_ADDRESS, PREFIX_SOCMED, PREFIX_TAG, PREFIX_BIRTHDAY, PREFIX_MODULE);

Index index;

Expand Down Expand Up @@ -66,6 +68,7 @@ public EditCommand parse(String args) throws ParseException {
editPersonDescriptor.setSocialMedia(ParserUtil.parseSocialMedia(argMultimap.getValue(PREFIX_SOCMED).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(editPersonDescriptor::setTags);
parseModulesForEdit(argMultimap.getAllValues(PREFIX_MODULE)).ifPresent(editPersonDescriptor::setModules);

if (!editPersonDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
Expand All @@ -91,4 +94,20 @@ private Optional<Set<Tag>> parseTagsForEdit(Collection<String> tags) throws Pars
return Optional.of(ParserUtil.parseTags(tagSet));
}

/**
* Parses {@code Collection<String> modules} into a {@code Set<Module>} if
* {@code modules} is non-empty.
* If {@code modules} contain only one element which is an empty string, it will be
* parsed into a
* {@code Set<module>} containing zero modules.
*/
private Optional<Set<Module>> parseModulesForEdit(Collection<String> modules) throws ParseException {
assert modules != null;

if (modules.isEmpty()) {
return Optional.empty();
}
Collection<String> moduleSet = modules.size() == 1 && modules.contains("") ? Collections.emptySet() : modules;
return Optional.of(ParserUtil.parseModules(moduleSet));
}
}
28 changes: 28 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import seedu.address.model.socialmedia.SocialMedia;
import seedu.address.model.socialmedia.Telegram;
import seedu.address.model.socialmedia.WhatsApp;
import seedu.address.model.tag.Module;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -135,6 +136,21 @@ public static Tag parseTag(String tag) throws ParseException {
return new Tag(trimmedTag);
}

/**
* Parses a {@code String module} into a {@code module}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code module} is invalid.
*/
public static Module parseModule(String module) throws ParseException {
requireNonNull(module);
String trimmedModule = module.trim();
if (!Module.isValidTagName(trimmedModule)) {
throw new ParseException(Module.MESSAGE_CONSTRAINTS);
}
return new Module(trimmedModule);
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>}.
*/
Expand All @@ -147,6 +163,18 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
return tagSet;
}

/**
* Parses {@code Collection<String> modules} into a {@code Set<Module>}.
*/
public static Set<Module> parseModules(Collection<String> modules) throws ParseException {
requireNonNull(modules);
final Set<Module> moduleSet = new HashSet<>();
for (String moduleName : modules) {
moduleSet.add(parseModule(moduleName));
}
return moduleSet;
}

private static Instagram parseInstagram(String instagram) {
if (instagram == null || instagram.isEmpty()) {
return null;
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Set;

import seedu.address.model.socialmedia.SocialMedia;
import seedu.address.model.tag.Module;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -26,6 +27,7 @@ public class Person {
// Data fields
private Optional<Address> address;
private final Set<Tag> tags = new HashSet<>();
private final Set<Module> modules = new HashSet<>();
private Optional<Birthday> birthday;

// Social media fields
Expand All @@ -34,15 +36,16 @@ public class Person {
/**
* Every field must be present and not null.
*/
public Person(Name name, Set<Tag> tags) {
requireAllNonNull(name, tags);
public Person(Name name, Set<Tag> tags, Set<Module> modules) {
requireAllNonNull(name, tags, modules);
this.name = name;
this.phone = Optional.empty();
this.email = Optional.empty();
this.address = Optional.empty();
this.socialMedia = Optional.empty();
this.tags.addAll(tags);
this.birthday = Optional.empty();
this.modules.addAll(modules);
}

public void setPhone(Phone phone) {
Expand Down Expand Up @@ -98,6 +101,15 @@ public Set<Tag> getTags() {
return Collections.unmodifiableSet(tags);
}

/**
* Returns an immutable module set, which throws
* {@code UnsupportedOperationException}
* if modification is attempted.
*/
public Set<Module> getModules() {
return Collections.unmodifiableSet(modules);
}

/**
* Returns true if both persons have the same name.
* This defines a weaker notion of equality between two persons.
Expand Down Expand Up @@ -130,13 +142,14 @@ public boolean equals(Object other) {
&& otherPerson.getPhone().equals(getPhone())
&& otherPerson.getEmail().equals(getEmail())
&& otherPerson.getAddress().equals(getAddress())
&& otherPerson.getTags().equals(getTags());
&& otherPerson.getTags().equals(getTags())
&& otherPerson.getModules().equals(getModules());
}

@Override
public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(name, phone, email, address, tags);
return Objects.hash(name, phone, email, address, tags, modules);
}

@Override
Expand All @@ -159,6 +172,11 @@ public String toString() {
builder.append("; Tags: ");
tags.forEach(builder::append);
}
Set<Module> modules = getModules();
if (!modules.isEmpty()) {
builder.append("; Modules: ");
modules.forEach(builder::append);
}
return builder.toString();
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/model/tag/Module.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package seedu.address.model.tag;

/**
* Represents a Module in the address book.
* Guarantees: immutable; name is valid as declared in {@link #isValidTagName(String)}
*/
public class Module extends Tag {

public static final String MESSAGE_CONSTRAINTS = "Module names should be alphanumeric";
public final String moduleName;

/**
* Constructs a {@code Module}.
*
* @param moduleName A valid module name.
*/
public Module(String moduleName) {
super(moduleName, MESSAGE_CONSTRAINTS);
this.moduleName = moduleName;
}
/**
* Returns true if a given string is a valid module name.
*/
public static boolean isValidModuleName(String test) {
return isValidTagName(test);
}
}
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public Tag(String tagName) {
this.tagName = tagName;
}

/**
* Constructs a {@code Tag}.
*
* @param tagName A valid tag name.
*/
public Tag(String tagName, String message) {
requireNonNull(tagName);
checkArgument(isValidTagName(tagName), message);
this.tagName = tagName;
}

/**
* Returns true if a given string is a valid tag name.
*/
Expand Down
Loading

0 comments on commit 0041ed7

Please sign in to comment.