From bb493cccff0789cef2f648590683bfe5538463b4 Mon Sep 17 00:00:00 2001 From: QX-CHEN Date: Tue, 29 Sep 2020 21:35:09 +0800 Subject: [PATCH] Revert "Add messages and move exception handling to main" This reverts commit bb750a5390fc14ed310dd91249b90c04d05ae8f7. --- src/main/java/Duke.java | 49 +++++++++---------- src/main/java/commands/Command.java | 4 +- src/main/java/commands/DeadlineCommand.java | 3 +- src/main/java/commands/EventCommand.java | 3 +- src/main/java/common/Message.java | 11 ++--- src/main/java/common/Utils.java | 7 +-- .../UnknownDateFormatException.java | 4 -- src/main/java/storage/Storage.java | 22 ++++++--- src/main/java/tasks/Deadline.java | 7 +-- src/main/java/tasks/Event.java | 8 +-- src/main/java/ui/Ui.java | 6 ++- 11 files changed, 56 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/exceptions/UnknownDateFormatException.java diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index e7496644cd..a229e761e3 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -2,12 +2,14 @@ import commands.Command; import commands.CommandResult; import data.TaskList; -import exceptions.*; +import exceptions.InvalidCommandException; +import exceptions.InvalidTaskNumberException; +import exceptions.TaskDoneException; +import exceptions.UnknownCommandException; import parser.Parser; import storage.Storage; import ui.Ui; -import java.io.IOException; import java.util.Scanner; import static common.Message.*; @@ -27,38 +29,31 @@ public class Duke { * Reads data from file and runs main program. */ public static void main(String[] args) { - try { - Storage.loadTasks(tasks); - run(); - } catch (NumberFormatException | InvalidTaskNumberException e) { - Ui.printMessageWithHorizontalLines(INVALID_TASK_NUMBER_MESSAGE); - } catch (UnknownCommandException e) { - Ui.printMessageWithHorizontalLines(UNKNOWN_COMMAND_MESSAGE); - } catch (InvalidCommandException e) { - Ui.printMessageWithHorizontalLines(INVALID_COMMAND_MESSAGE); - } catch (TaskDoneException e) { - Ui.printMessageWithHorizontalLines(TASK_ALREADY_DONE_MESSAGE); - } catch (UnknownDateFormatException e) { - Ui.printMessageWithHorizontalLines(UNKNOWN_DATE_FORMAT_MESSAGE); - } catch (IOException e) { - Ui.printMessageWithHorizontalLines(FILE_NOT_FOUND_MESSAGE); - } catch (Exception e) { - Ui.printMessageWithHorizontalLines(UNKNOWN_ERROR_MESSAGE); - } + Storage.loadTasks(tasks); + run(); } - private static void run() throws NumberFormatException, InvalidTaskNumberException, - UnknownCommandException, InvalidCommandException, TaskDoneException, UnknownDateFormatException { + private static void run() { Scanner scanner = new Scanner(System.in); Ui.greet(); boolean running = true; while (running) { - Command command = Parser.processInput(scanner.nextLine()); - CommandResult result = command.execute(tasks); - Ui.printCommandResult(result); - if (command instanceof ByeCommand) { - running = false; + try { + Command command = Parser.processInput(scanner.nextLine()); + CommandResult result = command.execute(tasks); + Ui.printCommandResult(result); + if (command instanceof ByeCommand) { + running = false; + } + } catch (NumberFormatException | InvalidTaskNumberException e) { + Ui.printMessageWithHorizontalLines(INVALID_TASK_NUMBER_MESSAGE); + } catch (UnknownCommandException e) { + Ui.printMessageWithHorizontalLines(UNKNOWN_COMMAND_MESSAGE); + } catch (InvalidCommandException e) { + Ui.printMessageWithHorizontalLines(INVALID_COMMAND_MESSAGE); + } catch (TaskDoneException e) { + Ui.printMessageWithHorizontalLines(TASK_ALREADY_DONE_MESSAGE); } } } diff --git a/src/main/java/commands/Command.java b/src/main/java/commands/Command.java index a35d6834a5..89772344bc 100644 --- a/src/main/java/commands/Command.java +++ b/src/main/java/commands/Command.java @@ -3,7 +3,6 @@ import data.TaskList; import exceptions.InvalidTaskNumberException; import exceptions.TaskDoneException; -import exceptions.UnknownDateFormatException; /** * Representation of generic command. @@ -12,6 +11,5 @@ public abstract class Command { /** * Execute the command. */ - public abstract CommandResult execute(TaskList tasks) - throws InvalidTaskNumberException, TaskDoneException, UnknownDateFormatException; + public abstract CommandResult execute(TaskList tasks) throws InvalidTaskNumberException, TaskDoneException; } diff --git a/src/main/java/commands/DeadlineCommand.java b/src/main/java/commands/DeadlineCommand.java index 7741f5e508..c419e85106 100644 --- a/src/main/java/commands/DeadlineCommand.java +++ b/src/main/java/commands/DeadlineCommand.java @@ -2,7 +2,6 @@ import data.TaskList; import exceptions.InvalidCommandException; -import exceptions.UnknownDateFormatException; import storage.Storage; import tasks.Deadline; @@ -46,7 +45,7 @@ public DeadlineCommand(String trimmedInput) throws InvalidCommandException { * * @param tasks runtime storage of tasks. */ - public CommandResult execute(TaskList tasks) throws UnknownDateFormatException { + public CommandResult execute(TaskList tasks) { Deadline deadline = new Deadline(description, date, time); tasks.add(deadline); Storage.updateFile(tasks); diff --git a/src/main/java/commands/EventCommand.java b/src/main/java/commands/EventCommand.java index 22711ea6b8..faf8f31b44 100644 --- a/src/main/java/commands/EventCommand.java +++ b/src/main/java/commands/EventCommand.java @@ -2,7 +2,6 @@ import data.TaskList; import exceptions.InvalidCommandException; -import exceptions.UnknownDateFormatException; import storage.Storage; import tasks.Event; @@ -48,7 +47,7 @@ public EventCommand(String trimmedInput) throws InvalidCommandException { * * @param tasks runtime storage of tasks. */ - public CommandResult execute(TaskList tasks) throws UnknownDateFormatException { + public CommandResult execute(TaskList tasks) { Event event = new Event(description, date, start, end); tasks.add(event); Storage.updateFile(tasks); diff --git a/src/main/java/common/Message.java b/src/main/java/common/Message.java index 4cccb0fd11..574cbfeaef 100644 --- a/src/main/java/common/Message.java +++ b/src/main/java/common/Message.java @@ -1,10 +1,10 @@ package common; public class Message { - public static final String HORIZONTAL_LINE = "\t______________________________" - + "______________________________"; - public static final String GREET_MESSAGE = "\t Hello! I'm Duke" + System.lineSeparator() - + "\t What can I do for you?"; + public static final String HORIZONTAL_LINE = "\t______________________________" + + "______________________________"; + public static final String GREET_MESSAGE = "\t Hello! I'm Duke" + System.lineSeparator() + + "\t What can I do for you?"; public static final String BYE_MESSAGE = "\t Bye. Hope to see you again soon!"; public static final String ADD_MESSAGE = "\t Got it. I've added: this task:"; public static final String LIST_MESSAGE = "\t Here are the tasks in your list:"; @@ -15,7 +15,4 @@ public class Message { public static final String TASK_ALREADY_DONE_MESSAGE = "\t Task already done!"; public static final String INVALID_COMMAND_MESSAGE = "\t Check command format!"; public static final String UNKNOWN_COMMAND_MESSAGE = "\t Check command word!"; - public static final String UNKNOWN_DATE_FORMAT_MESSAGE = "\t Unknown date format!"; - public static final String FILE_NOT_FOUND_MESSAGE = "\t File not found!"; - public static final String UNKNOWN_ERROR_MESSAGE = "\t Unknown error!"; } diff --git a/src/main/java/common/Utils.java b/src/main/java/common/Utils.java index 4d20ac39db..37000a6afb 100644 --- a/src/main/java/common/Utils.java +++ b/src/main/java/common/Utils.java @@ -1,6 +1,6 @@ package common; -import exceptions.UnknownDateFormatException; +import ui.Ui; import java.time.LocalDate; import java.time.LocalTime; @@ -8,7 +8,7 @@ public class Utils { - public static LocalDate dateStringToDate(String dateString) throws UnknownDateFormatException { + public static LocalDate dateStringToDate(String dateString) { if (dateString == null) { return null; } @@ -17,7 +17,8 @@ public static LocalDate dateStringToDate(String dateString) throws UnknownDateFo } else if (dateString.contains("-")) { return LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd")); } else { - throw new UnknownDateFormatException(); + Ui.printMessageWithHorizontalLines("Unknown date format!"); + return null; } } diff --git a/src/main/java/exceptions/UnknownDateFormatException.java b/src/main/java/exceptions/UnknownDateFormatException.java deleted file mode 100644 index e4e84e3ea4..0000000000 --- a/src/main/java/exceptions/UnknownDateFormatException.java +++ /dev/null @@ -1,4 +0,0 @@ -package exceptions; - -public class UnknownDateFormatException extends Exception { -} diff --git a/src/main/java/storage/Storage.java b/src/main/java/storage/Storage.java index 273f7834a5..e97cd270b8 100644 --- a/src/main/java/storage/Storage.java +++ b/src/main/java/storage/Storage.java @@ -1,10 +1,10 @@ package storage; import data.TaskList; -import exceptions.UnknownDateFormatException; import tasks.Deadline; import tasks.Event; import tasks.Todo; +import ui.Ui; import java.io.File; import java.io.FileNotFoundException; @@ -24,8 +24,12 @@ public class Storage { * * @param tasks latest TaskList object after modification. */ - public static void updateFile(TaskList tasks) throws IOException { - writeTasksToFile(tasks); + public static void updateFile(TaskList tasks) { + try { + writeTasksToFile(tasks); + } catch (IOException e) { + Ui.printMessageWithHorizontalLines("\t Check file path!"); + } } private static void writeTasksToFile(TaskList tasks) throws IOException { @@ -42,9 +46,13 @@ private static void writeTasksToFile(TaskList tasks) throws IOException { * * @param tasks empty TaskList. */ - public static void loadTasks(TaskList tasks) throws FileNotFoundException, UnknownDateFormatException { + public static void loadTasks(TaskList tasks) { if (!createDirectory()) { - readTasksFromFile(tasks); + try { + readTasksFromFile(tasks); + } catch (FileNotFoundException e) { + Ui.printMessageWithHorizontalLines("\t File not found!"); + } } } @@ -61,7 +69,7 @@ private static boolean createDirectory() { return directoryCreated; } - private static void readTasksFromFile(TaskList tasks) throws FileNotFoundException, UnknownDateFormatException { + private static void readTasksFromFile(TaskList tasks) throws FileNotFoundException { File file = new File(DIRECTORY_NAME + "/" + FILE_NAME); Scanner scanner = new Scanner(file); while (scanner.hasNext()) { @@ -69,7 +77,7 @@ private static void readTasksFromFile(TaskList tasks) throws FileNotFoundExcepti } } - private static void processLine(TaskList tasks, String line) throws UnknownDateFormatException { + private static void processLine(TaskList tasks, String line) { String[] parts = line.split("\\|"); boolean done = ("1".equals(parts[1])); switch (parts[0]) { diff --git a/src/main/java/tasks/Deadline.java b/src/main/java/tasks/Deadline.java index 77473667ac..f3b2cf8d59 100644 --- a/src/main/java/tasks/Deadline.java +++ b/src/main/java/tasks/Deadline.java @@ -1,7 +1,5 @@ package tasks; -import exceptions.UnknownDateFormatException; - import java.time.LocalDate; import java.time.LocalTime; @@ -22,7 +20,7 @@ public class Deadline extends Task { * @param dateString date of a deadline. * @param timeString time of a deadline. */ - public Deadline(String description, String dateString, String timeString) throws UnknownDateFormatException { + public Deadline(String description, String dateString, String timeString) { super(description); date = dateStringToDate(dateString); time = timeStringToTime(timeString); @@ -36,8 +34,7 @@ public Deadline(String description, String dateString, String timeString) throws * @param dateString date of a deadline. * @param timeString time of a deadline. */ - public Deadline(boolean done, String description, String dateString, String timeString) - throws UnknownDateFormatException { + public Deadline(boolean done, String description, String dateString, String timeString) { super(done, description); date = dateStringToDate(dateString); time = timeStringToTime(timeString); diff --git a/src/main/java/tasks/Event.java b/src/main/java/tasks/Event.java index 34ab9af239..e2a39afcfc 100644 --- a/src/main/java/tasks/Event.java +++ b/src/main/java/tasks/Event.java @@ -1,7 +1,5 @@ package tasks; -import exceptions.UnknownDateFormatException; - import java.time.LocalDate; import java.time.LocalTime; @@ -24,8 +22,7 @@ public class Event extends Task { * @param startString start time of a event. * @param endString end time of a event. */ - public Event(String description, String dateString, String startString, String endString) - throws UnknownDateFormatException { + public Event(String description, String dateString, String startString, String endString) { super(description); date = dateStringToDate(dateString); start = timeStringToTime(startString); @@ -41,8 +38,7 @@ public Event(String description, String dateString, String startString, String e * @param startString start time of a event. * @param endString end time of a event. */ - public Event(boolean done, String description, String dateString, String startString, String endString) - throws UnknownDateFormatException { + public Event(boolean done, String description, String dateString, String startString, String endString) { super(done, description); date = dateStringToDate(dateString); start = timeStringToTime(startString); diff --git a/src/main/java/ui/Ui.java b/src/main/java/ui/Ui.java index 133f662aa7..a34a43639d 100644 --- a/src/main/java/ui/Ui.java +++ b/src/main/java/ui/Ui.java @@ -30,10 +30,12 @@ public static void printMessageWithHorizontalLines(String message) { } /** - * Prints greeting message. + * Prints a greeting message. */ public static void greet() { - printMessageWithHorizontalLines(GREET_MESSAGE); + printHorizontalLine(); + System.out.println(GREET_MESSAGE); + printHorizontalLine(); } public static void printCommandResult(CommandResult commandResult) {