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

[W8.6b][T11-B1] Eka Buyung Lienadi #971

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
95 changes: 95 additions & 0 deletions src/seedu/addressbook/data/Tagging/Tagging.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package seedu.addressbook.data.Tagging;

import seedu.addressbook.data.person.Person;
import seedu.addressbook.data.tag.Tag;

import java.util.ArrayList;

/**
* Records recently created and deleted Taggings.
* Tagging represents an association between a person and a tag.
*/
public class Tagging {

public static final int INVALID_INDEX = -1;
/**
* Lists all taggings as a String.
*/
public static ArrayList<Tagging> uniqueTaggingList;
public static ArrayList<String> toBePrintedTaggings;

private Person person;
private Tag tag;

/**
* Constructs a Tagging object.
* new taggings are recorded in {@code} toBePrintedTaggings.
*/
Tagging(Person person, Tag tag) {
this.person = person;
this.tag = tag;
recordNewTagging(person, tag);
}

@Override
/**
* Records recently removed taggings.
*/
protected void finalize() {
recordDeletedTagging();
}

/**
* @return the printable form of all added/deleted taggings.
*/
public static String getPrintableResult() {
String printableTaggings = new String();

for (String tagging : toBePrintedTaggings) {
printableTaggings = tagging + "\n";
}
return printableTaggings;
}

/**
* Clears all element in the printable list.
*/
public static void reset() {
toBePrintedTaggings.clear();
}

private void recordDeletedTagging() {
toBePrintedTaggings.add("- " + person.getName().toString() + " " + tag.toString());
uniqueTaggingList.remove(getTargetIndex());
}

/**
* @return index of tagging that will be removed.
* @return INVALID_INDEX if tagging does not exist.
*/
private int getTargetIndex() {
for (int i = 0; i < uniqueTaggingList.size(); i++) {
if(isSame(i)) {
return i;
}
}
return INVALID_INDEX;
}

private boolean isSame(int i) {
return uniqueTaggingList.get(i).getPerson() == person && uniqueTaggingList.get(i).getTag() == tag;
}

private void recordNewTagging(Person person, Tag tag) {
uniqueTaggingList.add(new Tagging(person, tag));
toBePrintedTaggings.add("+ " + person.getName().toString() + " " + tag.toString());
}

public Person getPerson() {
return person;
}

public Tag getTag() {
return tag;
}
}