Skip to content

Commit

Permalink
Added colour option to remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelory committed Apr 9, 2024
1 parent 1172e82 commit 15198b0
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 106 deletions.
41 changes: 1 addition & 40 deletions src/main/java/florizz/command/AddFlowerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce

ArrayList<Flower> matchingFlowers = FlowerDictionary.filterByName(flowerName);

if (matchingFlowers.isEmpty()) {
throw new FlorizzException("Mentioned flower is not in our database." + System.lineSeparator() +
"Check available flowers: `flower` " + System.lineSeparator() +
"Add custom flowers: {{TO BE DONE}}");
}
if (hasColour){
ArrayList<Flower> matchedFlowerAndColour = FlowerDictionary.filterByColour(matchingFlowers, colour);
if (!matchedFlowerAndColour.isEmpty()){
Expand All @@ -73,7 +68,7 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
ui.printAddFlowerSuccess(bouquetList, flowerName, quantity, bouquetName);
}
} else {
Flower flowerToAdd = chooseColour(ui, matchingFlowers, flowerName);
Flower flowerToAdd = ui.chooseColour(matchingFlowers, flowerName);
if (!flowerToAdd.getFlowerName().isBlank()){
bouquetToAddFlower.addFlower(flowerToAdd, this.quantity);
if (enableUi) {
Expand All @@ -89,38 +84,4 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
return true;
}

private Flower chooseColour(Ui ui, ArrayList<Flower> flowers, String flowerName){
ui.printAddFlowerColour(flowers, flowerName);

while (true){
String colourInput = ui.getInput().trim().toLowerCase();
try {
switch (colourInput) {
case "back":
ui.printBackPage();
break;
case "next":
ui.printNextPage();
break;
case "cancel":
return new Flower();
default:
Flower.Colour chosenColour = Flower.stringToColour(colourInput);
ArrayList<Flower> chosenFlowerList = FlowerDictionary.filterByColour(flowers, chosenColour);
if (!chosenFlowerList.isEmpty()){
return chosenFlowerList.get(0);
} else {
throw new FlorizzException("This flower is not available in this colour, " +
"try typing a colour shown above!");
}

}
} catch(FlorizzException error){
ui.printError(error);
} catch(IllegalArgumentException error){
System.out.println("Unrecognised input, type a colour that's available for this flower, " +
"or 'cancel' to stop adding a flower.");
}
}
}
}
49 changes: 33 additions & 16 deletions src/main/java/florizz/command/RemoveFlowerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ public class RemoveFlowerCommand extends Command {
private String flowerName;
private Integer quantity;
private String bouquetName;
private Flower.Colour colour;
private boolean hasColour = false;

public RemoveFlowerCommand(String flowerName, int quantity, String bouquetName) {
this.flowerName = flowerName;
this.quantity = quantity;
this.bouquetName = bouquetName;
}

public RemoveFlowerCommand(String flowerName, Flower.Colour colour, int quantity, String bouquetName) {
this.flowerName = flowerName;
this.quantity = quantity;
this.bouquetName = bouquetName;
this.colour = colour;
hasColour = true;
}
@Override
public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzException {
logger.entering(RemoveFlowerCommand.class.getName(), "execute");
Expand All @@ -39,28 +48,36 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
"Create a bouquet by using 'new <bouquetName>`");
}

boolean doesFlowerExist = false;
Flower flowerToBeAdded = new Flower();
for (int i = 0; !doesFlowerExist && i < FlowerDictionary.size(); i++) {
if (FlowerDictionary.get(i).getFlowerName().toLowerCase().equals(flowerName)) {
flowerToBeAdded = FlowerDictionary.get(i);
doesFlowerExist = true;
}
}
ArrayList<Flower> matchingFlowers = FlowerDictionary.filterByName(
bouquetToRemoveFlower.getFlowerList(), flowerName);

if (!doesFlowerExist) {
throw new FlorizzException("Mentioned flower is not in our database." + System.lineSeparator() +
"Check available flowers: `flower` " + System.lineSeparator() +
"Add custom flowers: {{TO BE DONE}}");
}
if (hasColour){
ArrayList<Flower> matchedFlowerAndColour = FlowerDictionary.filterByColour(matchingFlowers, colour);
if (!matchedFlowerAndColour.isEmpty()){
bouquetToRemoveFlower.removeFlower(matchedFlowerAndColour.get(0),this.quantity);
ui.printRemoveFlowerSuccess(bouquetList, flowerName, quantity, bouquetName);
} else {
throw new FlorizzException("This bouquet does not contain that colour" +
"Type mybouquets to view all your bouquets.");
}
} else if (matchingFlowers.size()==1){
bouquetToRemoveFlower.removeFlower(matchingFlowers.get(0), this.quantity);
ui.printAddFlowerSuccess(bouquetList, flowerName, quantity, bouquetName);

if (bouquetToRemoveFlower.removeFlower(flowerToBeAdded, this.quantity)) {
ui.printRemoveFlowerSuccess(bouquetList, flowerName, quantity, bouquetName);
} else {
ui.printRemoveFlowerUnsuccessful(bouquetList, flowerName, bouquetName);
Flower flowerToRemove = ui.chooseColour(matchingFlowers, flowerName);
if (!flowerToRemove.getFlowerName().isBlank()){
bouquetToRemoveFlower.removeFlower(flowerToRemove, this.quantity);
ui.printRemoveFlowerSuccess(bouquetList, flowerName, quantity, bouquetName);
} else {
ui.printCancelCommand();
}

}

logger.exiting(RemoveFlowerCommand.class.getName(), "execute");
return true;
}


}
13 changes: 13 additions & 0 deletions src/main/java/florizz/core/FlowerDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ public static ArrayList<Flower> filterByName(String name) throws FlorizzExceptio
return filteredFlowers;
}

public static ArrayList<Flower> filterByName(ArrayList<Flower> listOfFlowers, String name) throws FlorizzException {
ArrayList<Flower> filteredFlowers = new ArrayList<>();
for (Flower flower : listOfFlowers) {
if (flower.getFlowerName().toLowerCase().contains(name.toLowerCase())) {
filteredFlowers.add(flower);
}
}
if (filteredFlowers.isEmpty()) {
throw new FlorizzException("Flower name is unidentified.");
}
return filteredFlowers;
}

/**
* Gets a list of flowers that contain the colour search
*
Expand Down
27 changes: 23 additions & 4 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class Parser {

private static final String ADD_FLOWER_AND_COLOUR_REGEX = "(.+)/c(\\s*)(.+)(\\s*)/q(\\s*)(\\d+)(\\s*)/to(.+)";
private static final String REMOVE_FLOWER_REGEX = "(.+)/q(\\s*)(\\d+)(\\s*)/from(.+)";
private static final String REMOVE_FLOWER_AND_COLOUR_REGEX = "(.+)/c(\\s*)(.+)(\\s*)/q(\\s*)(\\d+)(\\s*)/from(.+)";
private static final String PARSE_OCCASION_REGEX = "^\\s*[A-Za-z]+(?:\\s+[A-Za-z]+)?\\s*$";
private static final String PARSE_COLOUR_REGEX = "^\\s*[A-Za-z]+(?:\\s+[A-Za-z]+)?\\s*$";
private static final String SAVE_BOUQUET_REGEX = "^\\s*(yes|no)\\s*$";
Expand Down Expand Up @@ -131,10 +132,14 @@ public static String[] commandHandler(String input) throws FlorizzException {
int secondWhitespace = trimmedArgument.indexOf(" ");
if (secondWhitespace < 0 && outputs[0].equals("remove")){
throw new FlorizzException("Incorrect usage of remove." +
" Correct format: remove <flowerName> /q <quantity> /from <bouquetName>");
" Correct format: remove <flowerName> " +
"/c <flowerColour> (optional) " +
"/q <quantity> /from <bouquetName>");
} else if (secondWhitespace < 0 && outputs[0].equals("add")) {
throw new FlorizzException("Incorrect usage of add." +
" Correct format: add <flowerName> /q <quantity> /to <bouquetName>");
" Correct format: add <flowerName> " +
"/c <flowerColour> (optional) " +
"/q <quantity> /to <bouquetName>");
}
arguments[0] = FuzzyLogic.detectItem(trimmedArgument.substring(0,secondWhitespace));
arguments[1] = trimmedArgument.substring(secondWhitespace).trim();
Expand Down Expand Up @@ -259,6 +264,7 @@ private static AddFlowerCommand handleAddFlower(String argument, boolean enableU
* @throws FlorizzException If the input does not match the required format.
*/
private static RemoveFlowerCommand handleRemoveFlower(String argument) throws FlorizzException {
boolean includeColour = false;
if (argument == null) {
throw new FlorizzException("No argument detected! " +
"Please use the correct format of 'remove <flowerName> /q <quantity> /from <bouquetName>");
Expand All @@ -268,7 +274,9 @@ private static RemoveFlowerCommand handleRemoveFlower(String argument) throws Fl
throw new FlorizzException("Incorrect format detected! " +
"Please use the correct format of 'remove <flowerName> /q <quantity> /from <bouquetName>");
}

if (argument.matches(REMOVE_FLOWER_AND_COLOUR_REGEX)){
includeColour = true;
}
// [WARNING] might need to check for extra slash k

int prefixIndex = argument.indexOf(REMOVE_FLOWER_PREFIX);
Expand All @@ -279,7 +287,18 @@ private static RemoveFlowerCommand handleRemoveFlower(String argument) throws Fl
// [WARNING] might need to check if it's a valid integer
int quantity = Integer.parseInt(quantityString);
String bouquetName = removePrefix(argument.substring(prefixIndex), REMOVE_FLOWER_PREFIX).trim();

if (includeColour){
int colourIndex = argument.indexOf(COLOUR);
try{
flowerName = argument.substring(0,colourIndex).trim();
String colourString = removePrefix(argument.substring(colourIndex, quantityIndex), COLOUR).trim();
Flower.Colour colourToAdd = Flower.stringToColour(colourString);
return new RemoveFlowerCommand(flowerName, colourToAdd, quantity, bouquetName);
} catch(IllegalArgumentException error){
throw new FlorizzException("Tried to add a non recognised colour" +
"Type 'flowers' to view all the currently available flowers and their colours.");
}
}
return new RemoveFlowerCommand(flowerName, quantity, bouquetName);
}

Expand Down
70 changes: 56 additions & 14 deletions src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String getInput(){
*/
public void printBouquetAdded(Bouquet bouquetAdded){
lastCommand = "OTHERS";
System.out.println("Added new bouquet to list: \n" + bouquetAdded);
System.out.println("Added new bouquet to list:\n" + bouquetAdded);
printBreakLine();

}
Expand All @@ -61,7 +61,7 @@ public void printBouquetAdded(Bouquet bouquetAdded){
*/
public void printBouquetDeleted(Bouquet bouquetDeleted){
lastCommand = "OTHERS";
System.out.println("Deleted bouquet: \n" + bouquetDeleted);
System.out.println("Deleted bouquet:\n" + bouquetDeleted);
printBreakLine();

}
Expand Down Expand Up @@ -122,8 +122,10 @@ public void printHelpMessage() {
System.out.println("2. delete <bouquetName> - Delete a bouquets");
System.out.println("3. mybouquets - List current saved bouquets");
System.out.println("4. info <flowerName> - Provide information on chosen flower");
System.out.println("5. add <flowerName> /q <quantity> /to <bouquetName> - add flower to a bouquet");
System.out.println("6. remove <flowerName> /q <quantity> /from <bouquetName> - remove flower from a bouquet");
System.out.println("5. add <flowerName> /c <colour> (optional) /q <quantity> " +
"/to <bouquetName> - add flower to a bouquet");
System.out.println("6. remove <flowerName> /c <colour> (optional) /q <quantity> " +
"/from <bouquetName> - remove flower from a bouquet");
System.out.println("7. flowers - Shows a list of flowers that can be added into mybouquets");
System.out.println("8. flowers <occasion> - Shows a list of flowers associated with said occasion");
System.out.println("9. occasion - Shows a list of occasions associated with available flowers");
Expand Down Expand Up @@ -182,7 +184,7 @@ public void printAllDictFlowerName(int pageNo) {
lastCommand = "ALL_FLOWERS";
int maxPages = (int) Math.ceil((double)lastShownList.size() / PAGE_SIZE);
System.out.println("Showing page " + pageNo + "/" + maxPages
+ " of all the flowers you can add: ");
+ " of all the flowers you can add:");
printFlowerList(false);
}

Expand All @@ -197,7 +199,7 @@ public void printFilteredFlowers(ArrayList<Flower> flowers, String filter, int p
lastCommand = "FILTERED_FLOWERS " + filter;
int maxPages = (int) Math.ceil((double)lastShownList.size() / PAGE_SIZE);
System.out.println("Here is page " + lastPageNo + "/" + maxPages +
" of all the flowers related to " + filter + ": ");
" of all the flowers related to " + filter + ":");
printFlowerList(false);
}

Expand All @@ -212,7 +214,7 @@ public void printFlowerInfo(ArrayList<Flower> flowers, String targetFlower, int
lastCommand = "INFO_FLOWERS " + targetFlower;
int maxPages = (int) Math.ceil((double)lastShownList.size() / PAGE_SIZE);
System.out.println("Here is page " + lastPageNo + "/" + maxPages +
" of info regarding flowers whose name contains " + targetFlower + ": ");
" of info regarding flowers whose name contains " + targetFlower + ":");
printFlowerList(true);
}

Expand Down Expand Up @@ -272,7 +274,7 @@ public void printBackPage() throws FlorizzException{
*/
public void printAllOccasions() {
lastCommand = "OTHERS";
System.out.println("Here are all the occasions associated with the available flowers: ");
System.out.println("Here are all the occasions associated with the available flowers:");
for (Flower.Occasion occasion : Flower.Occasion.values()){
System.out.println("- " + Flower.occasionToString(occasion));
}
Expand All @@ -291,7 +293,7 @@ public void printAllOccasions() {
public void printAddFlowerSuccess(ArrayList<Bouquet> bouquetList,
String flowerName, Integer quantity, String bouquetName) {
lastCommand = "OTHERS";
System.out.println("You have successfully added the following: " + System.lineSeparator() +
System.out.println("You have successfully added the following:" + System.lineSeparator() +
" - " + quantity + " x " + flowerName + " -> Bouquet: " + bouquetName);
printAllBouquets(bouquetList);
}
Expand All @@ -307,7 +309,7 @@ public void printAddFlowerSuccess(ArrayList<Bouquet> bouquetList,
public void printRemoveFlowerSuccess(ArrayList<Bouquet> bouquetList,
String flowerName, Integer quantity, String bouquetName) {
lastCommand = "OTHERS";
System.out.println("You have successfully removed the following: " + System.lineSeparator() +
System.out.println("You have successfully removed the following:" + System.lineSeparator() +
" - " + quantity + " x " + flowerName + " -> Bouquet: " + bouquetName);
printAllBouquets(bouquetList);
}
Expand Down Expand Up @@ -361,7 +363,7 @@ public String printAskColour(ArrayList<Flower> eligibleFlowers) {
System.out.println("What colour would you like your bouquets to be?");

// print all available colours in a given array list
System.out.println("Here is the list of colours available for the occasion: ");
System.out.println("Here is the list of colours available for the occasion:");
// remove duplicate colours in eligible flowers
ArrayList<String> colourList = new ArrayList<>();
for (Flower flower : eligibleFlowers) {
Expand Down Expand Up @@ -393,9 +395,49 @@ public void printSaveSuccess(String bouquetName) {
printBreakLine();
}

public void printAddFlowerColour(ArrayList<Flower> flowers, String flowerName){
System.out.println("The flower you're looking to add has more than one colour available, " +
"each with their own vastly different meanings. Here's some info: ");
/**
* Asks the user to choose a colour of a flower from a list of the flowers.
* @param flowers The list of flowers where the user can choose the colour from
* @param flowerName The name of the flower that the user is trying to choose its colour from
* @return Flower the specific Flower with the correct colour. Is blank if user chose to cancel the command instead
*/
public Flower chooseColour(ArrayList<Flower> flowers, String flowerName){
printGetFlowerColour(flowers, flowerName);

while (true){
String colourInput = getInput().trim().toLowerCase();
try {
switch (colourInput) {
case "back":
printBackPage();
break;
case "next":
printNextPage();
break;
case "cancel":
return new Flower();
default:
Flower.Colour chosenColour = Flower.stringToColour(colourInput);
ArrayList<Flower> chosenFlowerList = FlowerDictionary.filterByColour(flowers, chosenColour);
if (!chosenFlowerList.isEmpty()){
return chosenFlowerList.get(0);
} else {
throw new FlorizzException("This flower is not available in this colour, " +
"try typing a colour shown above!");
}

}
} catch(FlorizzException error){
printError(error);
} catch(IllegalArgumentException error){
System.out.println("Unrecognised input, type a colour that's available for this flower, " +
"or 'cancel' to go back to the main menu.");
}
}
}
public void printGetFlowerColour(ArrayList<Flower> flowers, String flowerName){
System.out.println("The flower you're looking for has more than one colour available, " +
"each with their own vastly different meanings. Here's some info:");
printFlowerInfo(flowers, flowerName, 1);
System.out.println("Type the colour you want to add into the bouquet, or 'cancel' to return to the main menu.");
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/florizz/objects/Bouquet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import florizz.core.FlorizzException;

import java.util.ArrayList;
import java.util.HashMap;

import java.util.Objects;
Expand Down Expand Up @@ -122,4 +123,8 @@ public String getBouquetName() {
public void setName(String newName) {
this.bouquetName = newName;
}

public ArrayList<Flower> getFlowerList (){
return new ArrayList<Flower> (flowerHashMap.keySet());
}
}
Loading

0 comments on commit 15198b0

Please sign in to comment.