Skip to content

Commit

Permalink
Revert "Add messages and move exception handling to main"
Browse files Browse the repository at this point in the history
This reverts commit bb750a5.
  • Loading branch information
QX-CHEN committed Sep 29, 2020
1 parent bb750a5 commit bb493cc
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 68 deletions.
49 changes: 22 additions & 27 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import data.TaskList;
import exceptions.InvalidTaskNumberException;
import exceptions.TaskDoneException;
import exceptions.UnknownDateFormatException;

/**
* Representation of generic command.
Expand All @@ -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;
}
3 changes: 1 addition & 2 deletions src/main/java/commands/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import data.TaskList;
import exceptions.InvalidCommandException;
import exceptions.UnknownDateFormatException;
import storage.Storage;
import tasks.Deadline;

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import data.TaskList;
import exceptions.InvalidCommandException;
import exceptions.UnknownDateFormatException;
import storage.Storage;
import tasks.Event;

Expand Down Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/common/Message.java
Original file line number Diff line number Diff line change
@@ -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:";
Expand All @@ -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!";
}
7 changes: 4 additions & 3 deletions src/main/java/common/Utils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package common;

import exceptions.UnknownDateFormatException;
import ui.Ui;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Utils {

public static LocalDate dateStringToDate(String dateString) throws UnknownDateFormatException {
public static LocalDate dateStringToDate(String dateString) {
if (dateString == null) {
return null;
}
Expand All @@ -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;
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/exceptions/UnknownDateFormatException.java

This file was deleted.

22 changes: 15 additions & 7 deletions src/main/java/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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!");
}
}
}

Expand All @@ -61,15 +69,15 @@ 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()) {
processLine(tasks, scanner.nextLine());
}
}

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]) {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tasks;

import exceptions.UnknownDateFormatException;

import java.time.LocalDate;
import java.time.LocalTime;

Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package tasks;

import exceptions.UnknownDateFormatException;

import java.time.LocalDate;
import java.time.LocalTime;

Expand All @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bb493cc

Please sign in to comment.