Skip to content

Commit

Permalink
Compare Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffinsonDarmawan committed Apr 14, 2024
1 parent a7e65bb commit e266939
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
32 changes: 32 additions & 0 deletions src/main/java/florizz/command/CompareCommand.java
Original file line number Diff line number Diff line change
@@ -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<Bouquet> bouquetList, Ui ui) throws FlorizzException {
ArrayList<Flower> firstFilteredFlowers = FlowerDictionary.filterByName(firstFlowerName);
ArrayList<Flower> 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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/florizz/core/FuzzyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()) {
Expand Down
54 changes: 42 additions & 12 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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!");
}
Expand All @@ -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(" ");
Expand Down Expand Up @@ -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 <flowerName1> /vs/ <flowerName2>");
}
outputs[1] = FuzzyLogic.detectItem(flowers[0]);
outputs[2] = FuzzyLogic.detectItem(flowers[1]);
break;
default:
outputs[1] = FuzzyLogic.detectItem(processedInput.substring(firstWhitespace).trim());
break;
Expand All @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/florizz/objects/Flower.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ public Double getPrice () {
return price;
}

public ArrayList<String> getMeanings() {
return meanings;
}
/**
* Generates a string representation of the Flower object.
* @return A string representation of the Flower object.
Expand Down

0 comments on commit e266939

Please sign in to comment.