From 98f1864c50749cf9364f3ae3a4b86cf762e25d97 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 12 Feb 2020 20:28:22 +0800 Subject: [PATCH] Updated level 5 & 6 features. Refactored out todo, events and deadline --- database.txt | 1 + src/main/java/Duke.java | 184 +++--------------------------- src/main/java/DukeExceptions.java | 22 ++-- src/main/java/Parser.java | 6 +- src/main/java/Storage.java | 6 +- src/main/java/Task.java | 2 +- src/main/java/TaskList.java | 170 +++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 185 deletions(-) create mode 100644 database.txt create mode 100644 src/main/java/TaskList.java diff --git a/database.txt b/database.txt new file mode 100644 index 0000000000..a289037833 --- /dev/null +++ b/database.txt @@ -0,0 +1 @@ +[T],true,say hello, diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index f6feb79813..b34eef302e 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,182 +1,24 @@ import java.util.Scanner; -import java.util.ArrayList; public class Duke { - private ArrayList tasks; - private ToDo newToDo; - private Event newEvent; - private Deadline newDeadline; private UI userInterface; private Parser parser; - private DukeExceptions dukeExceptions; + private TaskList tasks; private Storage storage; - private final int startIndex = 0; - private final int lengthOfDeadline = 9; - private final int lengthOfEvent = 5; - private final int lengthOfDone = 5; - private final int lengthOfDelete = 7; public Duke() { - this.tasks = new ArrayList(); userInterface = new UI(); parser = new Parser(); - dukeExceptions = new DukeExceptions(); + tasks = new TaskList(); storage = new Storage(); } - private void printList() { - int position = 1; - for (Task text: tasks) { - System.out.println(" " + position + ". " + text); - position ++; - } - } - - private void list() { - tasks = storage.loadTasks(); - if (tasks.size() == 0) { - dukeExceptions.printListExceptions(); - } else { - System.out.println(" Here are the list of tasks:"); - printList(); - } - } - - private int getIndex(String input, int position) { - String removeTrailingSpaces = input.trim(); - String numericalIndex = removeTrailingSpaces.substring(position,input.length()).trim(); - int index = Integer.valueOf(numericalIndex) - 1; - return index; - } - - private void markAsDone(int index) { - Task completedTask = tasks.get(index); - boolean isCompleted = completedTask.getStatus(); - if (isCompleted) { - dukeExceptions.printDoneExceptions(); - } else { - completedTask.markAsDone(); - tasks.set(index, completedTask); - System.out.println(" Duke has marked the following tasks as done:"); - System.out.println(" " + completedTask); - storage.save(tasks); - } - } - - private void getDoneExceptions(String input) { - try { - int index = getIndex(input, lengthOfDone); - markAsDone(index); - } catch(IndexOutOfBoundsException exception) { - dukeExceptions.printDoneIOBExceptions(input); - } catch (NumberFormatException exception) { - dukeExceptions.printDoneNFExceptions(); - } - } - - private void addToDo(String input) { - String removeTrailingSpaces = input.trim(); - String description = removeTrailingSpaces.substring(5, input.length()); - newToDo = new ToDo(description); - tasks.add(newToDo); - System.out.println(" Got it! I have added the following tasks: "); - System.out.println( newToDo); - System.out.println("There are currently " + tasks.size() + " task(s) queued"); - storage.save(tasks); - } - - private void getToDoExceptions(String input) { - try { - addToDo(input); - } catch (StringIndexOutOfBoundsException exception) { - dukeExceptions.toDoExceptions(input); - } - } - - private void addEvent(String input) { - String description = input.substring(lengthOfEvent).trim(); - int index = description.indexOf("/"); - String details = description.substring(startIndex, index).trim(); - String specifier = description.substring(index + 1, index + 3); - String time = description.substring(index + 3, description.length()).trim(); - boolean isAt = specifier.toLowerCase().equals("at"); - if (details.isEmpty() || time.isEmpty() || !isAt) { - String errorMessage = "Missing Event Description / Event Time / Missing parameters"; - dukeExceptions.printEventExceptions(errorMessage, input); - } else { - newEvent = new Event(details, time); - tasks.add(newEvent); - System.out.println(" New task has been added:"); - System.out.println(" " + newEvent); - System.out.println(" There are currently " + tasks.size() + " task(s) being queued"); - storage.save(tasks); - } - } - - private void getEventExceptions(String input) { - try { - addEvent(input); - } catch (IndexOutOfBoundsException exception) { - String errorMessage = "Missing Event Description / Event Time & Missing parameters"; - dukeExceptions.printEventExceptions(errorMessage, input); - } - } - - private void addDeadline(String input) { - String description = input.substring(lengthOfDeadline, input.length()).trim(); - int index = description.indexOf("/"); - String details = description.substring(startIndex, index); - String day = description.substring(index + 3, description.length()).trim(); - String specifier = description.substring(index + 1, index + 3); - boolean isBy = specifier.toLowerCase().equals("by"); - if (details.isEmpty() || day.isEmpty() || !isBy) { - String errorMessage = "Missing Event Description / Event Time / Missing parameters"; - dukeExceptions.printDeadlineExceptions(errorMessage, input); - } else { - newDeadline = new Deadline(details, day); - tasks.add(newDeadline); - System.out.println(" Got it!I've added this task:"); - System.out.println(" " + newDeadline); - System.out.println(" There are currently " + tasks.size() + " being queued"); - storage.save(tasks); - } - } - - private void getDeadlineExceptions(String input) { - try { - addDeadline(input); - } catch (IndexOutOfBoundsException exception) { - String errorMessage = "Missing Event Description / Event Time & Missing parameters"; - dukeExceptions.printDeadlineExceptions(errorMessage, input); - } - } - - private void deleteItems(int index) { - Task deletedTask = tasks.get(index); - int length = tasks.size() - 1; - tasks.remove(deletedTask); - System.out.println(" Got it! I've removed this task:"); - System.out.println(" " + deletedTask); - System.out.println(" There are currently " + length + " task(s) being queued"); - storage.save(tasks); - } - - private void getDeleteExceptions(String input) { - try { - int index = getIndex(input, lengthOfDelete); - deleteItems(index); - } catch (NumberFormatException exceptions) { - dukeExceptions.printNFEDeleteExceptions(input); - } catch (IndexOutOfBoundsException exception) { - dukeExceptions.printIOBDeleteExceptions(input); - } - } - private void run() { Scanner sc = new Scanner(System.in); userInterface.printGreetingMessage(); - storage.restoreArray(sc, tasks); + tasks.list(); + userInterface.printLine(); while (true) { String input = sc.nextLine().trim(); parser.updateInput(input); @@ -186,17 +28,19 @@ private void run() { userInterface.printLine(); break; } else if (parser.isList()) { - list(); + tasks.list(); } else if (parser.isDone()) { - getDoneExceptions (input); + tasks.getDoneExceptions(input); } else if (parser.isToDo()) { - getToDoExceptions (input); + tasks.getToDoExceptions (input); } else if (parser.isEvent()) { - getEventExceptions(input); + tasks.getEventExceptions(input); } else if (parser.isDeadline()) { - getDeadlineExceptions(input); - } else if (parser.isDelete()) { - getDeleteExceptions(input); + tasks.getDeadlineExceptions(input); + } else if(parser.isDelete()) { + tasks.getDeleteExceptions(input); + } else if (parser.isSave()) { + System.out.println("SAVING..."); } else { System.out.println(" Duke does not understand your command! Can you repeat again?"); } @@ -204,7 +48,7 @@ private void run() { } sc.close(); } - + public static void main(String[] args) { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" diff --git a/src/main/java/DukeExceptions.java b/src/main/java/DukeExceptions.java index 9919e30d22..400083f2f9 100644 --- a/src/main/java/DukeExceptions.java +++ b/src/main/java/DukeExceptions.java @@ -1,5 +1,6 @@ public class DukeExceptions { protected String input; + public DukeExceptions() { this.input = null; } @@ -12,25 +13,25 @@ public void printDoneExceptions() { System.out.println(" [Warning: The task(s) has already been marked as done]"); } - public void printDoneIOBExceptions(String input) { + public void printIOBDoneExceptions(String input) { System.out.println(" Command Entered: " + input); - System.out.println(" [Error: Index is missing / out of bounds]"); - System.out.println(" done : check the task as completed"); - System.out.println(" Parameters: done [index] "); + System.out.println(" [Error: Specifier entered is out of range]"); + System.out.println(" done: marks task to be completed"); + System.out.println(" Parameters: done [TASK NUMBER]"); System.out.println(" Example: done 1"); } - public void printDoneNFExceptions() { + public void printNFEDoneExceptions(String input) { System.out.println(" Command Entered: " + input); - System.out.println(" [Error: Index is missing / out of bounds]"); - System.out.println(" done : check the task as completed"); - System.out.println(" Parameters: done [index] "); + System.out.println(" [Error: Specifier entered is not a numerical value]"); + System.out.println(" done: marks task to be completed"); + System.out.println(" Parameters: done [Specifier]"); System.out.println(" Example: done 1"); } - public void toDoExceptions(String input) { + public void printToDoExceptions(String input) { System.out.println(" Command Entered: " + input); - System.out.println(" [Error: Event Description is missing]"); + System.out.println(" [Error: Missing specifier(s)]"); System.out.println(" todo: Adds todo items"); System.out.println(" Parameters: todo [description]"); System.out.println(" Example: todo read book"); @@ -69,5 +70,4 @@ public void printNFEDeleteExceptions(String input) { System.out.println(" Parameters: delete [TASK NUMBER]"); System.out.println(" Example: delete 1"); } - } \ No newline at end of file diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 166f8e9c6a..3bc50832ae 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -54,5 +54,9 @@ public boolean isDelete() { String firstWord = getFirstWord(); return firstWord.equals("delete"); } - + + public boolean isSave() { + String firstWord = getFirstWord(); + return firstWord.equals("save"); + } } \ No newline at end of file diff --git a/src/main/java/Storage.java b/src/main/java/Storage.java index 2f09e164f9..eeaf89d341 100644 --- a/src/main/java/Storage.java +++ b/src/main/java/Storage.java @@ -3,7 +3,7 @@ import java.io.IOException; import java.util.ArrayList; import java.io.FileNotFoundException; -import java.util.Scanner; +import java.util.Scanner; public class Storage{ protected File file; @@ -11,7 +11,7 @@ public class Storage{ protected ArrayList tasks; public Storage() { - file = new File("store.txt"); + file = new File("database.txt"); filePath = file.getAbsolutePath(); } @@ -61,6 +61,8 @@ public ArrayList loadTasks() { fileReader.close(); } catch (FileNotFoundException exception) { System.out.println("file not found"); + } catch (Exception exception) { + System.out.println("File not found"); } return tasksList; } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index c71aff135a..665794b918 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -11,7 +11,7 @@ public Task (String description, boolean isDone) { this.description = description; this.isDone = isDone; } - + public boolean getStatus() { return isDone; } diff --git a/src/main/java/TaskList.java b/src/main/java/TaskList.java new file mode 100644 index 0000000000..7265f7d908 --- /dev/null +++ b/src/main/java/TaskList.java @@ -0,0 +1,170 @@ +import java.util.ArrayList; +public class TaskList { + protected Storage storage; + protected Event newEvent; + protected ToDo newToDo; + protected Deadline newDeadline; + protected ArrayList tasks; + protected DukeExceptions dukeExceptions; + + public TaskList() { + storage = new Storage(); + dukeExceptions = new DukeExceptions(); + this.tasks = new ArrayList(); + } + + public ArrayList getArray() { + ArrayList returnedArray = new ArrayList(); + for (Task task : tasks) { + returnedArray.add(task); + } + return returnedArray; + } + + public int getIndex (String input, int position) { + String removeTrailingSpaces = input.trim(); + String numericalIndex = removeTrailingSpaces.substring(position,input.length()).trim(); + int index = Integer.valueOf(numericalIndex) - 1; + return index; + } + + public void markAsDone(int index) { + Task completedTask = tasks.get(index); + boolean isCompleted = completedTask.getStatus(); + if (isCompleted) { + dukeExceptions.printDoneExceptions(); + } else { + completedTask.markAsDone(); + tasks.set(index, completedTask); + System.out.println(" Duke has marked the following tasks as done:"); + System.out.println(" " + completedTask); + storage.save(tasks); + } + } + + public void addToDo(String input) { + String removeTrailingSpaces = input.trim(); + String description = removeTrailingSpaces.substring(5, input.length()); + newToDo = new ToDo(description); + tasks.add(newToDo); + System.out.println(" Got it! I have added the following tasks: "); + System.out.println( newToDo); + System.out.println("There are currently " + tasks.size() + " task(s) queued"); + storage.save(tasks); + } + + public void addEvent(String input) { + String description = input.substring(5, input.length()).trim(); + int index = description.indexOf("/"); + String details = description.substring(0, index).trim(); + String specifier = description.substring(index + 1, index + 3); + String time = description.substring(index + 3, description.length()).trim(); + boolean isAt = specifier.toLowerCase().equals("at"); + if (details.isEmpty() || time.isEmpty() || !isAt) { + String errorMessage = "Missing placeholder"; + dukeExceptions.printEventExceptions(errorMessage, input); + } else { + newEvent = new Event(details, time); + tasks.add(newEvent); + System.out.println(" New task has been added:"); + System.out.println(" " + newEvent); + System.out.println(" There are currently " + tasks.size() + " task(s) being queued"); + storage.save(tasks); + } + } + + public void addDeadline(String input) { + String description = input.substring(9, input.length()).trim(); + int index = description.indexOf("/"); + String details = description.substring(0, index); + String day = description.substring(index + 3, description.length()).trim(); + String specifier = description.substring(index + 1, index + 3); + boolean isBy = specifier.toLowerCase().equals("by"); + if (details.isEmpty() || day.isEmpty() || !isBy) { + String errorMessage = "Missing placeholder"; + dukeExceptions.printDeadlineExceptions(errorMessage, input); + } else { + newDeadline = new Deadline(details, day); + tasks.add(newDeadline); + System.out.println(" Got it!I've added this task:"); + System.out.println(" " + newDeadline); + System.out.println(" There are currently " + tasks.size() + " task(s) being queued"); + storage.save(tasks); + } + } + + public void deleteItem(int index) { + Task deletedTask = tasks.get(index); + int length = tasks.size() - 1; + tasks.remove(index); + System.out.println(" Got it!I've removed this task: "); + System.out.println(" " + deletedTask); + System.out.println(" There are currently " + length + " task(s) being queued"); + storage.save(tasks); + } + + public void printTasks() { + int position = 1; + for (Task text: tasks) { + System.out.println(" " + position + ". " + text); + position ++; + } + } + + public void list() { + tasks = storage.loadTasks(); + if (tasks.size() == 0) { + dukeExceptions.printListExceptions(); + } else { + System.out.println(" Here are the list of tasks:"); + printTasks(); + } + } + public void getDoneExceptions(String input) { + try { + int index = getIndex(input,5); + markAsDone(index); + } catch(IndexOutOfBoundsException exception) { + dukeExceptions.printIOBDoneExceptions(input); + } catch (NumberFormatException exception) { + dukeExceptions.printNFEDoneExceptions(input); + } + } + + public void getToDoExceptions(String input) { + try { + addToDo(input); + } catch (StringIndexOutOfBoundsException exception) { + dukeExceptions.printToDoExceptions(input); + } + } + + public void getEventExceptions(String input) { + try { + addEvent(input); + } catch (IndexOutOfBoundsException exception) { + String errorMessage = "Missing specifier(s)"; + dukeExceptions.printEventExceptions(errorMessage, input); + } + } + + public void getDeadlineExceptions(String input) { + try { + addDeadline(input); + } catch (IndexOutOfBoundsException exception) { + String errorMessage = "Missing specifier(s)"; + dukeExceptions.printDeadlineExceptions(errorMessage, input); + } + } + + public void getDeleteExceptions(String input) { + try { + int index = getIndex(input, 6); + deleteItem(index); + } catch (IndexOutOfBoundsException exception) { + dukeExceptions.printIOBDeleteExceptions(input); + } catch (NumberFormatException exception) { + dukeExceptions.printNFEDeleteExceptions(input); + } + } +} \ No newline at end of file