|
1 | | -import java.util.Scanner; |
2 | | -import java.util.ArrayList; |
| 1 | +import Duke.*; |
| 2 | +import DukeException.DukeException; |
| 3 | +import Parser.Parser; |
| 4 | +import Storage.Storage; |
| 5 | +import Ui.Ui; |
| 6 | + |
3 | 7 |
|
4 | 8 | public class Duke { |
| 9 | + public static final String LOG_PATH = "./duke.txt"; |
| 10 | + private Storage storage; |
| 11 | + private TaskList tasks; |
| 12 | + private Ui ui; |
5 | 13 |
|
6 | | - /** |
7 | | - * main method to run the program |
8 | | - * |
9 | | - * @param args command line arguments taken in |
10 | | - */ |
11 | | - public static void main(String[] args) { |
12 | | - Scanner sc = new Scanner(System.in); |
13 | | - System.out.println("Hello! I'm Duke\n"+ "What can I do for you?"); |
14 | | - ArrayList<Task> items = new ArrayList<>(); |
| 14 | + public Duke(String filepath) { |
| 15 | + this.storage = new Storage(filepath); |
| 16 | + this.tasks = new TaskList(this.storage); |
| 17 | + this.ui = new Ui(); |
| 18 | + } |
15 | 19 |
|
16 | | - while (true) { |
17 | | - String input = sc.nextLine(); |
18 | | - try { |
19 | | - errorHandling(input); |
20 | | - if (input.equals("bye")) { |
21 | | - bye(); |
22 | | - break; |
23 | | - } else if (input.contains("done")) { |
24 | | - System.out.println("Nice! I've marked this task as done: "); |
| 20 | + public static void main(String[] args) throws DukeException { |
| 21 | + try { |
| 22 | + new Duke(LOG_PATH).run(); |
| 23 | + } catch (DukeException e) { |
| 24 | + throw new DukeException("unable to load file, probably wrong format."); |
| 25 | + } |
| 26 | + } |
25 | 27 |
|
26 | | - int taskNum = Integer.parseInt(input.substring(5)); |
27 | | - String name = items.get(taskNum - 1).getDescription(); |
| 28 | + private void process(Parser p) { |
28 | 29 |
|
29 | | - Task type = items.get(taskNum - 1); |
30 | | - items.remove(taskNum - 1); |
31 | | - if (type instanceof Todo) { |
32 | | - Todo markDone = new Todo(name, true); |
33 | | - items.add(taskNum - 1, markDone); |
34 | | - System.out.println(markDone); |
35 | | - } else if (type instanceof Deadline) { |
36 | | - Deadline markDone = new Deadline(name, true); |
37 | | - items.add(taskNum - 1, markDone); |
38 | | - System.out.println(markDone); |
39 | | - } else { |
40 | | - Event markDone = new Event(name, true); |
41 | | - items.add(taskNum - 1, markDone); |
42 | | - System.out.println(markDone); |
43 | | - } |
| 30 | + switch (p.getCommand()) { |
| 31 | + case "delete": |
| 32 | + this.tasks.remove(p.getTaskNum()); |
| 33 | + ui.printTaskRemovedMessage((p.getTaskNum())); |
| 34 | + break; |
| 35 | + case "done": |
| 36 | + Task done = this.tasks.get(p.getTaskNum()); |
| 37 | + done.markDone(); |
| 38 | + this.tasks.set(p.getTaskNum(), done); |
| 39 | + ui.printDoneMessage((done)); |
| 40 | + break; |
| 41 | + case "add": |
| 42 | + this.tasks.add(p.getTask()); |
| 43 | + ui.printTaskAddedMessage(p.getTask(), this.tasks.getSize()); |
| 44 | + break; |
| 45 | + case "set": |
| 46 | + this.tasks.set(p.getTaskNum(), p.getTask()); |
| 47 | + break; |
| 48 | + case "list": |
| 49 | + ui.printList(this.tasks); |
| 50 | + break; |
| 51 | + default: |
| 52 | + } |
| 53 | + } |
44 | 54 |
|
45 | | - } else if (input.equals("list")) { |
46 | | - int n = 1; |
47 | | - System.out.println("Here are the tasks in your list:"); |
48 | | - for (Task item : items) { |
49 | | - System.out.println(n + ". " + item); |
50 | | - n++; |
51 | | - } |
| 55 | + public void run() throws DukeException { |
| 56 | + Ui.printGreeting(); |
52 | 57 |
|
53 | | - } else if (input.length() >= 8 && input.substring(0,8).equals("deadline")) { |
54 | | - items.add(new Deadline(input.substring(8))); |
55 | | - } else if (input.length() >= 4 && input.substring(0,4).equals("todo")) { |
56 | | - items.add(new Todo(input.substring(4))); |
57 | | - } else if (input.length() >= 5 && input.substring(0,5).equals("event")) { |
58 | | - items.add(new Event(input.substring(5))); |
59 | | - } else if (input.length() >= 6 && input.substring(0,6).equals("delete")) { |
60 | | - int taskToDelete = Integer.parseInt(input.substring(7)); |
61 | | - Task toRemove = items.get(taskToDelete - 1); |
62 | | - items.remove(taskToDelete - 1); |
63 | | - System.out.println("Noted. I've removed this task:\n " |
64 | | - + toRemove + "\nNow you have " + items.size() + " tasks in the list."); |
| 58 | + while (true) { |
| 59 | + String userInput = ui.getUserInput(); |
| 60 | + Parser p; |
65 | 61 |
|
66 | | - } else { |
67 | | - items.add(new Task(input)); |
68 | | - } |
69 | | - } catch (Exception e) { |
70 | | - System.out.println(e); |
| 62 | + try { |
| 63 | + p = Parser.parse(userInput); |
| 64 | + } catch (DukeException e) { |
| 65 | + System.out.println(e.getMessage()); |
| 66 | + continue; |
71 | 67 | } |
| 68 | + if (p.getCommand().equals("bye")) { |
| 69 | + break; |
| 70 | + } |
| 71 | + this.process(p); |
72 | 72 | } |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * prints Goodbye message. |
77 | | - */ |
78 | | - public static void bye() { |
79 | | - System.out.println("Bye. Hope to see you again soon!"); |
80 | | - } |
81 | | - |
82 | | - public static void errorHandling(String input) throws DukeException { |
83 | | - if (input.length() == 4 && input.equals("todo")) { |
84 | | - throw new DukeException(" ☹ OOPS!!! The description of a todo cannot be empty."); |
85 | | - } else if (input.length() == 8 && input.equals("deadline")) { |
86 | | - throw new DukeException(" ☹ OOPS!!! The description of a deadline cannot be empty."); |
87 | | - } else if (input.length() == 5 && input.equals("event")) { |
88 | | - throw new DukeException(" ☹ OOPS!!! The description of a event cannot be empty."); |
89 | | - } else if (input.length() >= 4 && input.substring(0,4).equals("blah")) { |
90 | | - throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-("); |
91 | | - } else if (input.isBlank()) { |
92 | | - throw new DukeException(" ☹ OOPS!!! A Task cannot be empty."); |
93 | | - } else {} |
| 73 | + Ui.printGoodbye(); |
94 | 74 | } |
95 | 75 | } |
0 commit comments