Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions src/main/java/florizz/command/RecommendCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Comment on lines +53 to +73
Copy link

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 👍

/**
* Adds random flowers to the bouquet
* @param eligibleFlowers list of flowers to choose from
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes code more readable

}

logger.exiting(RecommendCommand.class.getName(), "askOccasion");
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isValidInput might not be necessary since parseSaveBouquet already used regex, although that one only accepts lower case input. If you didn't change parseSaveBouquet, the method might still reject capitalised yes even with equalsIgnoreCase.


if (saveInput.equalsIgnoreCase("yes")) {
bouquetList.add(recommendedBouquet);
ui.printBouquetAdded(recommendedBouquet);
assert !bouquetList.isEmpty() : "Bouquet list should not be empty";
Expand Down
1 change: 1 addition & 0 deletions src/main/java/florizz/core/Florizz.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static void main(String[] args) {
while (isRunning) {
logger.log(Level.INFO, "Entered isRunning while loop in Florizz.java");
try {
System.out.println("What can I do for you?");
String input = ui.getInput();
Command command = Parser.parse(input.trim(), true);
isRunning = command.execute(tempBouquetList, ui);
Expand Down
30 changes: 18 additions & 12 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good if all system.out should be done by Ui only to follow single responsibility principle. I understand why you remove the florizz exception since it kinds of stop users from inputting more command.

}

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;
}
}
6 changes: 5 additions & 1 deletion src/main/java/florizz/core/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static void printBreakLine(){
* @return Returns the user input as one String.
*/
public String getInput(){
System.out.println("What can I do for you?");
return inputScanner.nextLine();
}

Expand Down Expand Up @@ -359,6 +358,11 @@ public String printAskOccasion() {
return inputScanner.nextLine();
}

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?");
}

/**
* ask user for colour input
* @param eligibleFlowers list of flowers that are eligible for the occasion
Expand Down
Binary file removed tP-jar/tp.jar
Binary file not shown.
Loading