forked from nusCS2113-AY1819S2/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eee2b88
commit ad0c59e
Showing
4 changed files
with
84 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,90 @@ | ||
package planmysem.commands; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.regex.Pattern; | ||
|
||
import javafx.util.Pair; | ||
|
||
import planmysem.data.semester.Day; | ||
import planmysem.data.slot.ReadOnlySlot; | ||
import planmysem.data.slot.Slot; | ||
|
||
|
||
/** | ||
* Finds all slots and tags in planner whose name directly matches any of the argument keywords. | ||
* Finds all slots in planner whose name contains the argument keyword. | ||
* Keyword matching is case sensitive. | ||
*/ | ||
public class FindCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "find"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + "Finds all slots and tags which directly " | ||
+ "matches the specified keywords (case-sensitive).\n\t" | ||
public static final String COMMAND_WORD_SHORT = "f"; | ||
public static final String MESSAGE_SUCCESS = "%1$s Slots listed.\n%2$s"; | ||
public static final String MESSAGE_SUCCESS_NONE = "0 Slots listed.\n"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + "Finds all slots whose name " | ||
+ "contains the specified keywords (case-sensitive).\n\t" | ||
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n\t" | ||
+ "Example: " + COMMAND_WORD + " CS2113T"; | ||
+ "Example: " + COMMAND_WORD + "CS2113T"; | ||
|
||
private final Set<String> keywords; | ||
|
||
public FindCommand(Set<String> keywords) { | ||
this.keywords = keywords; | ||
} | ||
|
||
/** | ||
* Returns copy of keywords in this command. | ||
*/ | ||
public Set<String> getKeywords() { | ||
return new HashSet<>(keywords); | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
String result = getSlotsWithTag(keywords).stream().map(Object::toString) | ||
.collect(Collectors.joining(", ")); | ||
return new CommandResult(result); | ||
} | ||
final List<Pair<LocalDate, ? extends ReadOnlySlot>> relevantSlots = new ArrayList<>(); | ||
List<Slot> matchedSlots = new ArrayList<>(); | ||
LocalDate date; | ||
|
||
/** | ||
* Retrieve all slot in the semesters of the planner whose slots contain some of the specified keywords. | ||
* | ||
* @param keywords for searching | ||
* @return list of persons found | ||
*/ | ||
private List<Slot> getSlotsWithTag(Set<String> keywords) { | ||
List<Slot> test = new ArrayList<>(); | ||
for (Day days : planner.getSemester().getDays().values()) { | ||
for (Slot slots : days.getSlots()) { | ||
for (Map.Entry<LocalDate, Day> entry : planner.getSemester().getDays().entrySet()) { | ||
for (Slot slots : entry.getValue().getSlots()) { | ||
for (String keyword : keywords) { | ||
if (slots.getName().value.equalsIgnoreCase(keyword)) { | ||
test.add(slots); | ||
if (Pattern.matches(".*" + keyword + ".*", slots.getName().value)) { | ||
if (!matchedSlots.contains(slots)) { | ||
matchedSlots.add(slots); | ||
date = entry.getKey(); | ||
relevantSlots.add(new Pair<>(date, slots)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return test; | ||
|
||
if (matchedSlots.isEmpty()) { | ||
return new CommandResult(MESSAGE_SUCCESS_NONE); | ||
} | ||
setData(this.planner, relevantSlots); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, matchedSlots.size(), | ||
craftSuccessMessage(relevantSlots))); | ||
} | ||
|
||
/** | ||
* Craft success message. | ||
*/ | ||
private String craftSuccessMessage(List<Pair<LocalDate, ? extends ReadOnlySlot>> result) { | ||
int count = 1; | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
for (Pair<LocalDate, ? extends ReadOnlySlot> pair : result) { | ||
sb.append("\n"); | ||
sb.append(count + ".\t"); | ||
sb.append("Name: "); | ||
sb.append(pair.getValue().getName().toString()); | ||
sb.append(",\n\t"); | ||
sb.append("Date: "); | ||
sb.append(pair.getKey().toString()); | ||
sb.append(",\n\t"); | ||
sb.append("Start Time: "); | ||
sb.append(pair.getValue().getStartTime()); | ||
sb.append("\n"); | ||
count++; | ||
} | ||
return sb.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters