From 9034b8420aeed365fad49391a34d1f9c8329e29e Mon Sep 17 00:00:00 2001 From: Jason Manson-Hing Date: Tue, 13 Mar 2018 21:30:57 +0800 Subject: [PATCH] Added Tagging Session Event Logging --- .gitignore | 2 ++ .../addressbook/commands/AddCommand.java | 8 ++++++ .../addressbook/commands/ExitCommand.java | 10 ++++++- src/seedu/addressbook/data/AddressBook.java | 18 +++++++++++++ src/seedu/addressbook/data/Tagging.java | 27 +++++++++++++++++++ test/expected.txt | 6 +++++ test/runtests.sh | 4 +-- 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/seedu/addressbook/data/Tagging.java diff --git a/.gitignore b/.gitignore index 9c362a58e..074b18f05 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,5 @@ test/localrun.bat # Gradle build files .gradle/ build/ + +*.DS_Store diff --git a/src/seedu/addressbook/commands/AddCommand.java b/src/seedu/addressbook/commands/AddCommand.java index 7f27791e7..fcad7cf2b 100644 --- a/src/seedu/addressbook/commands/AddCommand.java +++ b/src/seedu/addressbook/commands/AddCommand.java @@ -1,8 +1,11 @@ package seedu.addressbook.commands; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; +import seedu.addressbook.data.AddressBook; +import seedu.addressbook.data.Tagging; import seedu.addressbook.data.exception.IllegalValueException; import seedu.addressbook.data.person.Address; import seedu.addressbook.data.person.Email; @@ -67,6 +70,11 @@ public ReadOnlyPerson getPerson() { public CommandResult execute() { try { addressBook.addPerson(toAdd); + if (toAdd.getTags() != null) { + for (Tag tag : toAdd.getTags()) { + AddressBook.addSessionsTaggingEvent(new Tagging(toAdd, tag, Tagging.OperationType.ADDITION)); + } + } return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); } catch (UniquePersonList.DuplicatePersonException dpe) { return new CommandResult(MESSAGE_DUPLICATE_PERSON); diff --git a/src/seedu/addressbook/commands/ExitCommand.java b/src/seedu/addressbook/commands/ExitCommand.java index 2f280395c..9aff17e77 100644 --- a/src/seedu/addressbook/commands/ExitCommand.java +++ b/src/seedu/addressbook/commands/ExitCommand.java @@ -1,5 +1,9 @@ package seedu.addressbook.commands; +import seedu.addressbook.data.Tagging; + +import java.util.ArrayList; + /** * Terminates the program. */ @@ -13,7 +17,11 @@ public class ExitCommand extends Command { @Override public CommandResult execute() { - return new CommandResult(MESSAGE_EXIT_ACKNOWEDGEMENT); + StringBuilder sessionTaggings = new StringBuilder(); + for (Tagging taggingEvent : addressBook.getSessionsTaggings()) { + sessionTaggings.append(taggingEvent.toString()).append("\n"); + } + return new CommandResult(sessionTaggings + MESSAGE_EXIT_ACKNOWEDGEMENT); } public static boolean isExit(Command command) { diff --git a/src/seedu/addressbook/data/AddressBook.java b/src/seedu/addressbook/data/AddressBook.java index a99a92f9f..ad2c798ff 100644 --- a/src/seedu/addressbook/data/AddressBook.java +++ b/src/seedu/addressbook/data/AddressBook.java @@ -1,5 +1,6 @@ package seedu.addressbook.data; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -22,6 +23,7 @@ */ public class AddressBook { + private static ArrayList sessionTaggings; // list of tagging actions performed in a session private final UniquePersonList allPersons; private final UniqueTagList allTags; // can contain tags not attached to any person @@ -31,6 +33,7 @@ public class AddressBook { public AddressBook() { allPersons = new UniquePersonList(); allTags = new UniqueTagList(); + sessionTaggings = new ArrayList<>(); } /** @@ -43,6 +46,7 @@ public AddressBook() { public AddressBook(UniquePersonList persons, UniqueTagList tags) { this.allPersons = new UniquePersonList(persons); this.allTags = new UniqueTagList(tags); + this.sessionTaggings = new ArrayList<>(); for (Person p : allPersons) { syncTagsWithMasterList(p); } @@ -121,6 +125,20 @@ public UniqueTagList getAllTags() { return new UniqueTagList(allTags); } + /** + * Returns an ArrayList of all tagging events triggered in the session at the time of the call. + */ + public ArrayList getSessionsTaggings() { + return this.sessionTaggings; + } + + /** + * Adds a tagging event triggered in the session to the session tagging list. + */ + public static void addSessionsTaggingEvent(Tagging tagInfo) { + sessionTaggings.add(tagInfo); + } + @Override public boolean equals(Object other) { return other == this // short circuit if same object diff --git a/src/seedu/addressbook/data/Tagging.java b/src/seedu/addressbook/data/Tagging.java new file mode 100644 index 000000000..35836949e --- /dev/null +++ b/src/seedu/addressbook/data/Tagging.java @@ -0,0 +1,27 @@ +package seedu.addressbook.data; + +import seedu.addressbook.data.person.Person; +import seedu.addressbook.data.tag.Tag; + +public class Tagging { + + public enum OperationType { + ADDITION, DELETION + } + + private final Person person; + private final Tag tag; + private final OperationType operationType; + + public Tagging(Person person, Tag tag, OperationType operationType) { + this.person = person; + this.tag = tag; + this.operationType = operationType; + } + + @Override + public String toString() { + final String operationSymbol = (operationType == OperationType.ADDITION ? "+ " : "- "); + return operationSymbol + person.getName().toString() + " [" + tag.tagName + "]"; + } +} diff --git a/test/expected.txt b/test/expected.txt index 49993eaa1..2341f9588 100644 --- a/test/expected.txt +++ b/test/expected.txt @@ -298,6 +298,12 @@ || 0 persons listed! || =================================================== || Enter command: || [Command entered: exit] +|| + Betsy Choo [secretive] +|| + Charlie Dickson [school] +|| + Charlie Dickson [friends] +|| + Dickson Ee [friends] +|| + Esther Potato [tubers] +|| + Esther Potato [starchy] || Exiting Address Book as requested ... || =================================================== || Good bye! diff --git a/test/runtests.sh b/test/runtests.sh index 99bc046d8..ac60307b1 100755 --- a/test/runtests.sh +++ b/test/runtests.sh @@ -13,10 +13,10 @@ then fi # compile the code into the bin folder -javac -cp ../src -Xlint:none -d ../bin ../src/seedu/addressbook/Main.java +javac -cp ../src -Xlint:none -d ../bin --add-modules java.xml.bind ../src/seedu/addressbook/Main.java # run the program, feed commands from input.txt file and redirect the output to the actual.txt -java -classpath ../bin seedu.addressbook.Main < input.txt > actual.txt +java -classpath ../bin --add-modules java.xml.bind seedu.addressbook.Main < input.txt > actual.txt # compare the output to the expected output diff actual.txt expected.txt