Skip to content

Commit

Permalink
Merge pull request #88 from yungyung04/v.12-viewsort
Browse files Browse the repository at this point in the history
[V1.2][T11-B1]Eka Buyung | update initialization, test cases and viewSort command
  • Loading branch information
ctchozzz authored Mar 25, 2018
2 parents 4475311 + 517a693 commit 5f188cb
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 218 deletions.
16 changes: 14 additions & 2 deletions src/main/java/seedu/address/logic/commands/ViewSortCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.util.function.Predicate;

import seedu.address.model.person.Person;
import seedu.address.model.tutee.EducationLevelContainsKeywordsPredicate;
import seedu.address.model.tutee.GradeContainsKeywordsPredicate;
import seedu.address.model.tutee.Tutee;
import seedu.address.model.tutee.SchoolContainsKeywordsPredicate;
import seedu.address.model.tutee.SubjectContainsKeywordsPredicate;

/**
* Views a sorted person or task list that suits the specified category.
Expand All @@ -16,6 +18,7 @@ public class ViewSortCommand extends Command {
public static final String MESSAGE_SUCCESS = "Sorted all persons";

private static final String CATEGORY_MONTH = "month";
private static final String CATEGORY_EDUCATION_LEVEL = "level";
private static final String CATEGORY_GRADE = "grade";
private static final String CATEGORY_SCHOOL = "school";
private static final String CATEGORY_SUBJECT = "subject";
Expand All @@ -25,6 +28,7 @@ public class ViewSortCommand extends Command {
+ "Parameters: sort-category keyword\n"
+ "Choice of categories: "
+ CATEGORY_MONTH + ", "
+ CATEGORY_EDUCATION_LEVEL + ", "
+ CATEGORY_GRADE + ", "
+ CATEGORY_SCHOOL + ", "
+ CATEGORY_SUBJECT + "\n"
Expand All @@ -44,16 +48,24 @@ public CommandResult execute() {
switch (category) {
case CATEGORY_MONTH:
break;
case CATEGORY_EDUCATION_LEVEL:
predicate = new EducationLevelContainsKeywordsPredicate(Arrays.asList(keywords));
model.updateFilteredPersonList(predicate);
break;
case CATEGORY_GRADE:
predicate = new GradeContainsKeywordsPredicate(Arrays.asList(keywords));
model.updateFilteredPersonList(predicate);
break;
case CATEGORY_SCHOOL:
predicate = new SchoolContainsKeywordsPredicate(Arrays.asList(keywords));
model.updateFilteredPersonList(predicate);
break;
case CATEGORY_SUBJECT:
predicate = new SubjectContainsKeywordsPredicate(Arrays.asList(keywords));
model.updateFilteredPersonList(predicate);
break;
default:
// no valid category
// invalid category should be detected in parser
assert (false);
}
return new CommandResult(getMessageForPersonListShownSummary(model.getFilteredPersonList().size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public class ViewSortCommandParser implements Parser<ViewSortCommand> {
private static final String CATEGORY_GRADE = "grade";
private static final String CATEGORY_SCHOOL = "school";
private static final String CATEGORY_SUBJECT = "subject";
private static final String CATEGORY_EDUCATION_LEVEL = "level";
private static final int EXPECTED_AMOUNT_OF_PARAMETERS = 2;
private static final int INDEX_OF_FILTER_CATEGORY = 0;
private static final int INDEX_OF_KEYWORDS = 1;

private List<String> validCategories =
new ArrayList<>(Arrays.asList(CATEGORY_MONTH, CATEGORY_GRADE, CATEGORY_SCHOOL, CATEGORY_SUBJECT));
new ArrayList<>(Arrays.asList(
CATEGORY_MONTH, CATEGORY_EDUCATION_LEVEL, CATEGORY_GRADE, CATEGORY_SCHOOL, CATEGORY_SUBJECT));

/**
* Parses the given {@code String} of arguments in the context of the ViewSortCommand
Expand Down
20 changes: 4 additions & 16 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class AddressBook implements ReadOnlyAddressBook {

private final UniquePersonList persons;
private final UniqueTagList tags;
private final UniqueTuteeList tutees;

/*
* The 'unusual' code block below is an non-static initialization block, sometimes used to avoid duplication
Expand All @@ -39,7 +38,6 @@ public class AddressBook implements ReadOnlyAddressBook {
{
persons = new UniquePersonList();
tags = new UniqueTagList();
tutees = new UniqueTuteeList();
}

public AddressBook() {}
Expand Down Expand Up @@ -74,16 +72,11 @@ public void resetData(ReadOnlyAddressBook newData) {

try {
setPersons(syncedPersonList);
setTutees(syncedPersonList);
} catch (DuplicatePersonException e) {
throw new AssertionError("AddressBooks should not have duplicate persons");
}
}

private void setTutees(List<Person> persons) throws DuplicatePersonException {
this.tutees.setTutees(persons);
}

//// person-level operations

/**
Expand Down Expand Up @@ -141,11 +134,10 @@ private Person syncWithMasterTagList(Person person) {
personTags.forEach(tag -> correctTagReferences.add(masterTagObjects.get(tag)));

if (person instanceof Tutee) {
return new Tutee(
person.getName(), person.getPhone(), person.getEmail(), person.getAddress(), (
(Tutee) person).getSubject(), ((Tutee) person).getGrade(), (
(Tutee) person).getEducationLevel(), (
(Tutee) person).getSchool(), correctTagReferences);
return new Tutee(person.getName(), person.getPhone(), person.getEmail(), person.getAddress(),
((Tutee) person).getSubject(), ((Tutee) person).getGrade(),
((Tutee) person).getEducationLevel(),
((Tutee) person).getSchool(), correctTagReferences);
} else {
return new Person(
person.getName(), person.getPhone(), person.getEmail(), person.getAddress(), correctTagReferences);
Expand Down Expand Up @@ -206,10 +198,6 @@ public ObservableList<Person> getPersonList() {
return persons.asObservableList();
}

public ObservableList<Tutee> getTuteeList() {
return tutees.asObservableList();
}

@Override
public ObservableList<Tag> getTagList() {
return tags.asObservableList();
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
import seedu.address.model.tag.Tag;
import seedu.address.model.tutee.Tutee;

/**
* The API of the Model component.
Expand Down Expand Up @@ -47,12 +46,6 @@ void updatePerson(Person target, Person editedPerson)
*/
void updateFilteredPersonList(Predicate<Person> predicate);

///**
//* Updates the filter of the filtered tutee list to filter by the given {@code predicate}.
//* @throws NullPointerException if {@code predicate} is null.
//*/
//void updateFilteredTuteeList(String category, String[] keywords);

/**
* Removes the given {@code tag} from the specified {@code person}.
*/
Expand Down
39 changes: 0 additions & 39 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import seedu.address.model.person.exceptions.DuplicatePersonException;
import seedu.address.model.person.exceptions.PersonNotFoundException;
import seedu.address.model.tag.Tag;
import seedu.address.model.tutee.Tutee;

/**
* Represents the in-memory model of the address book data.
Expand All @@ -27,7 +26,6 @@ public class ModelManager extends ComponentManager implements Model {

private final AddressBook addressBook;
private FilteredList<Person> filteredPersons;
private FilteredList<Tutee> filteredTutees;

/**
* Initializes a ModelManager with the given addressBook and userPrefs.
Expand All @@ -40,13 +38,6 @@ public ModelManager(ReadOnlyAddressBook addressBook, UserPrefs userPrefs) {

this.addressBook = new AddressBook(addressBook);
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
filteredTutees = new FilteredList<>(this.addressBook.getTuteeList());

//for (Person tutee: filteredPersons) {
// if (tutee instanceof Tutee) {
// filteredTutees.add((Tutee) tutee);
// }
//}
}

public ModelManager() {
Expand Down Expand Up @@ -97,8 +88,6 @@ public void deleteTag(Tag tag, Person person) {
addressBook.removeTagFromPerson(tag, person);
}

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

/**
* Returns an unmodifiable view of the list of {@code Person} backed by the internal list of
* {@code addressBook}
Expand Down Expand Up @@ -131,32 +120,4 @@ public boolean equals(Object obj) {
return addressBook.equals(other.addressBook)
&& filteredPersons.equals(other.filteredPersons);
}

//=========== Filtered Tutee List Accessors =============================================================

/**
* Returns an unmodifiable view of the list of {@code Person} which is an instance of {@code Tutee}
* backed by the internal list of {@code addressBook}
*/
/**
@Override
public void updateFIlteredTuteeList(String category, String[] keywords) {
for (Person person : filteredPersons) {
if (person instanceof Tutee) {
for (String keyword : keywords) {
switch category {
case "grade":
if (keyword == ((Tutee) person).getGrade()) {
}
}
}
}
}
}
*/
//public void updateFilteredTuteeList(Predicate<Tutee> predicate) {
// requireNonNull(predicate);
// filteredTutees.setPredicate(predicate);
//}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package seedu.address.model.tutee;

import java.util.List;
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.model.person.Person;

/**
* Tests that a {@code Tutee}'s {@code Education Level} matches any of the keywords given.
*/
public class EducationLevelContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public EducationLevelContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Person tutee) {
if (!(tutee instanceof Tutee)) {
return false;
} else {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase((
(Tutee) tutee).getEducationLevel().toString(), keyword)) == true;
}
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof EducationLevelContainsKeywordsPredicate // instanceof handles nulls
&& this.keywords.equals(((EducationLevelContainsKeywordsPredicate) other).keywords)); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public boolean test(Person tutee) {
return false;
} else {
return keywords.stream()
.anyMatch(keyword -> StringUtil
.containsWordIgnoreCase( ((Tutee) tutee).getGrade().toString(), keyword)) ==true;
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase((
(Tutee) tutee).getGrade().toString(), keyword)) == true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.model.person.Person;

/**
* Tests that a {@code Tutee}'s {@code School} matches any of the keywords given.
*/
public class SchoolContainsKeywordsPredicate implements Predicate<Tutee> {
public class SchoolContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public SchoolContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Tutee tutee) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(tutee.getSchool().toString(), keyword)) == true;
public boolean test(Person tutee) {
if (!(tutee instanceof Tutee)) {
return false;
} else {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase((
(Tutee) tutee).getSchool().toString(), keyword)) == true;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@
import java.util.function.Predicate;

import seedu.address.commons.util.StringUtil;
import seedu.address.model.person.Person;

/**
* Tests that a {@code Tutee}'s {@code Subject} matches any of the keywords given.
*/
public class SubjectContainsKeywordsPredicate implements Predicate<Tutee> {
public class SubjectContainsKeywordsPredicate implements Predicate<Person> {
private final List<String> keywords;

public SubjectContainsKeywordsPredicate(List<String> keywords) {
this.keywords = keywords;
}

@Override
public boolean test(Tutee tutee) {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase(tutee.getSubject().toString(), keyword)) == true;
public boolean test(Person tutee) {
if (!(tutee instanceof Tutee)) {
return false;
} else {
return keywords.stream()
.anyMatch(keyword -> StringUtil.containsWordIgnoreCase((
(Tutee) tutee).getSubject().toString(), keyword)) == true;
}
}

@Override
Expand Down
Loading

0 comments on commit 5f188cb

Please sign in to comment.