diff --git a/src/main/java/florizz/command/CompareCommand.java b/src/main/java/florizz/command/CompareCommand.java new file mode 100644 index 0000000000..098500161e --- /dev/null +++ b/src/main/java/florizz/command/CompareCommand.java @@ -0,0 +1,32 @@ +package florizz.command; + +import florizz.core.FlorizzException; +import florizz.core.FlowerDictionary; +import florizz.core.Ui; +import florizz.objects.Bouquet; +import florizz.objects.Flower; + +import java.util.ArrayList; + +public class CompareCommand extends Command { + + private String firstFlowerName; + private String secondFlowerName; + + public CompareCommand(String firstFlowerName, String secondFlowerName) { + this.firstFlowerName = firstFlowerName; + this.secondFlowerName = secondFlowerName; + } + + public boolean execute(ArrayList bouquetList, Ui ui) throws FlorizzException { + ArrayList firstFilteredFlowers = FlowerDictionary.filterByName(firstFlowerName); + ArrayList secondFilteredFlowers = FlowerDictionary.filterByName(secondFlowerName); + + if (firstFilteredFlowers.isEmpty() || secondFilteredFlowers.isEmpty()) { + throw new FlorizzException("Flower does not exist, type 'flowers' for a list of flowers"); + } + + ui.printCompareFlowers(firstFilteredFlowers, secondFilteredFlowers); + return true; + } +} diff --git a/src/main/java/florizz/core/FuzzyLogic.java b/src/main/java/florizz/core/FuzzyLogic.java index d297654e77..44ef0fa2d1 100644 --- a/src/main/java/florizz/core/FuzzyLogic.java +++ b/src/main/java/florizz/core/FuzzyLogic.java @@ -34,6 +34,7 @@ public class FuzzyLogic { ITEMS.put("recommend", "Command: Get a recommended bouquet"); ITEMS.put("next", "Command: Goes to next page"); ITEMS.put("back", "Command: Goes to previous page"); + ITEMS.put("compare", "Command: Compare two flowers"); ITEMS.put("Orchid", "Flower"); ITEMS.put("Rose", "Flower"); ITEMS.put("Lily", "Flower"); @@ -182,6 +183,7 @@ protected static String processCommand(String userInput) throws FlorizzException || userInput.startsWith("remove") || userInput.startsWith("add") || userInput.startsWith("info") + || userInput.startsWith("compare") || userInput.startsWith("flowers"))) { String[] arguments = new String[2]; arguments[0] = userInput.substring(0,firstWhitespace).toLowerCase(); @@ -273,6 +275,18 @@ protected static String splitAndMergeInput(String userInput) throws FlorizzExcep .strip(); correctedInput = "add " + addArgument; uiFuzzy.printFuzzyInputDetection(userInput, correctedInput); + } else if (arguments[0].matches("(compare)")) { + String argument = userInput.replaceFirst("c", "") + .replaceFirst("o", "") + .replaceFirst("m", "") + .replaceFirst("p", "") + .replaceFirst("a", "") + .replaceFirst("r", "") + .replaceFirst("e", "") + .strip(); + String[] flowerNames = argument.split("/vs/"); + correctedInput = "compare " + flowerNames[0] + " /vs/ " + flowerNames[1]; + uiFuzzy.printFuzzyInputDetection(userInput, correctedInput); } else { correctedInput = userInput; } @@ -320,6 +334,9 @@ protected static String splitInput(String userInput) throws FlorizzException { } else if (userInput.matches("(save)[a-zA-Z].+")) { String argumentSave = userInput.replaceFirst("save", ""); correctedInput = "save " + argumentSave; + } else if (userInput.matches("(compare)[a-zA-Z].+")) { + String argumentCompare = userInput.replaceFirst("compare", ""); + correctedInput = "compare " + argumentCompare; } if (correctedInput.isEmpty()) { diff --git a/src/main/java/florizz/core/Parser.java b/src/main/java/florizz/core/Parser.java index b1954ab5bc..d860470bdb 100644 --- a/src/main/java/florizz/core/Parser.java +++ b/src/main/java/florizz/core/Parser.java @@ -1,20 +1,21 @@ package florizz.core; -import florizz.command.InfoCommand; -import florizz.command.HelpCommand; -import florizz.command.SaveCommand; -import florizz.command.FlowerCommand; -import florizz.command.ExitCommand; -import florizz.command.RemoveFlowerCommand; -import florizz.command.ListOccasionCommand; -import florizz.command.ListBouquetCommand; -import florizz.command.DeleteBouquetCommand; -import florizz.command.AddFlowerCommand; -import florizz.command.Command; import florizz.command.AddBouquetCommand; +import florizz.command.AddFlowerCommand; import florizz.command.BackCommand; +import florizz.command.Command; +import florizz.command.CompareCommand; +import florizz.command.DeleteBouquetCommand; +import florizz.command.ExitCommand; +import florizz.command.FlowerCommand; +import florizz.command.HelpCommand; +import florizz.command.InfoCommand; +import florizz.command.ListBouquetCommand; +import florizz.command.ListOccasionCommand; import florizz.command.NextCommand; import florizz.command.RecommendCommand; +import florizz.command.RemoveFlowerCommand; +import florizz.command.SaveCommand; import florizz.objects.Bouquet; import florizz.objects.Flower; @@ -94,6 +95,9 @@ public static Command parse (String input, boolean enableUi) throws FlorizzExcep case ("save"): command = new SaveCommand(decodedInput[1]); break; + case ("compare"): + command = handleCompareCommand(decodedInput[1], decodedInput[2]); + break; default: throw new FlorizzException("Unidentified input, type help to get a list of all commands!"); } @@ -115,7 +119,7 @@ public static Command parse (String input, boolean enableUi) throws FlorizzExcep * @return String[] output; output[0] = item ; output[1] = argument(s) */ public static String[] commandHandler(String input) throws FlorizzException { - String[] outputs = new String[2]; + String[] outputs = new String[3]; String trimmedInput = input.trim(); String processedInput = FuzzyLogic.processCommand(trimmedInput); int firstWhitespace = processedInput.indexOf(" "); @@ -148,6 +152,16 @@ public static String[] commandHandler(String input) throws FlorizzException { arguments[1] = trimmedArgument.substring(firstSlash).trim(); outputs[1] = arguments[0] + " " + arguments[1]; break; + case ("compare"): + outputs[1] = processedInput.substring(firstWhitespace).trim(); + String[] flowers = outputs[1].split(" /vs/ "); + if (flowers.length != 2) { + throw new FlorizzException("Incorrect usage of compare." + + " Correct format: compare /vs/ "); + } + outputs[1] = FuzzyLogic.detectItem(flowers[0]); + outputs[2] = FuzzyLogic.detectItem(flowers[1]); + break; default: outputs[1] = FuzzyLogic.detectItem(processedInput.substring(firstWhitespace).trim()); break; @@ -162,6 +176,22 @@ public static String[] commandHandler(String input) throws FlorizzException { return outputs; } + /** + * Handles the parsing and creation of a CompareCommand object based on user input. + * + * @param firstFlowerName The name of the first flower to compare. + * @param secondFlowerName The name of the second flower to compare. + * @return A CompareCommand object corresponding to the parsed input. + * @throws FlorizzException If the input does not contain the required flower names. + */ + private static CompareCommand handleCompareCommand(String firstFlowerName, String secondFlowerName) + throws FlorizzException { + if (firstFlowerName == null || secondFlowerName == null) { + throw new FlorizzException("Please specify two flowers to compare!"); + } + return new CompareCommand(firstFlowerName, secondFlowerName); + } + /** * remove prefix from an input string * e.g. "/to For Mom" -> " For Mom" diff --git a/src/main/java/florizz/objects/Flower.java b/src/main/java/florizz/objects/Flower.java index 67046fa08d..b647bba6aa 100644 --- a/src/main/java/florizz/objects/Flower.java +++ b/src/main/java/florizz/objects/Flower.java @@ -194,6 +194,9 @@ public Double getPrice () { return price; } + public ArrayList getMeanings() { + return meanings; + } /** * Generates a string representation of the Flower object. * @return A string representation of the Flower object.