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

PE-D bug fixes #166

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
3 changes: 2 additions & 1 deletion src/main/java/florizz/command/RecommendCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ 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) {
private void addRandomFlowers(ArrayList<Flower> eligibleFlowers, Bouquet recommendedBouquet)
throws FlorizzException {
logger.entering(RecommendCommand.class.getName(), "addRandomFlowers");
// [TEMPORARY CODE]
// generate random combination of flowers from eligible flowers totaling to 5
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/florizz/core/Florizz.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static void main(String[] args) {
boolean isRunning = true;
Ui ui = new Ui();
ui.printIntroMessage();
assert tempBouquetList !=null : "tempBouquetList doesn't exist";

// Set up logger

Expand Down Expand Up @@ -61,7 +60,7 @@ public static void main(String[] args) {
Command command = Parser.parse(input.trim(), true);
isRunning = command.execute(tempBouquetList, ui);
storage.trySaveAllBouquets(tempBouquetList);
} catch(FlorizzException error){
} catch (FlorizzException error) {
ui.printError(error);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/florizz/core/FlorizzException.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package florizz.core;

public class FlorizzException extends Exception{
public class FlorizzException extends Exception {
public String errorMessage;

public FlorizzException(String errorMessage){
this.errorMessage = errorMessage;
}

}
37 changes: 22 additions & 15 deletions src/main/java/florizz/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public class Parser {
private static final String REMOVE_FLOWER_PREFIX = "/from";

// regex
private static final String ADD_FLOWER_REGEX = "(.+)/q(\\s*)(\\d+)(\\s*)/to(.+)";
private static final String ADD_FLOWER_REGEX = "(.+)/q(\\s*)(-?\\d+)(\\s*)/to(.+)";

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 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,20 +132,20 @@ public static String[] commandHandler(String input) throws FlorizzException {
case ("add"):
String[] arguments = new String[2];
String trimmedArgument = processedInput.substring(firstWhitespace).trim();
int secondWhitespace = trimmedArgument.indexOf(" ");
if (secondWhitespace < 0 && outputs[0].equals("remove")) {
int firstSlash = trimmedArgument.indexOf("/");
if (firstSlash < 0 && outputs[0].equals("remove")) {
throw new FlorizzException("Incorrect usage of remove." +
" Correct format: remove <flowerName> " +
"/c <flowerColour> (optional) " +
"/q <quantity> /from <bouquetName>");
} else if (secondWhitespace < 0) { // add
} else if (firstSlash < 0) { // add
throw new FlorizzException("Incorrect usage of add." +
" Correct format: add <flowerName> " +
"/c <flowerColour> (optional) " +
"/q <quantity> /to <bouquetName>");
}
arguments[0] = FuzzyLogic.detectItem(trimmedArgument.substring(0,secondWhitespace).trim());
arguments[1] = trimmedArgument.substring(secondWhitespace).trim();
arguments[0] = FuzzyLogic.detectItem(trimmedArgument.substring(0,firstSlash).trim());
arguments[1] = trimmedArgument.substring(firstSlash).trim();
outputs[1] = arguments[0] + " " + arguments[1];
break;
default:
Expand Down Expand Up @@ -243,11 +244,14 @@ private static AddFlowerCommand handleAddFlower(String argument, boolean enableU
int quantity;
try {
quantity = Integer.parseInt(quantityString);
}catch(NumberFormatException error){
} catch (NumberFormatException error) {
throw new FlorizzException("Invalid number inputted, please enter a sensible number next time!");
}
if (quantity < 0) {
throw new FlorizzException("Flowers are physical objects which cannot be negative!");
}
String bouquetName = removePrefix(argument.substring(prefixIndex), ADD_FLOWER_PREFIX).trim();
if (includeColour){
if (includeColour) {
int colourIndex = argument.indexOf(COLOUR);
try{
flowerName = argument.substring(0,colourIndex).trim();
Expand Down Expand Up @@ -293,9 +297,12 @@ private static RemoveFlowerCommand handleRemoveFlower(String argument) throws Fl
int quantity;
try {
quantity = Integer.parseInt(quantityString);
}catch(NumberFormatException error){
} catch(NumberFormatException error) {
throw new FlorizzException("Invalid number inputted, please enter a sensible number next time!");
}
if (quantity < 0) {
throw new FlorizzException("Flowers are physical objects which cannot be negative!");
}
String bouquetName = removePrefix(argument.substring(prefixIndex), REMOVE_FLOWER_PREFIX).trim();
if (includeColour){
int colourIndex = argument.indexOf(COLOUR);
Expand Down Expand Up @@ -329,14 +336,14 @@ private static InfoCommand handleInfoCommand(String flowerName) throws FlorizzEx
* @param argument The user input to be parsed.
* @return The parsed occasion.
*/
public static boolean parseOccasion(String argument) {
public static boolean parseOccasion(String argument) throws FlorizzException {
if (argument == null) {
System.out.println("No argument detected! " +
"Please input an occasion");
return false;
}

if (!argument.matches(PARSE_OCCASION_REGEX)) {
String detectedOccasion = FuzzyLogic.detectItem(argument);
if (detectedOccasion.isEmpty()) {
System.out.println("Incorrect format detected! " +
"Please input a single occasion");
return false;
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/florizz/objects/Bouquet.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ public boolean doesFlowerExist(Flower flowerName) {
* @param flowerName
* @param quantity
*/
public void addFlower(Flower flowerName, Integer quantity) {
public void addFlower(Flower flowerName, Integer quantity) throws FlorizzException {
if (doesFlowerExist(flowerName)) {
Integer currentQuantity = getFlowerQuantity(flowerName);
Integer newQuantity = currentQuantity + quantity;
int newQuantity = currentQuantity + quantity;
if ((quantity > 0 && currentQuantity > Integer.MAX_VALUE - quantity) || newQuantity < 0) {
throw new FlorizzException("You have added too much flowers!");
}
Comment on lines +73 to +76
Copy link

Choose a reason for hiding this comment

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

Great catching of large value exceptions!

flowerHashMap.replace(flowerName, newQuantity);
} else {
flowerHashMap.put(flowerName,quantity);
Expand Down
Loading