Skip to content

Commit

Permalink
Merge pull request #89 from samuelory/Samuel-V2.1
Browse files Browse the repository at this point in the history
Refactored and updated Info Command to support flowers with different meanings for different colours
  • Loading branch information
Ijaaz01 authored Apr 2, 2024
2 parents a426e28 + 5203d5d commit ae9d470
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 20 deletions.
11 changes: 8 additions & 3 deletions src/main/java/florizz/command/InfoCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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;
import java.util.logging.ConsoleHandler;
Expand All @@ -21,11 +23,14 @@ public InfoCommand(String flowerName) {

@Override
public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzException {
boolean flowerExists = ui.printFlowerInfo(flowerName);
if (!flowerExists) {
ArrayList<Flower> filteredFlowers = FlowerDictionary.filterByName(flowerName);
if (filteredFlowers.isEmpty()){
logger.log(Level.WARNING, "FLOWER DOES NOT EXIST");
throw new FlorizzException("Flower does not exist type 'flower' for a list of flowers");
throw new FlorizzException("Flower does not exist, type 'flowers' for a list of flowers");
} else{
ui.printFlowerInfo(filteredFlowers, flowerName);
}

return true;
}
}
27 changes: 24 additions & 3 deletions src/main/java/florizz/core/FlowerDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ private static void add(String name, String colour, String[] occasions, Double p
*/
public static void startup() {
add("Orchid", "White", new String[]{"Wedding"}, 10.00, new String[]{"Innocence","Respect","Beauty"});
add("Rose", "Dark Crimson", new String[]{"Funeral"}, 2.00, new String[]{"Mourning"});
add("Rose", "Red", new String[]{"Valentines", "Wedding", "Mothers Day"}, 2.00, new String[]{"Love"});
add("Lily", "White", new String[]{"Funeral", "Wedding"}, 2.50, new String[]{"Innocence"});
add("Daisy", "White", new String[]{"Valentines"}, 0.50, new String[]{"Innocence"});
Expand Down Expand Up @@ -65,11 +66,31 @@ public static Flower get(int i) {
return flowerDict.get(i);
}

/**
* Gets a list of flowers that suit that occasion
* @param occasion occasion to filter flowers by
* @return an ArrayList of flowers to be printed by ui
*/
public static ArrayList<Flower> filterByOccasion(Flower.Occasion occasion) {
ArrayList<Flower> filteredFlowers = new ArrayList<>();
for (int i = 0; i < flowerDict.size(); i++) {
if (flowerDict.get(i).getOccasion().contains(occasion)){
filteredFlowers.add(flowerDict.get(i));
for (Flower flower : flowerDict) {
if (flower.getOccasion().contains(occasion)) {
filteredFlowers.add(flower);
}
}
return filteredFlowers;
}

/**
* Gets a list of flowers that contain the name search
* @param name name of Flowers to filter by
* @return an ArrayList of Flowers to be printed by ui
*/
public static ArrayList<Flower> filterByName(String name){
ArrayList<Flower> filteredFlowers = new ArrayList<>();
for (Flower flower : flowerDict) {
if (flower.getFlowerName().contains(name)) {
filteredFlowers.add(flower);
}
}
return filteredFlowers;
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,20 @@ public void printFilteredFlowers(ArrayList<Flower> flowers, String filter){

/**
* Prints name, colour, occasion and price information about a specific flower.
* @param targetFlower The name of the flower to retrieve information for.
* @return true if the flower information is found and printed successfully, otherwise false.
* @param targetFlower The name of the flower the user searched for.
* @param flowers The list of flowers that contain that name.
*/
public boolean printFlowerInfo(String targetFlower) {
for (int i = 0; i < FlowerDictionary.size(); i++) {
if (FlowerDictionary.get(i).getFlowerName().equalsIgnoreCase(targetFlower)) {
System.out.println(FlowerDictionary.get(i));
printBreakLine();
return true;
}
public void printFlowerInfo(ArrayList<Flower> flowers, String targetFlower) {
System.out.println("Here are all the info for flowers named " + targetFlower + ": ");
for (Flower flower : flowers){
System.out.println();
System.out.println(flower);
}
return false;

}

/**
* print all occasions available
* Prints all possible occasions the user can query
*/
public void printAllOccasions() {
System.out.println("Here are all the occasions associated with the available flowers: ");
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/florizz/objects/Flower.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Flower {
* Enumerates different colours a flower can have.
*/
public enum Colour {
WHITE, BLUE, RED, PINK
WHITE, BLUE, RED, PINK, DARK_CRIMSON
}

/**
Expand Down Expand Up @@ -91,6 +91,9 @@ public static Colour stringToColour(String colourString){
return Colour.valueOf(colourString.replaceAll(" ", "_").toUpperCase());
}

public static String colourToString(Colour colour){
return colour.toString().charAt(0) + colour.toString().replaceAll("_", " ").toLowerCase().substring(1);
}
/**
* Gets the name of the flower.
* @return The name of the flower.
Expand Down Expand Up @@ -137,7 +140,22 @@ public Double getPrice () {
*/
@Override
public String toString() {
return ("Name: " + name + "\n" + "Colours: " + colour.toString() + "\n" + "Occasions: " + occasions.toString()
+ "\n" + "Price: $" + String.format("%.2f", price) + "\n" + "Meanings: " + meanings.toString());
StringBuilder occasionsString = new StringBuilder("Occasions: ");
StringBuilder meaningsString = new StringBuilder("Meanings: ");
for (Occasion occasion : occasions){
occasionsString.append(occasionToString(occasion));
occasionsString.append(", ");
}


for (String meaning : meanings){
meaningsString.append(meaning);
meaningsString.append(", ");
}
return ("Name: " + name + "\n" +
"Colour: " + colourToString(colour) + "\n" +
occasionsString.substring(0, occasionsString.lastIndexOf(",")) + "\n" +
"Price: $" + String.format("%.2f", price) + "\n" +
meaningsString.substring(0, meaningsString.lastIndexOf(",")));
}
}

0 comments on commit ae9d470

Please sign in to comment.