From e9aa54f7ca6aca096fc8b2560950810c43c3a7b2 Mon Sep 17 00:00:00 2001 From: Jeffinson Darmawan Date: Fri, 22 Mar 2024 23:54:18 +0800 Subject: [PATCH] Added logger --- src/main/java/florizz/core/Parser.java | 87 ++++++++++++++------------ 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/src/main/java/florizz/core/Parser.java b/src/main/java/florizz/core/Parser.java index 1d5e4169a7..9db80a6766 100644 --- a/src/main/java/florizz/core/Parser.java +++ b/src/main/java/florizz/core/Parser.java @@ -14,6 +14,7 @@ import florizz.objects.Bouquet; import java.util.logging.Logger; +import java.util.logging.Level; public class Parser { private static Logger logger = Logger.getLogger(Parser.class.getName()); @@ -26,48 +27,54 @@ public class Parser { private static final String ADD_FLOWER_REGEX = "(.+)/q(\\s*)(\\d+)(\\s*)/to(.+)"; private static final String REMOVE_FLOWER_REGEX = "(.+)/q(\\s*)(\\d+)(\\s*)/from(.+)"; - public static Command parse (String input) throws FlorizzException{ + public static Command parse (String input) throws FlorizzException { logger.entering("Parser", "parse"); - - String[] decodedInput = commandHandler(input); - //logger.log(Level.INFO, "commandHandler handled command successfully"); - Command command; - switch (decodedInput[0]){ - case ("mybouquets"): - command = new ListBouquetCommand(); - break; - case ("new"): - command = handleAddBouquet(input); - break; - case ("delete"): - command = handleDeleteBouquet(input); - break; - case ("bye"): - command = new ExitCommand(); - break; - case ("help"): - command = new HelpCommand(); - break; - case ("flower"): - command = handleFlowerCommand(input); - break; - case ("info"): - command = handleInfoCommand(input); - break; - case ("occasion"): - command = new ListOccasionCommand(); - break; - case ("add"): - command = handleAddFlower(decodedInput[1]); - break; - case ("remove"): - command = handleRemoveFlower(decodedInput[1]); - break; - default: - throw new FlorizzException("Unidentified input, type help to get a list of all commands!"); + Command command = null; + + try { + String[] decodedInput = commandHandler(input); + //logger.log(Level.INFO, "commandHandler handled command successfully"); + switch (decodedInput[0]) { + case ("mybouquets"): + command = new ListBouquetCommand(); + break; + case ("new"): + command = handleAddBouquet(input); + break; + case ("delete"): + command = handleDeleteBouquet(input); + break; + case ("bye"): + command = new ExitCommand(); + break; + case ("help"): + command = new HelpCommand(); + break; + case ("flower"): + command = handleFlowerCommand(input); + break; + case ("info"): + command = handleInfoCommand(input); + break; + case ("occasion"): + command = new ListOccasionCommand(); + break; + case ("add"): + command = handleAddFlower(decodedInput[1]); + break; + case ("remove"): + command = handleRemoveFlower(decodedInput[1]); + break; + default: + throw new FlorizzException("Unidentified input, type help to get a list of all commands!"); + } + logger.log(Level.INFO, "Command parsed successfully"); + } catch (FlorizzException ex) { + logger.log(Level.SEVERE, "Exception occurred while parsing command: " + ex.errorMessage, ex); + throw ex; + } finally { + logger.exiting("Parser", "parse"); } - - logger.exiting("Parser", "parse"); return command; }