Skip to content

Commit

Permalink
Merge pull request #45 from MontyPython28/add-Priority-To-Tag
Browse files Browse the repository at this point in the history
Add priority to tag
  • Loading branch information
jiewei98 authored Mar 17, 2022
2 parents 62279f0 + ec4d4f3 commit 333bdfb
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 77 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 @@ -61,6 +61,10 @@ public CommandResult execute(Model model) throws CommandException {
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

public Person getToAdd() {
return toAdd;
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ public class CliSyntax {
public static final Prefix PREFIX_ADDRESS = new Prefix("a/");
public static final Prefix PREFIX_INSURANCE_PACKAGE = new Prefix("i/");
public static final Prefix PREFIX_TAG = new Prefix("t/");

}
42 changes: 40 additions & 2 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashSet;
import java.util.Set;

import javafx.util.Pair;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -14,6 +15,7 @@
import seedu.address.model.person.InsurancePackage;
import seedu.address.model.person.Name;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Priority;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -111,6 +113,40 @@ public static Email parseEmail(String email) throws ParseException {
return new Email(trimmedEmail);
}

/**
* Parses a {@code String tag} into a {@code Pair<String, Priority>}.
* Seperates the priority from the tag entered
*/
public static Pair<String, Priority> parsePriority(String tag) throws ParseException {
requireNonNull(tag);
Priority priority = null;
String tagName = "";
String possiblePriority = tag.length() > 4 ? tag.substring(tag.length() - 4).toLowerCase() : "";

switch (possiblePriority) {
case " :p1":
tagName = tag.substring(0, tag.length() - 4);
priority = Priority.PRIORITY_1;
break;
case " :p2":
tagName = tag.substring(0, tag.length() - 4);
priority = Priority.PRIORITY_2;
break;
case " :p3":
tagName = tag.substring(0, tag.length() - 4);
priority = Priority.PRIORITY_3;
break;
case " :p4":
tagName = tag.substring(0, tag.length() - 4);
priority = Priority.PRIORITY_4;
break;
default:
tagName = tag;
priority = null;
}
return new Pair<>(tagName, priority);
}

/**
* Parses a {@code String tag} into a {@code Tag}.
* Leading and trailing whitespaces will be trimmed.
Expand All @@ -119,11 +155,13 @@ public static Email parseEmail(String email) throws ParseException {
*/
public static Tag parseTag(String tag) throws ParseException {
requireNonNull(tag);
String trimmedTag = tag.trim();
Pair<String, Priority> tagAndPriority = parsePriority(tag.trim());
String trimmedTag = tagAndPriority.getKey().trim();

if (!Tag.isValidTagName(trimmedTag)) {
throw new ParseException(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(trimmedTag);
return new Tag(trimmedTag, tagAndPriority.getValue());
}

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

public enum Priority {
PRIORITY_1,
PRIORITY_2,
PRIORITY_3,
PRIORITY_4
}
11 changes: 7 additions & 4 deletions src/main/java/seedu/address/model/tag/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@
*/
public class Tag {

public static final String MESSAGE_CONSTRAINTS = "Tags names should be alphanumeric";
public static final String VALIDATION_REGEX = "\\p{Alnum}+";
public static final String MESSAGE_CONSTRAINTS = "Tags names should only contain numbers, digits or spaces";
public static final String VALIDATION_REGEX = "[a-zA-Z0-9 ]+";

public final String tagName;
public final Priority tagPriority;

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

/**
Expand All @@ -36,7 +38,8 @@ public static boolean isValidTagName(String test) {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tag // instanceof handles nulls
&& tagName.equals(((Tag) other).tagName)); // state check
&& tagName.equals(((Tag) other).tagName))
&& tagPriority == (((Tag) other).tagPriority); // state check
}

@Override
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/seedu/address/model/util/SampleDataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import javafx.util.Pair;
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.person.Address;
Expand All @@ -12,6 +13,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Priority;
import seedu.address.model.tag.Tag;

/**
Expand All @@ -22,22 +24,23 @@ public static Person[] getSamplePersons() {
return new Person[] {
new Person(new Name("Alex Yeoh"), new Phone("87438807"), new Email("[email protected]"),
new InsurancePackage("Golden Package"), new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
getTagSet(new Pair<>("introduce to friends", null))),
new Person(new Name("Bernice Yu"), new Phone("99272758"), new Email("[email protected]"),
new InsurancePackage("Silver Package"), new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
getTagSet(new Pair<>("update insurance package", Priority.PRIORITY_1),
new Pair<>("going to move abroad soon", null))),
new Person(new Name("Charlotte Oliveiro"), new Phone("93210283"), new Email("[email protected]"),
new InsurancePackage("Undecided"), new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
getTagSet(new Pair<>("tell about Car insurance updates", Priority.PRIORITY_3))),
new Person(new Name("David Li"), new Phone("91031282"), new Email("[email protected]"),
new InsurancePackage("Undecided"), new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
getTagSet(new Pair<>("contact wife if not available", null))),
new Person(new Name("Irfan Ibrahim"), new Phone("92492021"), new Email("[email protected]"),
new InsurancePackage("Basic Family Package"), new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
getTagSet()),
new Person(new Name("Roy Balakrishnan"), new Phone("92624417"), new Email("[email protected]"),
new InsurancePackage("Car Theft Insurance"), new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
getTagSet(new Pair<>("update insurance package", Priority.PRIORITY_1)))
};
}

Expand All @@ -52,9 +55,9 @@ public static ReadOnlyAddressBook getSampleAddressBook() {
/**
* Returns a tag set containing the list of strings given.
*/
public static Set<Tag> getTagSet(String... strings) {
return Arrays.stream(strings)
.map(Tag::new)
public static Set<Tag> getTagSet(Pair<String, Priority>... tagPairs) {
return Arrays.stream(tagPairs)
.map(x -> new Tag(x.getKey(), x.getValue()))
.collect(Collectors.toSet());
}

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/seedu/address/storage/CsvAdaptedPerson.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.storage;

import static seedu.address.storage.CsvAdaptedTag.STRING_PRIORITY_MAP;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -15,6 +17,7 @@
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Priority;
import seedu.address.model.tag.Tag;

/**
Expand Down Expand Up @@ -83,8 +86,15 @@ public CsvAdaptedPerson(String s) {
// tags are internally separated by |
String[] tags = allTagsString.split("\\|");
for (String tagString : tags) {
String possiblePriority = tagString.length() > 4
? tagString.substring(tagString.length() - 4) : "";
Priority p = STRING_PRIORITY_MAP.get(possiblePriority);
if (p != null) {
tagString = tagString.substring(0, tagString.length() - 4);
}

if (tagString.length() > 0) {
tagged.add(new CsvAdaptedTag(tagString));
tagged.add(new CsvAdaptedTag(tagString, p));
}
}
}
Expand All @@ -98,7 +108,7 @@ public CsvAdaptedPerson(String s) {
*/
public Person toModelType() throws IllegalValueException {
final List<Tag> personTags = new ArrayList<>();
for (CsvAdaptedTag tag : tagged) {
for (CsvAdaptedTag tag : tagged) { //change here
personTags.add(tag.toModelType());
}

Expand Down Expand Up @@ -169,7 +179,7 @@ public String toCsvString() {
*/
public static String getTagsAsString(List<CsvAdaptedTag> tags) {
return tags.stream()
.map(CsvAdaptedTag::getTagName)
.map(CsvAdaptedTag::getTagNameString)
.collect(Collectors.joining("|"));
}

Expand Down
34 changes: 29 additions & 5 deletions src/main/java/seedu/address/storage/CsvAdaptedTag.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
package seedu.address.storage;

import java.util.Map;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.tag.Priority;
import seedu.address.model.tag.Tag;

/**
* CSV-friendly version of {@link Tag}.
*/
class CsvAdaptedTag {

public static final Map<String, Priority> STRING_PRIORITY_MAP = Map.of("[P1]", Priority.PRIORITY_1,
"[P2]", Priority.PRIORITY_2, "[P3]", Priority.PRIORITY_3, "[P4]", Priority.PRIORITY_4);
private final String tagName;
private final Priority tagPriority;


/**
* Constructs a {@code CsvAdaptedTag} with the given {@code tagName}.
*/
public CsvAdaptedTag(String tagName) {
public CsvAdaptedTag(String tagName, Priority priority) {
this.tagName = tagName;
this.tagPriority = priority;
}

/**
* Converts a given {@code Tag} into this class for CSV use.
*/
public CsvAdaptedTag(Tag source) {
tagName = source.tagName;
tagPriority = source.tagPriority;
}

public String getTagName() {
return tagName;
public String getTagNameString() {
String priorityString = "";
if (tagPriority != null) {
for (Map.Entry<String, Priority> e: STRING_PRIORITY_MAP.entrySet()) {
if (e.getValue() == tagPriority) {
priorityString = e.getKey();
break;
}
}
}

return tagName + priorityString;
}

/**
Expand All @@ -37,7 +56,7 @@ public Tag toModelType() throws IllegalValueException {
if (!Tag.isValidTagName(tagName)) {
throw new IllegalValueException(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(tagName);
return new Tag(tagName, tagPriority);
}

@Override
Expand All @@ -51,11 +70,16 @@ public boolean equals(Object other) {
}

CsvAdaptedTag otherTag = (CsvAdaptedTag) other;
return tagName.equals(otherTag.tagName);
return tagName.equals(otherTag.tagName) && tagPriority == otherTag.tagPriority;
}

@Override
public int hashCode() {
return tagName.hashCode();
}

@Override
public String toString() {
return tagName + tagPriority;
}
}
18 changes: 8 additions & 10 deletions src/main/java/seedu/address/storage/JsonAdaptedTag.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
package seedu.address.storage;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.tag.Priority;
import seedu.address.model.tag.Tag;

/**
* Jackson-friendly version of {@link Tag}.
*/
class JsonAdaptedTag {

private final String tagName;
private final Priority tagPriority;

/**
* Constructs a {@code JsonAdaptedTag} with the given {@code tagName}.
*/
@JsonCreator
public JsonAdaptedTag(String tagName) {
public JsonAdaptedTag(@JsonProperty("tagName") String tagName, @JsonProperty("priority") Priority priority) {
this.tagName = tagName;
this.tagPriority = priority;
}

/**
* Converts a given {@code Tag} into this class for Jackson use.
*/
public JsonAdaptedTag(Tag source) {
tagName = source.tagName;
}

@JsonValue
public String getTagName() {
return tagName;
tagPriority = source.tagPriority;
}

/**
Expand All @@ -42,7 +40,7 @@ public Tag toModelType() throws IllegalValueException {
if (!Tag.isValidTagName(tagName)) {
throw new IllegalValueException(Tag.MESSAGE_CONSTRAINTS);
}
return new Tag(tagName);
return new Tag(tagName, tagPriority);
}

@Override
Expand All @@ -56,7 +54,7 @@ public boolean equals(Object other) {
}

JsonAdaptedTag otherTag = (JsonAdaptedTag) other;
return tagName.equals(otherTag.tagName);
return tagName.equals(otherTag.tagName) && tagPriority.equals(otherTag.tagPriority);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class JsonSerializableAddressBook {
*/
@JsonCreator
public JsonSerializableAddressBook(@JsonProperty("persons") List<JsonAdaptedPerson> persons) {
System.out.println("here");
this.persons.addAll(persons);
}

Expand Down
Loading

0 comments on commit 333bdfb

Please sign in to comment.