diff --git a/src/planmysem/common/Messages.java b/src/planmysem/common/Messages.java index 81c301741..1e1d28ab6 100644 --- a/src/planmysem/common/Messages.java +++ b/src/planmysem/common/Messages.java @@ -1,6 +1,7 @@ package planmysem.common; import java.time.LocalDate; +import java.util.List; import java.util.Map; import java.util.Set; @@ -55,6 +56,7 @@ public static String craftSelectedMessage(Set tags) { sb.append("\n"); count++; } + sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n"); return sb.toString(); } @@ -85,40 +87,29 @@ public static String craftSelectedMessage(String header, /** * Craft selected message via weighted Set of Pairs. */ - public static String craftListMessage(Set pairs) { + public static String craftListMessage(List tries) { StringBuilder sb = new StringBuilder(); - sb.append("Here are the closest matching names/tags: \n"); - int count = 1; - for (WeightedName p : pairs) { - sb.append(count); - sb.append(".\t"); - sb.append(p.getName()); - sb.append("\n"); - count++; - } - sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n"); - - return sb.toString(); - } - - /** - * Craft selected message via weighted Set of Pairs. - */ - public static String craftSelectedMessagePair(Set pairs) { - StringBuilder sb = new StringBuilder(); sb.append("Here are the closest matching names/tags: \n"); int count = 1; - for (WeightedName p : pairs) { - sb.append(count); - sb.append(".\t"); - sb.append(p.getName()); + for (WeightedName wn : tries) { + sb.append("\n"); + sb.append(count + ".\t"); + sb.append("Name: "); + sb.append(wn.getName()); + sb.append(",\n\t"); + sb.append("Date: "); + sb.append(wn.getMap().getKey().toString()); + sb.append(",\n\t"); + sb.append("Start Time: "); + sb.append(wn.getSlot().getStartTime()); + sb.append("\n\t"); + sb.append("Tags: "); + sb.append(wn.getSlot().getTags()); sb.append("\n"); count++; } - sb.append("\nEnter 'list n/{name} OR t/{tag}' to list all slots related to the name/tag\n"); - return sb.toString(); } diff --git a/src/planmysem/logic/commands/FindCommand.java b/src/planmysem/logic/commands/FindCommand.java index a89a0a786..da79985a9 100644 --- a/src/planmysem/logic/commands/FindCommand.java +++ b/src/planmysem/logic/commands/FindCommand.java @@ -2,19 +2,24 @@ package planmysem.logic.commands; import java.time.LocalDate; +import java.util.ArrayList; import java.util.Comparator; +import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Set; -import java.util.TreeSet; + +import javafx.util.Pair; import planmysem.common.Messages; import planmysem.common.Utils; import planmysem.logic.CommandHistory; import planmysem.model.Model; import planmysem.model.semester.Day; +import planmysem.model.semester.ReadOnlyDay; import planmysem.model.semester.WeightedName; +import planmysem.model.slot.ReadOnlySlot; import planmysem.model.slot.Slot; /** @@ -53,21 +58,8 @@ public int compare(WeightedName p1, WeightedName p2) { } }); - private Set selectedNames = new TreeSet<>(new Comparator() { - @Override - public int compare(WeightedName p1, WeightedName p2) { - String n1 = p1.getName(); - String n2 = p2.getName(); - int d1 = p1.getDist(); - int d2 = p2.getDist(); - - if (d1 != d2) { - return d1 - d2; - } else { - return n1.compareTo(n2); - } - } - }); + private List selectedSlots = new ArrayList<>(); + private List>> lastShownList = new ArrayList<>(); public FindCommand(String name, String tag) { this.keyword = (name == null) ? tag.trim() : name.trim(); @@ -79,11 +71,11 @@ public CommandResult execute(Model model, CommandHistory commandHistory) { for (Map.Entry entry : model.getDays().entrySet()) { for (Slot slot : entry.getValue().getSlots()) { if (isFindByName) { - generateDiscoveredNames(keyword, slot.getName()); + generateDiscoveredNames(keyword, slot.getName(), entry, slot); } else { Set tagSet = slot.getTags(); for (String tag : tagSet) { - generateDiscoveredNames(keyword, tag); + generateDiscoveredNames(keyword, tag, entry, slot); } } } @@ -93,19 +85,28 @@ public CommandResult execute(Model model, CommandHistory commandHistory) { return new CommandResult(MESSAGE_SUCCESS_NONE); } - while (!weightedNames.isEmpty() && weightedNames.peek().getDist() < minDistance + 2) { - selectedNames.add(weightedNames.poll()); + while (!weightedNames.isEmpty() && weightedNames.peek().getDist() < minDistance + 1) { + selectedSlots.add(weightedNames.poll()); + } + + for (WeightedName entry : selectedSlots) { + ReadOnlyDay day = entry.getMap().getValue(); + ReadOnlySlot slot = entry.getSlot(); + Pair pair = new Pair<>(day, slot); + lastShownList.add(new Pair<>(entry.getMap().getKey(), pair)); } + model.setLastShownList(lastShownList); - return new CommandResult(String.format(MESSAGE_SUCCESS, selectedNames.size(), - Messages.craftListMessage(selectedNames))); + return new CommandResult(String.format(MESSAGE_SUCCESS, selectedSlots.size(), + Messages.craftListMessage(selectedSlots))); } /** * If a slot entry is found, calculates the Levenshtein Distance between the name and the keyword. * Updates the weightedNames PQ with the new WeightedName pair containing the name and its weight. */ - private void generateDiscoveredNames(String keyword, String compareString) { + private void generateDiscoveredNames(String keyword, String compareString, + Map.Entry entry, Slot slot) { if (compareString == null) { return; } @@ -115,8 +116,8 @@ private void generateDiscoveredNames(String keyword, String compareString) { minDistance = dist; } //System.out.println(compareString + " vs key: " + keyword + " dist: " + dist); - WeightedName distNamePair = new WeightedName(compareString, dist); + WeightedName distNameTrie = new WeightedName(entry, slot, dist); - weightedNames.add(distNamePair); + weightedNames.add(distNameTrie); } } diff --git a/src/planmysem/logic/parser/ParserManager.java b/src/planmysem/logic/parser/ParserManager.java index e7ed8f269..f5e505209 100644 --- a/src/planmysem/logic/parser/ParserManager.java +++ b/src/planmysem/logic/parser/ParserManager.java @@ -16,6 +16,8 @@ import planmysem.logic.commands.HelpCommand; import planmysem.logic.commands.HistoryCommand; import planmysem.logic.commands.ListCommand; +import planmysem.logic.commands.RedoCommand; +import planmysem.logic.commands.UndoCommand; import planmysem.logic.commands.ViewCommand; import planmysem.logic.parser.exceptions.ParseException; @@ -72,6 +74,12 @@ public Command parseCommand(String userInput) throws ParseException { case HistoryCommand.COMMAND_WORD: return new HistoryCommand(); + case UndoCommand.COMMAND_WORD: + return new UndoCommand(); + + case RedoCommand.COMMAND_WORD: + return new RedoCommand(); + case ClearCommand.COMMAND_WORD: return new ClearCommand(); diff --git a/src/planmysem/model/semester/WeightedName.java b/src/planmysem/model/semester/WeightedName.java index a0ee235d4..b2f79e75b 100644 --- a/src/planmysem/model/semester/WeightedName.java +++ b/src/planmysem/model/semester/WeightedName.java @@ -1,5 +1,11 @@ //@@author marcus-pzj package planmysem.model.semester; + +import java.time.LocalDate; +import java.util.Map; + +import planmysem.model.slot.Slot; + /** * WeightedName of integer and string */ @@ -7,17 +13,29 @@ public class WeightedName { private int dist; private String name; + private Map.Entry map; + private Slot slot; - public WeightedName(String name, int dist) { - this.name = name; + public WeightedName(Map.Entry map, Slot slot, int dist) { + this.map = map; this.dist = dist; + this.slot = slot; + this.name = slot.getName(); } public int getDist() { return this.dist; } + public Map.Entry getMap() { + return map; + } + public String getName() { - return name; + return this.name; + } + + public Slot getSlot() { + return this.slot; } }