11import java .util .Scanner ;
2- import java .util .List ;
3- import java .util .ArrayList ;
42
53public class Duke {
6- public static void main (String [] args ) throws TodoException , DeadlineException , EventException , UnknownException {
4+ private Storage storage ;
5+ private TaskList tasks ;
6+ private Ui ui ;
7+
8+ public Duke (String filePath ) {
9+ ui = new Ui ();
10+ storage = new Storage (filePath );
11+ tasks = new TaskList ();
12+ /*try {
13+ tasks = new TaskList(storage.load());
14+ } catch (DukeException e) {
15+ ui.showLoadingError();
16+ tasks = new TaskList();
17+ }*/
18+ }
19+
20+ public void run () {
721 Scanner sc = new Scanner (System .in );
8- List <Task > tasks = new ArrayList <>();
9- String logo = " ____ _ \n "
10- + "| _ \\ _ _| | _____ \n "
11- + "| | | | | | | |/ / _ \\ \n "
12- + "| |_| | |_| | < __/\n "
13- + "|____/ \\ __,_|_|\\ _\\ ___|\n " ;
14- System .out .println ("Hello from\n " + logo );
15- System .out .println ("What can I do for you?" );
22+ ui .reply ();
23+ Parser parser = new Parser (tasks , ui );
1624 while (true ) {
1725 String command = sc .nextLine ();
18- if (command .equals ("list" )) {
19- System .out .println ("Here are the tasks in your list:" );
20- for (int i = 0 ; i < tasks .size (); i ++) {
21- System .out .println (i + 1 + "." + tasks .get (i ).toString ());
22- }
23- } else if (command .contains ("todo" )) {
24- String [] line = command .split (" " );
25- int length = line .length ;
26- if (length == 1 ) {
27- throw new TodoException ("Oops! The description for Todo cannot be empty." );
28- }
29- System .out .println ("Got it. I've added this task:" );
30- String description = "" ;
31- String date = "" ;
32- String time = "" ;
33- boolean isDesc = true ;
34- boolean isDate = true ;
35- for (int i = 1 ; i < length ; i ++) {
36- if (line [i ].equals ("/at" )) {
37- isDesc = false ;
38- } else if (isDesc ) {
39- if (i + 1 == length ) {
40- description += line [i ];
41- } else {
42- description += line [i ] + " " ;
43- }
44- } else if (isDate ) {
45- date += line [i ];
46- isDate = false ;
47- } else {
48- time += line [i ];
49- }
50- }
51- Todo todo = new Todo (description , date , time );
52- tasks .add (todo );
53- System .out .println (todo .toString ());
54- System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
55- } else if (command .contains ("deadline" )) {
56- String [] line = command .split (" " );
57- int length = line .length ;
58- if (length == 1 ) {
59- throw new DeadlineException ("Oops! The description for Deadline cannot be empty." );
60- }
61- System .out .println ("Got it. I've added this task:" );
62- String description = "" ;
63- String date = "" ;
64- String time = "" ;
65- boolean isDesc = true ;
66- boolean isDate = true ;
67- for (int i = 1 ; i < length ; i ++) {
68- if (line [i ].equals ("/by" )) {
69- isDesc = false ;
70- } else if (isDesc ) {
71- if (i + 1 == length ) {
72- description += line [i ];
73- } else {
74- description += line [i ] + " " ;
75- }
76- } else if (isDate ) {
77- date += line [i ];
78- isDate = false ;
79- } else {
80- time += line [i ];
81- }
82- }
83- Deadline deadline = new Deadline (description , date , time );
84- tasks .add (deadline );
85- System .out .println (deadline .toString ());
86- System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
87- } else if (command .contains ("event" )) {
88- String [] line = command .split (" " );
89- int length = line .length ;
90- if (length == 1 ) {
91- throw new EventException ("Oops! The description for Event cannot be empty." );
92- }
93- System .out .println ("Got it. I've added this task:" );
94- String description = "" ;
95- String date = "" ;
96- String time = "" ;
97- boolean isDesc = true ;
98- boolean isDate = true ;
99- for (int i = 1 ; i < length ; i ++) {
100- if (line [i ].equals ("/at" )) {
101- isDesc = false ;
102- } else if (isDesc ) {
103- if (i + 1 == length ) {
104- description += line [i ];
105- } else {
106- description += line [i ] + " " ;
107- }
108- } else if (isDate ) {
109- date += line [i ];
110- isDate = false ;
111- } else {
112- time += line [i ];
113- }
114- }
115- Event event = new Event (description , date , time );
116- tasks .add (event );
117- System .out .println (event .toString ());
118- System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
119- } else if (command .contains ("done" )) {
120- String [] line = command .split (" " );
121- int number = Integer .parseInt (line [1 ]) - 1 ;
122- tasks .get (number ).markAsDone ();
123- System .out .println ("Nice! I've marked this task as done:" );
124- System .out .println (tasks .get (number ).toString ());
125- } else if (command .contains ("delete" )) {
126- System .out .println ("Noted. I've removed this task:" );
127- String [] line = command .split (" " );
128- int number = Integer .parseInt (line [1 ]) - 1 ;
129- Task removedTask = tasks .remove (number );
130- System .out .println (removedTask .toString ());
131- System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
132- } else if (command .equals ("bye" )) {
133- System .out .println ("Bye! Hope to see you again soon!" );
26+ parser .insertCommand (command );
27+ parser .process ();
28+ if (parser .isFinished ()) {
13429 break ;
135- } else {
136- throw new UnknownException ("Oops! I'm sorry, but I don't know what that means!" );
13730 }
13831 }
13932 System .exit (0 );
14033 }
34+
35+ public static void main (String [] args ) {
36+ new Duke ("C:/ip/src/main/java/Duke.java" ).run ();
37+ }
14138}
0 commit comments