-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change recommendCommand to no longer kick out user if bad input is given #154
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,10 +32,13 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce | |
throw new FlorizzException("No flowers available for this occasion and colour"); | ||
} | ||
|
||
// ask for bouquetName | ||
String bouquetName = askBouquetName(ui, bouquetList); | ||
|
||
// ask for size [FUTURE IMPLEMENTATION] | ||
|
||
// create bouquet with occasion and colour | ||
Bouquet recommendedBouquet = new Bouquet("Recommended Bouquet"); | ||
Bouquet recommendedBouquet = new Bouquet(bouquetName); | ||
|
||
// randomly add 3 flowers to bouquet | ||
addRandomFlowers(eligibleFlowers, recommendedBouquet); | ||
|
@@ -47,6 +50,27 @@ public boolean execute(ArrayList<Bouquet> bouquetList, Ui ui) throws FlorizzExce | |
return true; | ||
} | ||
|
||
/** | ||
* Gets the name the user wants to make for the recommended bouquet and makes sure the name does not already exist | ||
* @param ui Ui to take input and print messages | ||
* @param bouquetList List that contains all bouquets | ||
* @return The chosen valid bouquetName | ||
*/ | ||
public String askBouquetName(Ui ui, ArrayList<Bouquet> bouquetList) { | ||
boolean isValidName = false; | ||
String bouquetName = "placeHolder"; | ||
ui.printAskBouquetName(); | ||
while (!isValidName) { | ||
bouquetName = ui.getInput(); | ||
if (bouquetList.contains(new Bouquet(bouquetName))) { | ||
System.out.println("Sorry a bouquet with this name already exists, please enter another name"); | ||
} else { | ||
isValidName = true; | ||
} | ||
} | ||
return bouquetName; | ||
} | ||
|
||
/** | ||
* Adds random flowers to the bouquet | ||
* @param eligibleFlowers list of flowers to choose from | ||
|
@@ -67,13 +91,19 @@ private void addRandomFlowers(ArrayList<Flower> eligibleFlowers, Bouquet recomme | |
* Asks user for occasion | ||
* @return Occasion enum | ||
*/ | ||
private Flower.Occasion askOccasion(Ui ui) throws FlorizzException { | ||
private Flower.Occasion askOccasion(Ui ui) { | ||
logger.entering(RecommendCommand.class.getName(), "askOccasion"); | ||
String occasionInput = Parser.parseOccasion(ui.printAskOccasion()); | ||
|
||
// check if occasion is in our dictionary | ||
if (!Flower.isValidOccasion(occasionInput)) { | ||
throw new FlorizzException("This occasion does not exist. Type 'occasion' to get a list of occasions"); | ||
boolean isValidFormat = false; | ||
boolean isValidOccasion = false; | ||
String occasionInput = "placeholder"; | ||
while (!(isValidFormat && isValidOccasion)) { | ||
occasionInput = ui.printAskOccasion(); | ||
isValidFormat = Parser.parseOccasion(occasionInput); | ||
isValidOccasion = Flower.isValidOccasion(occasionInput); | ||
// check if occasion is in our dictionary | ||
if (!isValidOccasion) { | ||
System.out.println("This occasion does not exist."); | ||
} | ||
Comment on lines
+96
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes code more readable |
||
} | ||
|
||
logger.exiting(RecommendCommand.class.getName(), "askOccasion"); | ||
|
@@ -86,31 +116,40 @@ private Flower.Occasion askOccasion(Ui ui) throws FlorizzException { | |
* @param eligibleFlowers list of flowers to choose from | ||
* @return Colour enum | ||
*/ | ||
private Flower.Colour askColour(Ui ui, ArrayList<Flower> eligibleFlowers) throws FlorizzException { | ||
private Flower.Colour askColour(Ui ui, ArrayList<Flower> eligibleFlowers) { | ||
assert !eligibleFlowers.isEmpty() : "Eligible flowers should not be empty"; | ||
logger.entering(RecommendCommand.class.getName(), "askColour"); | ||
String colourInput = Parser.parseColour(ui.printAskColour(eligibleFlowers)); | ||
|
||
// check if colour is in our dictionary | ||
if (!Flower.isValidColour(colourInput)) { | ||
throw new FlorizzException("This colour does not exist. Type 'colour' to get a list of colours"); | ||
String colourInput = "placeHolder"; | ||
boolean isValidFormat = false; | ||
boolean isValidColour = false; | ||
while (!(isValidColour && isValidFormat)) { | ||
colourInput = ui.printAskColour(eligibleFlowers); | ||
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."); | ||
} | ||
} | ||
|
||
logger.exiting(RecommendCommand.class.getName(), "askColour"); | ||
return Flower.stringToColour(colourInput); | ||
} | ||
|
||
private void askSaveBouquet(Ui ui, ArrayList<Bouquet> bouquetList, | ||
Bouquet recommendedBouquet) throws FlorizzException { | ||
Bouquet recommendedBouquet) { | ||
logger.entering(RecommendCommand.class.getName(), "askSaveBouquet"); | ||
String saveInput = Parser.parseSaveBouquet(ui.printAskSaveBouquet(recommendedBouquet)); | ||
|
||
if (saveInput.equals("yes")) { | ||
if (bouquetList.contains(recommendedBouquet)) { | ||
// change name of bouquet | ||
recommendedBouquet.setName(recommendedBouquet.getBouquetName() + " (1)"); | ||
} | ||
String saveInput = "placeHolder"; | ||
boolean isValidFormat = false; | ||
boolean isValidInput = false; | ||
while (!(isValidInput && isValidFormat)) { | ||
saveInput = ui.printAskSaveBouquet(recommendedBouquet); | ||
isValidFormat = Parser.parseSaveBouquet(saveInput); | ||
isValidInput = (saveInput.equalsIgnoreCase("yes") || saveInput.equalsIgnoreCase("no")); | ||
} | ||
Comment on lines
+143
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if (saveInput.equalsIgnoreCase("yes")) { | ||
bouquetList.add(recommendedBouquet); | ||
ui.printBouquetAdded(recommendedBouquet); | ||
assert !bouquetList.isEmpty() : "Bouquet list should not be empty"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -277,55 +277,61 @@ private static InfoCommand handleInfoCommand(String flowerName) { | |
* @return The parsed occasion. | ||
* @throws FlorizzException If the input does not match the required format. | ||
*/ | ||
public static String parseOccasion(String argument) throws FlorizzException{ | ||
public static boolean parseOccasion(String argument) { | ||
if (argument == null) { | ||
throw new FlorizzException("No argument detected! " + | ||
System.out.println("No argument detected! " + | ||
"Please input an occasion"); | ||
return false; | ||
} | ||
|
||
if (!argument.matches(PARSE_OCCASION_REGEX)) { | ||
throw new FlorizzException("Incorrect format detected! " + | ||
System.out.println("Incorrect format detected! " + | ||
"Please input a single occasion"); | ||
return false; | ||
Comment on lines
-287
to
+290
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be good if all |
||
} | ||
|
||
return argument; | ||
return true; | ||
} | ||
|
||
/** | ||
* Parses the colour from the user input. | ||
* @param argument The user input to be parsed. | ||
* @return The parsed colour String | ||
*/ | ||
public static String parseColour(String argument) throws FlorizzException{ | ||
public static boolean parseColour(String argument) { | ||
if (argument == null) { | ||
throw new FlorizzException("No argument detected! " + | ||
System.out.println("No argument detected! " + | ||
"Please input a colour"); | ||
return false; | ||
} | ||
|
||
if (!argument.matches(PARSE_COLOUR_REGEX)) { | ||
throw new FlorizzException("Incorrect format detected! " + | ||
System.out.println("Incorrect format detected! " + | ||
"Please input a single colour"); | ||
return false; | ||
} | ||
|
||
return argument; | ||
return true; | ||
} | ||
|
||
/** | ||
* Parses the user input to save a bouquet. | ||
* @param argument The user input to be parsed. | ||
* @return The parsed save bouquet String | ||
*/ | ||
public static String parseSaveBouquet(String argument) throws FlorizzException{ | ||
public static boolean parseSaveBouquet(String argument) { | ||
if (argument == null) { | ||
throw new FlorizzException("No argument detected! " + | ||
System.out.println("No argument detected! " + | ||
"Please input a bouquet name to save"); | ||
return false; | ||
} | ||
|
||
if (!argument.matches(SAVE_BOUQUET_REGEX)) { | ||
throw new FlorizzException("Incorrect format detected! " + | ||
System.out.println("Incorrect format detected! " + | ||
"Please input a yes or a no"); | ||
return false; | ||
} | ||
|
||
return argument; | ||
return true; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should fix the naming issue, great job 👍