From 1bc98d4d255700f732afe4322fe2d727527b21e3 Mon Sep 17 00:00:00 2001 From: Gavzzz Date: Sun, 4 Sep 2022 01:43:54 +0800 Subject: [PATCH] Level 7 implemented --- database/duke.txt | 1 + src/main/java/Constants.java | 4 + src/main/java/Deadline.java | 19 ++- src/main/java/Duke.java | 187 ++++++++++++++------- src/main/java/Event.java | 19 ++- src/main/java/IllegalCommandException.java | 6 + src/main/java/Task.java | 7 + src/main/java/Todo.java | 7 +- 8 files changed, 182 insertions(+), 68 deletions(-) create mode 100644 database/duke.txt create mode 100644 src/main/java/Constants.java create mode 100644 src/main/java/IllegalCommandException.java diff --git a/database/duke.txt b/database/duke.txt new file mode 100644 index 0000000000..879fb34498 --- /dev/null +++ b/database/duke.txt @@ -0,0 +1 @@ +deadline | return book | Sunday | true diff --git a/src/main/java/Constants.java b/src/main/java/Constants.java new file mode 100644 index 0000000000..6a13cf6141 --- /dev/null +++ b/src/main/java/Constants.java @@ -0,0 +1,4 @@ +public enum Constants { + TODO, DEADLINE, EVENT, LIST, MARK, UNMARK, DELETE, BYE + +} diff --git a/src/main/java/Deadline.java b/src/main/java/Deadline.java index e4e4b232c6..9a42515ae0 100644 --- a/src/main/java/Deadline.java +++ b/src/main/java/Deadline.java @@ -1,14 +1,23 @@ +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + public class Deadline extends Task { - protected String by; + protected String deadline; + + public Deadline(String description) { + super(description.substring(9, description.indexOf('/') - 1)); + this.deadline = description.substring(description.indexOf('/') + 3); + } - public Deadline(String description, String by) { - super(description); - this.by = by; + @Override + public String fileFormat() { + return String.format("deadline | %s | %s | %b", super.description, deadline, super.isDone); } @Override public String toString() { - return "[D]" + super.toString() + " (by: " + by + ")"; + return "[D]" + super.toString() + " (by:" + deadline + ")"; } } diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index b09b821704..2cfae3aa31 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,6 @@ +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; @@ -5,8 +8,20 @@ public class Duke { public Task[] tasks; + private static final ArrayList collection = new ArrayList<>(); - public static void main(String[] args) throws DukeException { + public static final Scanner sc = new Scanner(System.in); + + + public static boolean isNotValid(String str) { + String[] s = str.split(" "); + return (s.length < 1); + } + + + + + public static void main(String[] args) throws DukeException, IOException, IllegalCommandException { String logo = " ____ _ \n" + "| _ \\ _ _| | _____ \n" + "| | | | | | | |/ / _ \\\n" @@ -15,116 +30,172 @@ public static void main(String[] args) throws DukeException { System.out.println("Hello from\n" + logo); System.out.println("___________________________________"); System.out.println("Hello! I'm Duke\n What can I do for you?"); - System.out.println("___________________________________"); + System.out.println("____________________________________"); - Task[] tasks = new Task[100]; - int counter = 0; - ArrayList collection = new ArrayList<>(); - - Scanner sc = new Scanner(System.in); String input = sc.nextLine(); + File file = new File("database/duke.txt"); + if (file.createNewFile()) { + System.out.println("File has been created: " + file.getName()); + } else { + System.out.println("File already exists."); + Scanner reader = new Scanner(file); + while (reader.hasNext()) { + reader.useDelimiter(" \\| "); + Constants constant = Constants.valueOf(reader.next().toUpperCase()); + Task task; + switch (constant) { + case TODO: + String tdDescription = reader.next(); + Todo todo = new Todo(tdDescription); + collection.add(todo); + break; + case DEADLINE: + String dlDescription = reader.next(); + Deadline deadline = new Deadline(dlDescription); + collection.add(deadline); + break; + case EVENT: + String evDescription = reader.next(); + Event event = new Event(evDescription); + collection.add(event); + break; + + default: + throw new IllegalCommandException("Illegal command given"); + } + } + } + + while (!input.equals("bye")) { + if (input.equals("list")) { System.out.println("___________________________________"); - for (int i = 0; i < counter; i++) { + for (int i = 0; i < collection.size(); i++) { System.out.println( (i+1) + "." + collection.get(i).toString()); } System.out.println("___________________________________"); - input = sc.next(); + input = sc.nextLine(); } - else if (input.equals("mark")) { - int number = sc.nextInt(); + if (input.startsWith("mark")) { + int number = Integer.parseInt(input.substring(5)); collection.get(number-1).mark(); System.out.println("___________________________________"); System.out.println("Nice! I've marked this task done: " + "\n" + "[" + collection.get(number-1).getStatusIcon() + "] " + collection.get(number-1).description); System.out.println("___________________________________"); - input = sc.next(); + input = sc.nextLine(); } - else if (input.equals("unmark")) { - int number = sc.nextInt(); + if (input.equals("unmark")) { + int number = Integer.parseInt(input.substring(7)); collection.get(number-1).unmark(); System.out.println("___________________________________"); System.out.println("OK, I've marked this task as not done yet: " + "\n" + "[" + collection.get(number-1).getStatusIcon() + "] " + collection.get(number-1).description); System.out.println("___________________________________"); - input = sc.next(); + input = sc.nextLine(); } - else if (input.equals("deadline")) { - String what = sc.nextLine(); - if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty."); - String byWhen = sc.nextLine(); - collection.add(new Deadline(what, byWhen)); - System.out.println("___________________________________"); - System.out.println("Got it. I've added this task:" + "\n" - + " " + collection.get(counter).toString() + "\n" - + "Now you have " + (counter+1) + " tasks in the list."); - System.out.println("___________________________________"); - counter++; - input = sc.nextLine(); + if (input.startsWith("deadline")) { + try { + if (isNotValid(input)) + throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty."); + Deadline deadline = new Deadline(input); + collection.add(deadline); + System.out.println("___________________________________"); + System.out.println("Got it. I've added this task:" + "\n" + + " " + deadline.toString() + "\n" + + "Now you have " + collection.size() + " tasks in the list."); + System.out.println("___________________________________"); + } catch(DukeException e) { + System.out.println(e.getMessage()); + + } finally { + input = sc.nextLine(); + } } - else if (input.equals("todo")) { - String what = sc.nextLine(); - if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); - collection.add(new Todo(what)); - System.out.println("___________________________________"); - System.out.println("Got it. I've added this task:" + "\n" - + " " + collection.get(counter).toString() + "\n" - + "Now you have " + (counter+1) + " tasks in the list."); - System.out.println("___________________________________"); - counter++; - input = sc.nextLine(); + if (input.startsWith("todo")) { + try { + if (isNotValid(input)) + throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty."); + Todo todo = new Todo(input); + collection.add(todo); + System.out.println("___________________________________"); + System.out.println("Got it. I've added this task:" + "\n" + + " " + todo.toString() + "\n" + + "Now you have " + collection.size() + " tasks in the list."); + System.out.println("___________________________________"); + } catch(DukeException e) { + System.out.println(e.getMessage()); + + } finally { + input = sc.nextLine(); + } } - else if (input.equals("event")) { - String what = sc.nextLine(); - if (what.equals("")) throw new DukeException("☹ OOPS!!! The description of a event cannot be empty."); - String atWhen = sc.nextLine(); - collection.add(new Event(what, atWhen)); - System.out.println("___________________________________"); - System.out.println("Got it. I've added this task:" + "\n" - + " " + collection.get(counter).toString() + "\n" - + "Now you have " + (counter+1) + " tasks in the list."); - System.out.println("___________________________________"); - counter++; - input = sc.nextLine(); + if (input.startsWith("event")) { + try { + if (isNotValid((input))) + throw new DukeException("☹ OOPS!!! The description of a event cannot be empty."); + Event event = new Event(input); + collection.add(event); + System.out.println("___________________________________"); + System.out.println("Got it. I've added this task:" + "\n" + + " " + event.toString() + "\n" + + "Now you have " + collection.size() + " tasks in the list."); + System.out.println("___________________________________"); + } catch(DukeException e) { + System.out.println(e.getMessage()); + + } finally { + input = sc.nextLine(); + } } - else if (input.equals("delete")) { - int number = sc.nextInt(); + + if (input.startsWith("delete")) { + int number = Integer.parseInt(input.substring(7)); Task temp = collection.get(number-1); collection.remove(number-1); - counter--; System.out.println("___________________________________"); System.out.println("Noted. I've removed this task:" + "\n" + " " + temp.toString() + "\n" - + "Now you have " + counter + " tasks in the list."); + + "Now you have " + collection.size() + " tasks in the list."); System.out.println("___________________________________"); - input = sc.next(); + input = sc.nextLine(); } else { - System.out.println("___________________________________"); - throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-("); - + try { + throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(."); + } catch (DukeException e) { + System.out.println(e.getMessage()); + } finally { + input = sc.nextLine(); + } } + } + FileWriter fileWriter = new FileWriter(file); + for (Task task : collection) { + fileWriter.write(task.fileFormat() + System.lineSeparator()); } + fileWriter.close(); System.out.println("___________________________________"); System.out.println("Bye. Hope to see you again soon!"); System.out.println("___________________________________"); } + } diff --git a/src/main/java/Event.java b/src/main/java/Event.java index 2025084f1d..149a627e61 100644 --- a/src/main/java/Event.java +++ b/src/main/java/Event.java @@ -1,14 +1,25 @@ +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + public class Event extends Task{ protected String at; - public Event(String description, String at) { - super(description); - this.at = at; + public Event(String description) { + super(description.substring(6, description.indexOf('/') - 1)); + this.at = description.substring(description.indexOf('/') + 3); + } + + @Override + public String fileFormat() { + return String.format("event | %s | %s | %b", super.description, at, super.isDone); } @Override public String toString() { - return "[E]" + super.toString() + " (at: " + at + ")"; + return "[E]" + super.toString() + " (at:" + at + ")"; } } diff --git a/src/main/java/IllegalCommandException.java b/src/main/java/IllegalCommandException.java new file mode 100644 index 0000000000..fd2cf1cdf2 --- /dev/null +++ b/src/main/java/IllegalCommandException.java @@ -0,0 +1,6 @@ +public class IllegalCommandException extends Exception { + + public IllegalCommandException(String message) { + super(message); + } +} diff --git a/src/main/java/Task.java b/src/main/java/Task.java index aa3d4a230b..3f789e4fe8 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,3 +1,6 @@ +import java.io.FileWriter; +import java.io.IOException; + public class Task { protected String description; protected boolean isDone; @@ -19,6 +22,10 @@ public void unmark() { this.isDone = false; } + public String fileFormat() { + return String.format("todo | %s | %b", description, isDone); + } + @Override public String toString() { return "[" + this.getStatusIcon() + "] " + description; diff --git a/src/main/java/Todo.java b/src/main/java/Todo.java index c5f2851cf4..f87860735e 100644 --- a/src/main/java/Todo.java +++ b/src/main/java/Todo.java @@ -1,7 +1,12 @@ public class Todo extends Task { public Todo(String description) { - super(description); + super(description.substring(5)); + } + + @Override + public String fileFormat() { + return String.format("todo | %s | %b", super.description, super.isDone); } @Override