Skip to content

Commit 39b4234

Browse files
authored
Merge pull request #159 from Lava-Iris/search-ccapos
Search ccapos
2 parents 67ef56b + b0c8271 commit 39b4234

File tree

13 files changed

+76
-151
lines changed

13 files changed

+76
-151
lines changed

docs/UserGuide.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ To retrieve your contacts after a <code>search</code>, type <code>list</code> in
564564

565565
## 4.9 Searching for contact information: `search`
566566

567-
Format: `search [KEYWORD]... [n/NAME_KEYWORD] [p/PHONE_KEYWORD] [e/EMAIL_KEYWORD] [b/BIRTHDAY_KEYWORD] [ig/INSTAGRAM_KEYWORD] [wa/WHATSAPP_KEYWORD] [tg/TELEGRAM_KEYWORD] [r/REMARK_KEYWORD]... [mod/MODULE_KEYWORD]... [cca/CCA_KEYWORD]... [maj/MAJOR_KEYWORD]...`
567+
Format: `search [KEYWORD]... [n/NAME_KEYWORD] [p/PHONE_KEYWORD] [e/EMAIL_KEYWORD] [b/BIRTHDAY_KEYWORD] [ig/INSTAGRAM_KEYWORD] [wa/WHATSAPP_KEYWORD] [tg/TELEGRAM_KEYWORD] [r/REMARK_KEYWORD]... [mod/MODULE_KEYWORD]... [cca/CCA_KEYWORD#CCA_POSITION_KEYWORD]... [maj/MAJOR_KEYWORD]...`
568568

569569
> For keywords without a specified field, finds all contacts whose information from any field contains the given keywords.<br>For keywords in a specified field, finds all contacts whose field contains given keyword.
570570
@@ -592,6 +592,7 @@ From the earlier example, if you have 2 contacts, one with the name January, and
592592
* `search n\alex n\may` returns all contacts whose name contains the keyword `alex` and whose birthday contains the keyword `may`
593593
* `search mod\cs mod\ma` returns all contacts who have at least one module which contains the keyword `cs` and at least one module which contain the keyword `ma`
594594
* `search ig\al cca\chess` returns all contacts whose instagram handle contains the keyword `al` and who has at least one cca with the word `chess` in it
595+
* `search cca\#president` returns all contacts who have a position that contains the keyword `president` in at least one cca
595596

596597
<div markdown="span" class="alert alert-primary">:bulb: **Tip:**<br>
597598
* The keywords are case-insensitive! This means that `search january`, `search JANUARY` and `search jAnUaRy` will all return the contacts whose information fields contain the keyword `january`.

src/main/java/seedu/connectus/logic/parser/SearchCommandParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import seedu.connectus.logic.commands.SearchCommand;
2222
import seedu.connectus.logic.parser.exceptions.ParseException;
2323
import seedu.connectus.model.person.FieldsContainKeywordsPredicate;
24+
import seedu.connectus.model.tag.Cca;
2425

2526
/**
2627
* Parses input arguments and creates a new FindCommand object
@@ -114,12 +115,12 @@ public SearchCommand parse(String args) throws ParseException {
114115
if (modules.size() > 0) {
115116
predicate.setModules(modules);
116117
}
117-
Set<String> ccas = new HashSet<>();
118+
Set<String[]> ccas = new HashSet<>();
118119
for (String s : argMultimap.getAllValues(PREFIX_CCA)) {
119120
if (s.trim().equals("")) {
120121
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SearchCommand.MESSAGE_USAGE));
121122
} else {
122-
ccas.add(s);
123+
ccas.add(Cca.decouple(s));
123124
}
124125
}
125126
if (ccas.size() > 0) {

src/main/java/seedu/connectus/model/person/FieldsContainKeywordsPredicate.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class FieldsContainKeywordsPredicate implements Predicate<Person> {
2525
private Set<String> remarks;
2626
private String birthday;
2727
private Set<String> modules;
28-
private Set<String> ccas;
28+
private Set<String[]> ccas;
2929
private Set<String> majors;
3030
public FieldsContainKeywordsPredicate() {}
3131
public FieldsContainKeywordsPredicate(List<String> keywords) {
@@ -173,7 +173,7 @@ public Optional<Set<String>> getModules() {
173173
* Sets {@code ccas} to this object's {@code ccas}.
174174
* A defensive copy of {@code ccas} is used internally.
175175
*/
176-
public void setCcas(Set<String> ccas) {
176+
public void setCcas(Set<String[]> ccas) {
177177
this.ccas = (ccas != null) ? new HashSet<>(ccas) : null;
178178
}
179179

@@ -183,7 +183,7 @@ public void setCcas(Set<String> ccas) {
183183
* if modification is attempted.
184184
* Returns {@code Optional#empty()} if {@code ccas} is null.
185185
*/
186-
public Optional<Set<String>> getCcas() {
186+
public Optional<Set<String[]>> getCcas() {
187187
return (ccas != null) ? Optional.of(Collections.unmodifiableSet(ccas)) : Optional.empty();
188188
}
189189

@@ -305,8 +305,18 @@ public boolean test(Person person) {
305305
}
306306
}
307307
if (getCcas().isPresent()) {
308-
if (!ccas.stream().allMatch(ccaKey -> person.getCcas().stream().anyMatch(cca ->
309-
StringUtil.containsKeywordsListIgnoreCase(cca.toString(), ccaKey)))) {
308+
boolean result = ccas.stream().allMatch(ccaKey -> person.getCcas().stream().anyMatch(cca -> {
309+
boolean check = true;
310+
check = check
311+
&& (ccaKey[0].trim().isEmpty()
312+
|| StringUtil.containsKeywordsListIgnoreCase(cca.ccaName, ccaKey[0]));
313+
if (ccaKey.length > 1) {
314+
check = check && cca.hasPosition() && StringUtil.containsKeywordsListIgnoreCase(cca.ccaPositionName,
315+
ccaKey[1]);
316+
}
317+
return check;
318+
}));
319+
if (!result) {
310320
return false;
311321
}
312322
}

src/main/java/seedu/connectus/model/person/NameContainsKeywordsPredicate.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/seedu/connectus/model/tag/Cca.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ public class Cca extends Tag {
1717
public final String ccaName;
1818
public final String ccaPositionName;
1919
public final String coupledCcaName;
20-
public final String decoupledCcaName;
2120

22-
private String cca;
21+
private final String cca;
2322
private String position;
24-
private String decoupled;
23+
private boolean isPosition = false;
2524

2625
/**
2726
* Constructs a {@code Cca}.
@@ -32,26 +31,25 @@ public Cca(String coupledCcaName) {
3231
super(coupledCcaName);
3332
checkArgument(isValidCcaName(coupledCcaName), MESSAGE_CONSTRAINTS);
3433
this.coupledCcaName = coupledCcaName;
35-
decoupleCcaName();
34+
String[] arr = decouple(coupledCcaName);
35+
cca = arr[0];
36+
if (arr.length > 1) {
37+
isPosition = true;
38+
position = arr[1];
39+
}
3640
ccaName = cca;
3741
ccaPositionName = position;
38-
decoupledCcaName = decoupled;
3942
}
4043

44+
public boolean hasPosition() {
45+
return isPosition;
46+
}
4147
/**
4248
* Decouples command into CCA Name and CCA Position.
4349
*/
44-
public void decoupleCcaName() {
50+
public static String[] decouple(String coupledCcaName) {
4551
String[] arr = coupledCcaName.split("#");
46-
if (arr.length == 2) {
47-
cca = arr[0];
48-
position = arr[1];
49-
decoupled = cca + " - " + position;
50-
} else {
51-
cca = arr[0];
52-
position = "";
53-
decoupled = cca;
54-
}
52+
return arr;
5553
}
5654

5755
/**
@@ -63,6 +61,10 @@ public static boolean isValidCcaName(String test) {
6361

6462
@Override
6563
public String toString() {
66-
return decoupledCcaName;
64+
if (isPosition) {
65+
return cca + " - " + position;
66+
} else {
67+
return cca;
68+
}
6769
}
6870
}

src/main/java/seedu/connectus/storage/JsonAdaptedCca.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public JsonAdaptedCca(String ccaName) {
2525
* Converts a given {@code Cca} into this class for Jackson use.
2626
*/
2727
public JsonAdaptedCca(Cca source) {
28-
this.ccaName = source.decoupledCcaName;
28+
this.ccaName = source.toString();
2929
}
3030

3131
@JsonValue

src/main/java/seedu/connectus/ui/PersonCard.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ public PersonCard(Person person, int displayedIndex) {
119119
tags.getChildren().add(moduleLabel);
120120
});
121121
person.getCcas().stream()
122-
.sorted(Comparator.comparing(cca -> cca.decoupledCcaName))
122+
.sorted(Comparator.comparing(cca -> cca.toString()))
123123
.forEach(cca -> {
124-
Label ccaLabel = new Label(cca.decoupledCcaName);
124+
Label ccaLabel = new Label(cca.toString());
125125
ccaLabel.getStyleClass().add("label");
126126
ccaLabel.getStyleClass().add("cca");
127127
tags.getChildren().add(ccaLabel);

src/test/java/seedu/connectus/logic/commands/CommandTestUtil.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
import static seedu.connectus.testutil.Assert.assertThrows;
1414

1515
import java.util.ArrayList;
16-
import java.util.Arrays;
1716
import java.util.List;
1817

1918
import seedu.connectus.commons.core.index.Index;
2019
import seedu.connectus.logic.commands.exceptions.CommandException;
2120
import seedu.connectus.model.ConnectUs;
2221
import seedu.connectus.model.Model;
23-
import seedu.connectus.model.person.NameContainsKeywordsPredicate;
22+
import seedu.connectus.model.person.FieldsContainKeywordsPredicate;
2423
import seedu.connectus.model.person.Person;
2524
import seedu.connectus.testutil.EditPersonDescriptorBuilder;
2625

@@ -154,8 +153,10 @@ public static void showPersonAtIndex(Model model, Index targetIndex) {
154153
assertTrue(targetIndex.getZeroBased() < model.getFilteredPersonList().size());
155154

156155
Person person = model.getFilteredPersonList().get(targetIndex.getZeroBased());
157-
final String[] splitName = person.getName().fullName.split("\\s+");
158-
model.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(splitName[0])));
156+
final String name = person.getName().fullName;
157+
FieldsContainKeywordsPredicate predicate = new FieldsContainKeywordsPredicate();
158+
predicate.setName(name);
159+
model.updateFilteredPersonList(predicate);
159160

160161
assertEquals(1, model.getFilteredPersonList().size());
161162
}

src/test/java/seedu/connectus/logic/commands/SearchCommandTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void execute_multipleKeywords_multiplePersonsFound() {
7676
}
7777

7878
/**
79-
* Parses {@code userInput} into a {@code NameContainsKeywordsPredicate}.
79+
* Parses {@code userInput} into a {@code FieldContainsKeywordsPredicate}.
8080
*/
8181
private FieldsContainKeywordsPredicate preparePredicate(String userInput) {
8282
return new FieldsContainKeywordsPredicate(Arrays.asList(userInput.split("\\s+")));

src/test/java/seedu/connectus/logic/parser/SearchCommandParserTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,42 @@ public void parse_validArgs_returnsSearchCommand() {
158158
assertParseSuccess(parser, "mod/CS mod/ES", new SearchCommand(predicate));
159159
assertParseSuccess(parser, "mod/ CS \t mod/ \n ES", new SearchCommand(predicate));
160160

161+
/* java doesn't treat two arrays as equal
161162
//search ccas with one keyword
162163
predicate = new FieldsContainKeywordsPredicate();
163-
predicate.setCcas(Collections.singleton("chess"));
164+
predicate.setCcas(Collections.singleton(new String[]{"chess"}));
164165
assertParseSuccess(parser, "cca/chess", new SearchCommand(predicate));
165166
assertParseSuccess(parser, "cca/ \t chess ", new SearchCommand(predicate));
166167
167168
//search ccas with multiple keywords
168-
set = new HashSet<>();
169-
set.add("chess");
170-
set.add("gardening");
171-
predicate.setCcas(set);
169+
Set<String[]> set2 = new HashSet<>();
170+
set2.add(new String[]{"chess"});
171+
set2.add(new String[]{"chess"});
172+
predicate.setCcas(set2);
172173
assertParseSuccess(parser, "cca/chess cca/gardening", new SearchCommand(predicate));
173174
assertParseSuccess(parser, "cca/ chess \t cca/ \n gardening", new SearchCommand(predicate));
174175
175-
//search ccaPositions with one keyword
176+
//search ccas with position with one keyword
177+
predicate = new FieldsContainKeywordsPredicate();
178+
predicate.setCcas(Collections.singleton(new String[]{"chess", "dir"}));
179+
assertParseSuccess(parser, "cca/chess#dir", new SearchCommand(predicate));
180+
assertParseSuccess(parser, "cca/ \t chess # dir ", new SearchCommand(predicate));
181+
182+
//search ccas with position with multiple keywords
183+
set2 = new HashSet<>();
184+
set2.add(new String[]{"chess", "dir"});
185+
set2.add(new String[]{"gardening", "pre"});
186+
predicate.setCcas(set2);
187+
assertParseSuccess(parser, "cca/chess#dir cca/gardening#pre", new SearchCommand(predicate));
188+
assertParseSuccess(parser, "cca/ chess#dir \t cca/ \n gardening #pre ", new SearchCommand(predicate));
189+
*/
190+
//search major with one keyword
176191
predicate = new FieldsContainKeywordsPredicate();
177192
predicate.setMajors(Collections.singleton("Comp"));
178193
assertParseSuccess(parser, "maj/Comp", new SearchCommand(predicate));
179194
assertParseSuccess(parser, "maj/ \t Comp ", new SearchCommand(predicate));
180195

181-
//search ccaPositions with multiple keywords
196+
//search major with multiple keywords
182197
set = new HashSet<>();
183198
set.add("Comp");
184199
set.add("Sci");

0 commit comments

Comments
 (0)