Skip to content

Commit

Permalink
Merge pull request #175 from IanFH/bug-fixes-2
Browse files Browse the repository at this point in the history
Bug fixes 2
  • Loading branch information
JeffinsonDarmawan committed Apr 15, 2024
2 parents f859bbc + 4da42be commit eb9e5bb
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 42 deletions.
103 changes: 91 additions & 12 deletions src/main/java/florizz/command/RecommendCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
import java.util.logging.Logger;

public class RecommendCommand extends Command{
private Logger logger = Logger.getLogger(RecommendCommand.class.getName());
private static final int SMALL_BOUQUET_FLOWER_COUNT = 1;
private static final int MEDIUM_BOUQUET_FLOWER_COUNT = 3;
private static final int LARGE_BOUQUET_FLOWER_COUNT = 5;
private static final int SMALL_BOUQUET_FILLER_COUNT = 2;
private static final int MEDIUM_BOUQUET_FILLER_COUNT = 4;
private static final int LARGE_BOUQUET_FILLER_COUNT = 6;
private final Logger logger = Logger.getLogger(RecommendCommand.class.getName());
@Override
public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzException {
logger.entering(RecommendCommand.class.getName(), "execute");
Expand All @@ -35,13 +41,14 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce
// ask for bouquetName
String bouquetName = askBouquetName(ui, bouquetList);

// ask for size [FUTURE IMPLEMENTATION]
// ask for size
String size = askSize(ui);

// create bouquet with occasion and colour
Bouquet recommendedBouquet = new Bouquet(bouquetName);

// randomly add 3 flowers to bouquet
addRandomFlowers(eligibleFlowers, recommendedBouquet);
addRandomFlowers(eligibleFlowers, recommendedBouquet, size, colour);

// ask if they want to save bouquet to array
askSaveBouquet(ui, bouquetList, recommendedBouquet);
Expand Down Expand Up @@ -77,23 +84,61 @@ public String askBouquetName(Ui ui, ArrayList<Bouquet> bouquetList) throws Flori
* @param eligibleFlowers list of flowers to choose from
* @param recommendedBouquet bouquet to add flowers to
*/
private void addRandomFlowers(ArrayList<Flower> eligibleFlowers, Bouquet recommendedBouquet)
public void addRandomFlowers(ArrayList<Flower> eligibleFlowers,
Bouquet recommendedBouquet, String size, Flower.Colour colour)
throws FlorizzException {
logger.entering(RecommendCommand.class.getName(), "addRandomFlowers");
// [TEMPORARY CODE]

int flowerCount = 0;
int fillerCount = 0;
switch (size) {
case "small":
flowerCount = SMALL_BOUQUET_FLOWER_COUNT;
fillerCount = SMALL_BOUQUET_FILLER_COUNT;
break;
case "medium":
flowerCount = MEDIUM_BOUQUET_FLOWER_COUNT;
fillerCount = MEDIUM_BOUQUET_FILLER_COUNT;
break;
case "large":
flowerCount = LARGE_BOUQUET_FLOWER_COUNT;
fillerCount = LARGE_BOUQUET_FILLER_COUNT;
break;
default:
// this should not happen at any case since size is validated before this
assert false : "Invalid size";
throw new FlorizzException("Invalid size");
}

// generate random combination of flowers from eligible flowers totaling to 5
for (int i = 0; i < 5; i++) {
for (int i = 0; i < flowerCount; i++) {
int randomIndex = (int) (Math.random() * eligibleFlowers.size());
recommendedBouquet.addFlower(eligibleFlowers.get(randomIndex), 1);
}

// get all fillers
ArrayList<Flower> fillers = FlowerDictionary.getFlowersByType(Flower.Type.FILLER_FLOWER);
for (int i = 0; i < fillerCount; i++) {
int randomIndex = (int) (Math.random() * fillers.size());
// filler colour must be green or same as main flower
boolean isSameColour = fillers.get(randomIndex).getColour().equalsIgnoreCase(colour.toString());
boolean isGreen = fillers.get(randomIndex).getColour().equalsIgnoreCase("green");
if (isSameColour || isGreen) {
recommendedBouquet.addFlower(fillers.get(randomIndex), 1);
} else {
i--;
}
}


logger.exiting(RecommendCommand.class.getName(), "addRandomFlowers");
}

/**
* Asks user for occasion
* @return Occasion enum
*/
private Flower.Occasion askOccasion(Ui ui) throws FlorizzException {
public Flower.Occasion askOccasion(Ui ui) throws FlorizzException {
logger.entering(RecommendCommand.class.getName(), "askOccasion");
boolean isValidFormat = false;
boolean isValidOccasion = false;
Expand All @@ -106,8 +151,9 @@ private Flower.Occasion askOccasion(Ui ui) throws FlorizzException {
isValidOccasion = Flower.isValidOccasion(occasionInput);
// check if occasion is in our dictionary
if (!isValidOccasion) {
System.out.println("This occasion does not exist." +
"Type 'cancel' if you would like to exit the recommendation page");
System.out.println("This occasion does not exist.");
Ui.printBreakLine();
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
}
}

Expand All @@ -129,22 +175,29 @@ private Flower.Colour askColour(Ui ui, ArrayList<Flower> eligibleFlowers) throws
boolean isValidColour = false;
ui.printAskColour(eligibleFlowers);
while (!(isValidColour && isValidFormat)) {
colourInput = ui.getInput();
colourInput = ui.getInput().trim();
Parser.checkRecommendExitCondition(colourInput);
isValidFormat = Parser.parseColour(colourInput);
isValidColour = Flower.isValidColour(colourInput);

// check if colour is in our dictionary
if (!isValidColour) {
System.out.println("This colour does not exist. " +
"Type 'cancel' if you would like to exit the recommendation page");
System.out.println("This colour does not exist.");
Ui.printBreakLine();
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
}
}

logger.exiting(RecommendCommand.class.getName(), "askColour");
return Flower.stringToColour(colourInput);
}

/**
* Asks user if they want to save the bouquet
* @param ui Ui to take input and print messages
* @param bouquetList List that contains all bouquets
* @param recommendedBouquet Bouquet to be saved
*/
private void askSaveBouquet(Ui ui, ArrayList<Bouquet> bouquetList,
Bouquet recommendedBouquet) {
logger.entering(RecommendCommand.class.getName(), "askSaveBouquet");
Expand All @@ -162,6 +215,32 @@ private void askSaveBouquet(Ui ui, ArrayList<Bouquet> bouquetList,
ui.printBouquetAdded(recommendedBouquet);
assert !bouquetList.isEmpty() : "Bouquet list should not be empty";
}

if (saveInput.equalsIgnoreCase("no")) {
ui.printBouquetNotAdded(recommendedBouquet);
}

logger.exiting(RecommendCommand.class.getName(), "askSaveBouquet");
}

/**
* Asks user for size of bouquet
* @param ui Ui to take input and print messages
* @return size of bouquet in lower case string
*/
private String askSize(Ui ui) {
logger.entering(RecommendCommand.class.getName(), "askSize");
String sizeInput = "placeHolder";
boolean isValidFormat = false;
boolean isValidInput = false;
while (!(isValidInput && isValidFormat)) {
sizeInput = ui.printAskSize().toLowerCase();
isValidFormat = Parser.parseSize(sizeInput);
isValidInput = (sizeInput.equalsIgnoreCase("small") ||
sizeInput.equalsIgnoreCase("medium") ||
sizeInput.equalsIgnoreCase("large"));
}
logger.exiting(RecommendCommand.class.getName(), "askSize");
return sizeInput.toLowerCase();
}
}
42 changes: 26 additions & 16 deletions src/main/java/florizz/core/FlowerDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,23 @@ public static void startup() {
// [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.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);
add("Chamomile", "White", new String[]{}, 1.90, new String[]{}, Flower.Type.FILLER);
add("Astilbe", "Pink", new String[]{}, 2.80, new String[]{}, Flower.Type.FILLER);
add("Hypericum", "Red", new String[]{}, 2.00, new String[]{}, Flower.Type.FILLER);
add("Freesia", "White", new String[]{}, 1.90, new String[]{}, Flower.Type.FILLER);
add("Helichrysum", "Yellow", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
add("Limonium", "Red", new String[]{}, 1.80, new String[]{}, Flower.Type.FILLER);
add("Limonium", "Dark Crimson", new String[]{}, 1.80, new String[]{}, Flower.Type.FILLER);
add("Limonium Perezii", "Purple", new String[]{}, 1.80, new String[]{}, Flower.Type.FILLER);
add("Statice", "Blue", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
add("Statice", "Purple", new String[]{}, 1.50, new String[]{}, Flower.Type.FILLER);
add("Rice Flower", "Pink", new String[]{}, 1.80, new String[]{}, Flower.Type.FILLER);

add("Eucalyptus", "Green", new String[]{"Wedding"}, 1.5,
new String[]{"Love", "Kindness"}, Flower.Type.MAIN_FLOWER);
add("Dusty Miller", "Green", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Pistacia", "Green", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Pittosporum", "Green", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Chamomile", "White", new String[]{}, 1.9, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Astilbe", "Pink", new String[]{}, 2.8, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Hypericum", "Red", new String[]{}, 2.0, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Freesia", "White", new String[]{}, 1.9, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Helichrysum", "Yellow", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Limonium", "Red", new String[]{}, 1.8, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Limonium", "Dark Crimson", new String[]{}, 1.8, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Limonium Perezii", "Purple", new String[]{}, 1.8, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Statice", "Blue", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Statice", "Purple", new String[]{}, 1.5, new String[]{}, Flower.Type.FILLER_FLOWER);
add("Rice Flower", "Pink", new String[]{}, 1.8, new String[]{}, Flower.Type.FILLER_FLOWER);
}

/**
Expand Down Expand Up @@ -180,6 +181,15 @@ public static ArrayList<Flower> filterByColour(ArrayList<Flower> listOfFlowers,

public static ArrayList<Flower> getAllFlowers(){
return flowerDict;
}

public static ArrayList<Flower> getFlowersByType(Flower.Type type) {
ArrayList<Flower> filteredFlowers = new ArrayList<>();
for (Flower flower : flowerDict) {
if (flower.getType().equals(type)) {
filteredFlowers.add(flower);
}
}
return filteredFlowers;
}
}
32 changes: 27 additions & 5 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class Parser {
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*$";
private static final String PARSE_SIZE_REGEX = "^\\s*[A-Za-z]+\\s*$";

public static Command parse (String input, boolean enableUi) throws FlorizzException {
logger.entering("Parser", "parse");
Expand Down Expand Up @@ -368,13 +369,13 @@ private static InfoCommand handleInfoCommand(String flowerName) throws FlorizzEx
* @return The parsed occasion.
*/
public static boolean parseOccasion(String argument) throws FlorizzException {
if (argument == null) {
if (argument == null || argument.isEmpty()) {
System.out.println("No argument detected! " +
"Please input an occasion");
return false;
}
String detectedOccasion = FuzzyLogic.detectItem(argument);
if (detectedOccasion.isEmpty()) {

if (!argument.matches(PARSE_OCCASION_REGEX)) {
System.out.println("Incorrect format detected! " +
"Please input a single occasion");
return false;
Expand All @@ -389,7 +390,7 @@ public static boolean parseOccasion(String argument) throws FlorizzException {
* @return The parsed colour String
*/
public static boolean parseColour(String argument) {
if (argument == null) {
if (argument == null || argument.isEmpty()) {
System.out.println("No argument detected! " +
"Please input a colour");
return false;
Expand All @@ -410,7 +411,7 @@ public static boolean parseColour(String argument) {
* @return The parsed save bouquet String
*/
public static boolean parseSaveBouquet(String argument) {
if (argument == null) {
if (argument == null || argument.isEmpty()) {
System.out.println("No argument detected! " +
"Please input a bouquet name to save");
return false;
Expand All @@ -435,4 +436,25 @@ public static void checkRecommendExitCondition(String input) throws FlorizzExcep
throw new FlorizzException("Leaving recommend");
}
}

/**
* Parses the size from the user input.
* @param argument the user input to be parsed.
* @return whether it is a valid size format
*/
public static boolean parseSize(String argument) {
if (argument == null || argument.isEmpty()) {
System.out.println("No argument detected! " +
"Please input a size of either small, medium or large");
return false;
}

if (!argument.matches(PARSE_SIZE_REGEX)) {
System.out.println("Incorrect format detected! " +
"Please input a single size");
return false;
}

return true;
}
}
28 changes: 25 additions & 3 deletions src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,15 @@ public String getInput(){
* Prints the message indicating a new bouquet has been added.
* @param bouquetAdded The bouquet that has been added.
*/
public void printBouquetAdded(Bouquet bouquetAdded){
public void printBouquetAdded(Bouquet bouquetAdded) {
lastCommand = "OTHERS";
System.out.println("Added new bouquet to list:\n" + bouquetAdded);
printBreakLine();
}

public void printBouquetNotAdded(Bouquet bouquetDiscarded) {
System.out.println("Discarded the bouquet: " + bouquetDiscarded);
printBreakLine();
}

/**
Expand Down Expand Up @@ -434,7 +438,9 @@ public void printAskOccasion() {
public void printAskBouquetName() {
System.out.println("Great we managed to find some flowers for you!");
System.out.println("Before we carry on what would you like to call your bouquet?");
System.out.println("Note: please take note 'cancel' cannot be used as a bouquet name");
System.out.println("Note: please take note 'cancel' cannot be used as a bouquet name");
printBreakLine();
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
}

/**
Expand All @@ -456,8 +462,8 @@ public void printAskColour(ArrayList<Flower> eligibleFlowers) {
for (String colour : colourList){
System.out.println("- " + colour);
}
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
printBreakLine();
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
}

/**
Expand All @@ -471,6 +477,7 @@ public String printAskSaveBouquet(Bouquet recommendedBouquet) {
System.out.println("Would you like to save this bouquet to your list?");
printFullBouquet(recommendedBouquet);
System.out.println("Type 'yes' to save, 'no' to discard");
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
return inputScanner.nextLine();
}

Expand Down Expand Up @@ -600,5 +607,20 @@ protected void printFlowersTable(List<TableData> tableData) throws FlorizzExcept
public void printCancelCommand(){
System.out.println("Canceled command, returning to main menu.");
}

/**
* Prints text to ask user for size of bouquet
* @return raw string of user input
*/
public String printAskSize() {
System.out.println("What size would you like your recommended bouquet to be?" + System.lineSeparator() +
"1. Small" + System.lineSeparator() +
"2. Medium" + System.lineSeparator() +
"3. Large");
printBreakLine();
System.out.println("Type 'cancel' if you would like to exit the recommendation page");
return inputScanner.nextLine().trim();
}

}

12 changes: 12 additions & 0 deletions src/main/java/florizz/objects/Bouquet.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public boolean equals(Object obj) {
return Objects.equals(c.bouquetName.toUpperCase(), this.bouquetName.toUpperCase());
}

/**
* Get total quantity of flowers in the bouquet
* @return int total quantity of flowers
*/
public int totalNumberOfFlowers() {
int quantity = 0;
for (Flower flower : flowerHashMap.keySet()) {
quantity += flowerHashMap.get(flower);
}
return quantity;
}

/**
* Checks if a flower exists in the bouquet.
*
Expand Down
Loading

0 comments on commit eb9e5bb

Please sign in to comment.