Skip to content

Commit

Permalink
Exception for incorrect formatting and modification to toString() met…
Browse files Browse the repository at this point in the history
…hod in flower
  • Loading branch information
JeffinsonDarmawan committed Apr 15, 2024
1 parent 8017ee2 commit 6a7deff
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 28 deletions.
3 changes: 3 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ Examples of typos that Florizz can rectify include:
- `flowersmothersday`
- `infoDaisy`

Note: Fuzzy Logic only works for commands, flower names, colours and occasions, and it cannot rectify typos which
combine no. 1 with no. 2 or no. 3.

Expected output:

```
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/florizz/core/FlowerDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,32 @@ private static void add(String name, String colour, String[] occasions,
*/
public static void startup() {
add("Orchid", "White", new String[]{"Wedding"}, 10.00,
new String[]{"Innocence", "Respect", "Beauty"}, Flower.Type.FLOWER);
new String[]{"Innocence", "Respect", "Beauty"}, Flower.Type.MAIN_FLOWER);
add("Rose", "Dark Crimson", new String[]{"Funeral"}, 2.00,
new String[]{"Mourning"}, Flower.Type.FLOWER);
new String[]{"Mourning"}, Flower.Type.MAIN_FLOWER);
add("Rose", "Red", new String[]{"Valentines", "Wedding", "Mothers Day"}, 2.00,
new String[]{"Love"}, Flower.Type.FLOWER);
new String[]{"Love"}, Flower.Type.MAIN_FLOWER);
add("Rose", "Yellow", new String[]{}, 2.00,
new String[]{"Jealousy, Decrease of love, Infidelity"}, Flower.Type.FLOWER);
new String[]{"Jealousy, Decrease of love, Infidelity"}, Flower.Type.MAIN_FLOWER);
add("Lily", "White", new String[]{"Funeral", "Wedding"}, 2.50,
new String[]{"Innocence"}, Flower.Type.FLOWER);
new String[]{"Innocence"}, Flower.Type.MAIN_FLOWER);
add("Lily", "Orange", new String[]{}, 2.50,
new String[]{"Hatred"}, Flower.Type.FLOWER);
new String[]{"Hatred"}, Flower.Type.MAIN_FLOWER);
add("Daisy", "White", new String[]{"Valentines"}, 0.50,
new String[]{"Innocence"}, Flower.Type.FLOWER);
new String[]{"Innocence"}, Flower.Type.MAIN_FLOWER);
add("Chrysanthemum", "White", new String[]{"Funeral"}, 1.00,
new String[]{"Love", "Loyalty", "Innocence"}, Flower.Type.FLOWER);
new String[]{"Love", "Loyalty", "Innocence"}, Flower.Type.MAIN_FLOWER);
add("Hydrangea", "Blue", new String[]{"Wedding"}, 9.00,
new String[]{"Forgiveness", "Gratitude"}, Flower.Type.FLOWER);
new String[]{"Forgiveness", "Gratitude"}, Flower.Type.MAIN_FLOWER);
add("Carnation", "Pink", new String[]{"Mothers Day"}, 2.00,
new String[]{"Gratitude", "Love"}, Flower.Type.FLOWER);
new String[]{"Gratitude", "Love"}, Flower.Type.MAIN_FLOWER);
add("Carnation", "Red", new String[]{"Valentines"}, 2.00,
new String[]{"My heart aches", "Deep Love"}, Flower.Type.FLOWER);
new String[]{"My heart aches", "Deep Love"}, Flower.Type.MAIN_FLOWER);
// [Fillers have yet to be implemented]
add("Baby Breath", "White", new String[]{"Wedding", "Valentines", "Mothers Day"}, 1.00,
new String[]{"Innocence", "Kindness", "Care", "Humble"}, Flower.Type.FLOWER);
add("Eucalyptus", "Green", new String[]{"Wedding"}, 1.50, new String[]{"Love", "Kindness"}, Flower.Type.FLOWER);
new String[]{"Innocence", "Kindness", "Care", "Humble"}, Flower.Type.MAIN_FLOWER);
add("Eucalyptus", "Green", new String[]{"Wedding"}, 1.50, new String[]{"Love", "Kindness"}
, Flower.Type.MAIN_FLOWER);
add("Dusty Miller", "Green", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
add("Pistacia", "Green", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
add("Pittosporum", "Green", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
Expand Down
44 changes: 40 additions & 4 deletions src/main/java/florizz/core/FuzzyLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected static String detectItem(String userInput) throws FlorizzException {
}

if (userInput.length() == 1) {
throw new FlorizzException("No matching command/flower/occasion/colour match the input: " + userInput);
throw new FlorizzException("No command/flower/occasion/colour matches the input: " + userInput);
}

String bestMatch = null;
Expand All @@ -107,8 +107,8 @@ protected static String detectItem(String userInput) throws FlorizzException {
} else if (bestDistance == 0) {
return bestMatch;
} else {
logger.log(Level.SEVERE, "No matching command/item/occasion found for input: {0}", userInput);
throw new FlorizzException("No matching command/item/occasion found for input: " + userInput);
logger.log(Level.SEVERE, "No command/flower/occasion/colour matches the input: {0}", userInput);
throw new FlorizzException("No command/flower/occasion/colour matches the input: " + userInput);
}
}

Expand Down Expand Up @@ -240,19 +240,33 @@ protected static String splitAndMergeInput(String userInput) throws FlorizzExcep
correctedInput = arguments[0];
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(info|flowers)")) {
if (arguments.length == 1 && arguments[0].matches("(info)")) {
throw new FlorizzException("Incorrect format for " + arguments[0] + ". Correct format: "
+ arguments[0] + " <flowerName>");
}
if (arguments.length == 1 && arguments[0].matches("(flowers)")) {
throw new FlorizzException("Incorrect format for " + arguments[0] + ". Correct format: "
+ arguments[0] + " <occasion>");
}
if (Objects.equals(arguments[1].toLowerCase(), "mothersday")) {
arguments[1] = "Mothers Day";
}
correctedInput = arguments[0] + " " + arguments[1];
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(new)")) {
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for new. Correct format: new <bouquetName>");
}
bouquetName = userInput.replaceFirst("n", "")
.replaceFirst("e", "")
.replaceFirst("w", "")
.strip();
correctedInput = "new " + bouquetName;
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(delete)")) {
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for delete. Correct format: delete <bouquetName>");
}
bouquetName = userInput.replaceFirst("d", "")
.replaceFirst("e", "")
.replaceFirst("l", "")
Expand All @@ -263,6 +277,9 @@ protected static String splitAndMergeInput(String userInput) throws FlorizzExcep
correctedInput = "delete " + bouquetName;
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(save)")) {
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for save. Correct format: save <bouquetName>");
}
bouquetName = userInput.replaceFirst("s", "")
.replaceFirst("a", "")
.replaceFirst("v", "")
Expand All @@ -271,6 +288,10 @@ protected static String splitAndMergeInput(String userInput) throws FlorizzExcep
correctedInput = "save " + bouquetName;
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(remove)")) {
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for remove. Correct format: remove <flowerName>" +
" /c <colour> /q <quantity> /from <bouquetName>");
}
removeArgument = userInput.replaceFirst("r", "")
.replaceFirst("e", "")
.replaceFirst("m", "")
Expand All @@ -281,14 +302,29 @@ protected static String splitAndMergeInput(String userInput) throws FlorizzExcep
correctedInput = "remove " + removeArgument;
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(add)")) {
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for add. Correct format: add <flowerName>" +
" /c <colour> /q <quantity> /to <bouquetName>");
}
addArgument = userInput.replaceFirst("a", "")
.replaceFirst("d", "")
.replaceFirst("d", "")
.strip();
correctedInput = "add " + addArgument;
uiFuzzy.printFuzzyInputDetection(userInput, correctedInput);
} else if (arguments[0].matches("(compare)")) {
String[] flowerNames = arguments[1].split("/vs/");
if (arguments.length == 1) {
throw new FlorizzException("Incorrect format for compare. Correct format: compare <firstFlowerName>" +
" /vs/ <secondFlowerName>");
}
String[] flowerNames;
if (arguments[1].contains("/vs/")) {
flowerNames = arguments[1].split("/vs/");
} else {
throw new FlorizzException("Incorrect format for compare. Correct format: compare <firstFlowerName>" +
" /vs/ <secondFlowerName>");
}

if (flowerNames.length != 2 || flowerNames[0].length() <= 2 || flowerNames[1].length() <= 2) {
throw new FlorizzException("Please input 2 flowers to compare.");
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ public void printHelpMessage() {
, "Exits the programme"
, "bye")
);
System.out.println(AsciiTable.getTable(tableData, Arrays.asList(
System.out.println(AsciiTable.getTable(AsciiTable.BASIC_ASCII_NO_DATA_SEPARATORS, tableData, Arrays.asList(
new Column().header("No.").dataAlign(HorizontalAlign.CENTER)
.with((TableData data) -> Integer.toString(data.getId())),
new Column().header("Command").dataAlign(HorizontalAlign.LEFT).maxWidth(58)
new Column().header("Command").dataAlign(HorizontalAlign.LEFT)
.with(TableData::getCommand),
new Column().header("Explanation").dataAlign(HorizontalAlign.LEFT).maxWidth(40)
new Column().header("Explanation").dataAlign(HorizontalAlign.LEFT)
.with(TableData::getExplanation),
new Column().header("Example").dataAlign(HorizontalAlign.LEFT)
.with(TableData::getExample))));
Expand Down Expand Up @@ -262,8 +262,8 @@ public void printFlowerInfo(ArrayList<Flower> flowers, String targetFlower, int
int id = 1;

for (Flower flower : flowers) {
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.getOccasion().toString(),
flower.getMeanings().toString(), String.format("%.2f", flower.getPrice())
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.tableOccasionToString(),
flower.tableMeaningToString(), String.format("%.2f", flower.getPrice())
, flower.getType().toString()));
id++;
}
Expand Down Expand Up @@ -545,14 +545,14 @@ public void printCompareFlowers(ArrayList<Flower> firstFilteredFlowers, ArrayLis
int id = 1;

for (Flower flower : firstFilteredFlowers) {
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.getOccasion().toString(),
flower.getMeanings().toString(), String.format("%.2f", flower.getPrice())
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.tableOccasionToString(),
flower.tableMeaningToString(), String.format("%.2f", flower.getPrice())
, flower.getType().toString()));
id++;
}
for (Flower flower : secondFilteredFlowers) {
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.getOccasion().toString(),
flower.getMeanings().toString(), String.format("%.2f", flower.getPrice())
tableData.add(new TableData(id, flower.getFlowerName(), flower.getColour(), flower.tableOccasionToString(),
flower.tableMeaningToString(), String.format("%.2f", flower.getPrice())
, flower.getType().toString()));
id++;
}
Expand All @@ -572,7 +572,7 @@ protected void printFlowersTable(List<TableData> tableData) throws FlorizzExcept
if (tableData.isEmpty()){
throw new FlorizzException("No flowers to display.");
}
System.out.println(AsciiTable.getTable(tableData, Arrays.asList(
System.out.println(AsciiTable.getTable(AsciiTable.BASIC_ASCII_NO_DATA_SEPARATORS, tableData, Arrays.asList(
new Column().header("No.").dataAlign(HorizontalAlign.CENTER)
.with((TableData data) -> Integer.toString(data.getId())),
new Column().header("Flower Name").dataAlign(HorizontalAlign.LEFT)
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/florizz/objects/Flower.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum Occasion {
}

public enum Type {
FLOWER, FILLER
MAIN_FLOWER, FILLER
}

/**
Expand Down Expand Up @@ -236,6 +236,44 @@ public String toString() {
finalMeaning);
}

/**
* Generates a string representation of the occasion of the Flower object for the table.
* @return A string representation of the occasion of the Flower object for the table.
*/
public String tableOccasionToString() {
String finalOccasion;
StringBuilder occasionsString = new StringBuilder();
for (Occasion occasion : occasions){
occasionsString.append(occasionToString(occasion));
occasionsString.append(", ");
}
if (!(occasionsString.toString().isEmpty())) {
finalOccasion = occasionsString.substring(0,occasionsString.lastIndexOf(","));
} else {
finalOccasion = "-";
}
return finalOccasion;
}

/**
* Generates a string representation of the meaning of the Flower object for the table.
* @return A string representation of the meaning of the Flower object for the table.
*/
public String tableMeaningToString() {
String finalMeaning;
StringBuilder meaningsString = new StringBuilder();
for (String meaning : meanings){
meaningsString.append(meaning);
meaningsString.append(", ");
}
if (!(meaningsString.toString().isEmpty())) {
finalMeaning = meaningsString.substring(0,meaningsString.lastIndexOf(","));
} else {
finalMeaning = "-";
}
return finalMeaning;
}

public Type getType() {
return type;
}
Expand Down

0 comments on commit 6a7deff

Please sign in to comment.