From 59523c6e03b900a4a4f9e595eac3f5984f08fdc8 Mon Sep 17 00:00:00 2001 From: EugeneChanJiajun Date: Mon, 1 Apr 2024 10:22:07 +0800 Subject: [PATCH 1/4] Edit save text file --- omni.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/omni.txt b/omni.txt index e01987dd1f..e69de29bb2 100644 --- a/omni.txt +++ b/omni.txt @@ -1,5 +0,0 @@ -accommodation / 0 / Malaysia Airbnb / 2019-12-17 / 3hours -food / 0 / Popia / 2020-12-01 / 1hour -landmark / 0 / Eiffel Tower / 2018-06-01 / 4hours -accommodation / 0 / four seasons hotel / 2024-12-12 / 2 days -food / 0 / kfc / 2024-12-12 / 1 hour From d0137335d46aee4b0376c43878ba17bba12a8b6d Mon Sep 17 00:00:00 2001 From: EugeneChanJiajun Date: Mon, 1 Apr 2024 10:30:21 +0800 Subject: [PATCH 2/4] Fix print outputs --- src/main/java/seedu/duke/Parser.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index d2903ce936..073ad67ce1 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -51,7 +51,7 @@ public static void activityCommand(String line, TravelActivityList list) throws String[] input = line.split(delimiter); //String tag = (input.length > 4 && !input[4].isBlank()) ? input[4].trim() : ""; if (input.length >= 4 && input[1].isBlank()) { - throw new OmniException("The description of accommodation cannot be empty!"); + throw new OmniException("The description cannot be empty!"); } else if (input.length >= 4 && input[2].isBlank()) { throw new OmniException("The date cannot be empty!"); } else if (input.length >= 4 && input[3].isBlank()) { @@ -97,10 +97,9 @@ public static void activityCommand(String line, TravelActivityList list) throws public static void addCommand(String line, TravelActivityList list) throws OmniException{ Ui.printLine(); String[] command = line.split("add | /date | /duration | /tag "); - //String tag = (command.length > 4 && !command[4].isBlank()) ? command[4].trim() : ""; logger.log(Level.INFO, command[0] + " // " + command[1]); if (command.length >= 4 && command[1].isBlank()) { - throw new OmniException("The description of add cannot be empty!"); + throw new OmniException("The description cannot be empty!"); } else if(command.length >= 4 && command[2].isBlank()){ throw new OmniException("The date cannot be empty!"); } else if (command.length >= 4 && command[3].isBlank()){ From 7287ea44aba3ccfa1b40b96208394d680a20158d Mon Sep 17 00:00:00 2001 From: EugeneChanJiajun Date: Mon, 1 Apr 2024 11:42:27 +0800 Subject: [PATCH 3/4] Add CheckParameters class to handle input exceptions --- mylog.txt | 0 omni.txt | 4 +- src/main/java/seedu/duke/CheckParameters.java | 73 ++++++++++++++++++ src/main/java/seedu/duke/Duke.java | 38 +++++----- src/main/java/seedu/duke/Parser.java | 74 ++++++------------- 5 files changed, 116 insertions(+), 73 deletions(-) create mode 100644 mylog.txt create mode 100644 src/main/java/seedu/duke/CheckParameters.java diff --git a/mylog.txt b/mylog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/omni.txt b/omni.txt index 1aa99aa1e9..150bf0a189 100644 --- a/omni.txt +++ b/omni.txt @@ -1,4 +1,6 @@ general / 1 / germany / 2019-12-12 / now / food / 0 / mala / 2023-12-12 / now / accommodation / 0 / hotel / 2023-12-12 / now / -landmark / 0 / statue of liberty / 2023-12-12 / now / \ No newline at end of file +landmark / 0 / statue of liberty / 2023-12-12 / now / +general / 0 / darone mum / 2023-12-12 / now / hello +general / 0 / paris / 2023-12-12 / 2 weeks / hello world diff --git a/src/main/java/seedu/duke/CheckParameters.java b/src/main/java/seedu/duke/CheckParameters.java new file mode 100644 index 0000000000..cbd1c99856 --- /dev/null +++ b/src/main/java/seedu/duke/CheckParameters.java @@ -0,0 +1,73 @@ +package seedu.duke; + +import java.io.IOException; +import java.time.DateTimeException; +import java.util.NoSuchElementException; + +import static seedu.duke.Parser.isNumeric; + +public class CheckParameters { + + /** + * Checks for all possible input errors that users may make and throws the corresponding exceptions + * + * @param input Input array that users placed into the chatbot + * @throws OmniException when any of the corresponding input format is wrong + */ + public static void addExceptions(String[] input) throws OmniException{ + if (input.length >= 4 && input[1].isBlank()) { + throw new OmniException("The description cannot be empty!"); + } else if (input.length >= 4 && input[2].isBlank()) { + throw new OmniException("The date cannot be empty!"); + } else if (input.length >= 4 && input[3].isBlank()) { + throw new OmniException("The duration cannot be empty!"); + } else if (input.length >= 5 && input[4].isBlank()) { + throw new OmniException("The tag cannot be empty!"); + } else if (input.length < 4 || input[3].contains("/tag")){ + throw new OmniException("Please check that your add command is in this format: add DESCRIPTION " + + "/date YYYY-MM-DD /duration DURATION" + + " or add DESCRIPTION /date YYYY-MM-DD /duration DURATION /tag TAG"); + } + } + + /** + * Checks for all possible input errors that users may make when updating and throws the corresponding exceptions + * + * @param command Command array that users placed into the chatbot + * @throws OmniException when any of the corresponding input format is wrong + */ + public static void updateExceptions(String[] command) throws OmniException { + if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1]))) { + throw new OmniException("The update index cannot be empty or non numerical!"); + } else if (command.length >= 4 && command[2].isBlank()) { + throw new OmniException("The date cannot be empty!"); + } else if (command.length >= 4 && command[3].isBlank()) { + throw new OmniException("The duration cannot be empty!"); + } else if(command.length >= 5 && command[4].isBlank()){ + throw new OmniException("The tag cannot be empty!"); + } else if (command.length >= 4 && !command[3].contains("/tag")) { + throw new OmniException("Please check that your update command is in this format: update INDEX " + + "/date YYYY-MM-DD /duration DURATION" + + " or update INDEX /date YYYY-MM-DD /duration DURATION /tag TAG"); + } + } + + /** + * Checks for all format errors in the user input and throes the correct exceptions + * + * @param exception Exception thrown + */ + public static void handleException(Exception exception) { + if (exception instanceof OmniException) { + Ui.printException((OmniException) exception); + } else if (exception instanceof NoSuchElementException) { + Ui.printNoSuchElementException((NoSuchElementException) exception); + } else if (exception instanceof NumberFormatException) { + Ui.printNumberTooLargeException((NumberFormatException) exception); + } else if (exception instanceof DateTimeException) { + Ui.printDateTimeExceptionError(); + } else if (exception instanceof IOException) { + Ui.printSavingError(); + } + } +} diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index c32c7a005b..c17fce2612 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -3,13 +3,18 @@ import java.time.DateTimeException; import java.util.NoSuchElementException; import java.util.Scanner; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.logging.*; public class Duke { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { Logger logger = Logger.getLogger("Main"); + FileHandler filehandler = new FileHandler("mylog.txt"); + SimpleFormatter formatter = new SimpleFormatter(); + filehandler.setFormatter(formatter); + logger.addHandler(filehandler); + LogManager.getLogManager().reset(); + logger.setLevel(java.util.logging.Level.OFF); Ui.printGreeting(); FileSave file = new FileSave("omni.txt"); TravelActivityList list = new TravelActivityList(); @@ -61,12 +66,21 @@ public static void main(String[] args) { file.saveActivityList(list); } catch (OmniException | NoSuchElementException | NumberFormatException | DateTimeException | IOException exception) { - handleException(exception); + CheckParameters.handleException(exception); } } } + + /** + * Handles the respective command inputs used by the user + * + * @param command Command array of the input without spaces + * @param line Line arry of the full input + * @param list List of travel activities + * @throws OmniException when any input format is wrong + */ private static void invokeCommand(String[] command, - String line, TravelActivityList list) throws OmniException, IOException { + String line, TravelActivityList list) throws OmniException { Ui.printLine(); switch (command[0].toLowerCase()) { case "delete": @@ -107,20 +121,6 @@ private static void invokeCommand(String[] command, } Ui.printLine(); } - - private static void handleException(Exception exception) { - if (exception instanceof OmniException) { - Ui.printException((OmniException) exception); - } else if (exception instanceof NoSuchElementException) { - Ui.printNoSuchElementException((NoSuchElementException) exception); - } else if (exception instanceof NumberFormatException) { - Ui.printNumberTooLargeException((NumberFormatException) exception); - } else if (exception instanceof DateTimeException) { - Ui.printDateTimeExceptionError(); - } else if (exception instanceof IOException) { - Ui.printSavingError(); - } - } } diff --git a/src/main/java/seedu/duke/Parser.java b/src/main/java/seedu/duke/Parser.java index 073ad67ce1..4c70c2ce65 100644 --- a/src/main/java/seedu/duke/Parser.java +++ b/src/main/java/seedu/duke/Parser.java @@ -49,18 +49,7 @@ public static void activityCommand(String line, TravelActivityList list) throws String[] command = line.split(" "); String delimiter = command[0] + "| /date | /duration | /tag "; String[] input = line.split(delimiter); - //String tag = (input.length > 4 && !input[4].isBlank()) ? input[4].trim() : ""; - if (input.length >= 4 && input[1].isBlank()) { - throw new OmniException("The description cannot be empty!"); - } else if (input.length >= 4 && input[2].isBlank()) { - throw new OmniException("The date cannot be empty!"); - } else if (input.length >= 4 && input[3].isBlank()) { - throw new OmniException("The duration cannot be empty!"); - } else if (input.length >= 5 && input[4].isBlank()) { - throw new OmniException("The tag cannot be empty!"); - } else if (input.length < 4 || input[3].contains("/tag")){ - throw new OmniException("Wrong format"); - } + CheckParameters.addExceptions(input); String description = input[1].trim(); LocalDate date = LocalDate.parse(input[2]); String duration = input[3].trim(); @@ -92,30 +81,21 @@ public static void activityCommand(String line, TravelActivityList list) throws * * @param line Line that the user inputs into the chatbot * @param list List of travel activities - * @throws OmniException if command.length < 2 + * @throws OmniException if any of the conditions are not met in the addExceptions() method */ public static void addCommand(String line, TravelActivityList list) throws OmniException{ Ui.printLine(); String[] command = line.split("add | /date | /duration | /tag "); logger.log(Level.INFO, command[0] + " // " + command[1]); - if (command.length >= 4 && command[1].isBlank()) { - throw new OmniException("The description cannot be empty!"); - } else if(command.length >= 4 && command[2].isBlank()){ - throw new OmniException("The date cannot be empty!"); - } else if (command.length >= 4 && command[3].isBlank()){ - throw new OmniException("The duration cannot be empty!"); - } else if (command.length >= 4 && !command[3].contains("/tag")){ - String description = command[1].trim(); - LocalDate date = LocalDate.parse(command[2]); - String duration = command[3].trim(); - String tag = (line.contains("/tag") && command.length == 5) ? command[4].trim() : ""; - TravelActivity newActivity = new TravelActivity(description, date, duration, tag); - list.addTravelActivity(newActivity); - System.out.println("I added a new travel activity"); - System.out.println(newActivity); - } else { - throw new OmniException("Wrong format"); - } + CheckParameters.addExceptions(command); + String description = command[1].trim(); + LocalDate date = LocalDate.parse(command[2]); + String duration = command[3].trim(); + String tag = (line.contains("/tag") && command.length == 5) ? command[4].trim() : ""; + TravelActivity newActivity = new TravelActivity(description, date, duration, tag); + list.addTravelActivity(newActivity); + System.out.println("I added a new travel activity"); + System.out.println(newActivity); Ui.printLine(); } @@ -130,7 +110,6 @@ public static void deleteCommand(String[] command, TravelActivityList list) thro if (command.length == 2 && isNumeric(command[1])){ int listNumber = Integer.parseInt(command[1]); list.removeTravelActivity(listNumber); - } else { throw new OmniException("Please specify which activity to delete"); } @@ -178,9 +157,12 @@ public static void uncheckCommand(String[] command, TravelActivityList list) thr */ public static void tagCommand(String line, TravelActivityList list) throws OmniException { String[] command = line.split(" "); - if (command.length == 3 && isNumeric(command[1])){ - int listNumber = Integer.parseInt(command[1]); - String tag = command[2]; + String index = command[1]; + String[] tagSplit = line.split(index); + String tag = tagSplit[1].trim(); + System.out.println(tag); + if (command.length >= 3 && isNumeric(command[1])){ + int listNumber = Integer.parseInt(index); list.tagActivity(listNumber, tag); } else if (command.length == 2) { throw new OmniException("Please specify a tag name"); @@ -213,23 +195,9 @@ public static void removeTagCommand(String[] command, TravelActivityList list) t */ public static void updateCommand(String line, TravelActivityList list) throws OmniException { String[] command = line.split("update | /date | /duration | /tag "); - if (command.length >= 4 && (command[1].isBlank() || !isNumeric(command[1]))) { - throw new OmniException("The update index cannot be empty or non numerical!"); - } else if (command.length >= 4 && command[2].isBlank()) { - throw new OmniException("The date cannot be empty!"); - } else if (command.length >= 4 && command[3].isBlank()) { - throw new OmniException("The duration cannot be empty!"); - } else if(command.length >= 5 && command[4].isBlank()){ - throw new OmniException("The tag cannot be empty!"); - } else if (command.length >= 4 && !command[3].contains("/tag")) { - String tag = (line.contains("/tag") && command.length == 5)? command[4].trim() : ""; - list.updateTravelActivity(Integer.parseInt(command[1]), LocalDate.parse(command[2]), command[3].trim(), - tag); - } else { - throw new OmniException("Please check that your update command is in this format: update INDEX " + - "/date YYYY-MM-DD /duration DURATION" - + " or update INDEX /date YYYY-MM-DD /duration DURATION /tag TAG"); - } + String tag = (line.contains("/tag") && command.length == 5)? command[4].trim() : ""; + list.updateTravelActivity(Integer.parseInt(command[1]), LocalDate.parse(command[2]), command[3].trim(), + tag); } /** @@ -253,7 +221,7 @@ public static void findTagCommand(String line, TravelActivityList list) throws O * Handles the case where the findtype command is given as input * * @param line User's input into Omnitravel - * @param list list List of travel activities + * @param list List of travel activities * @throws OmniException if command.length < 1 */ From 264d166b89549cfd4d9af168736e964d1b816f2b Mon Sep 17 00:00:00 2001 From: EugeneChanJiajun Date: Mon, 1 Apr 2024 11:47:48 +0800 Subject: [PATCH 4/4] Add CheckParameters class to handle input exceptions --- src/main/java/seedu/duke/Duke.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/seedu/duke/Duke.java b/src/main/java/seedu/duke/Duke.java index c17fce2612..d11e8628c5 100644 --- a/src/main/java/seedu/duke/Duke.java +++ b/src/main/java/seedu/duke/Duke.java @@ -3,7 +3,11 @@ import java.time.DateTimeException; import java.util.NoSuchElementException; import java.util.Scanner; -import java.util.logging.*; +import java.util.logging.FileHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; +import java.util.logging.LogManager; public class Duke {