1+ import java .io .File ;
2+ import java .io .FileWriter ;
3+ import java .io .IOException ;
14import java .util .Scanner ;
25import java .util .ArrayList ;
36
47public class Duke {
58
69 public Task [] tasks ;
710
11+ private static final ArrayList <Task > collection = new ArrayList <>();
812
9- public static void main (String [] args ) throws DukeException {
13+ public static final Scanner sc = new Scanner (System .in );
14+
15+
16+ public static boolean isNotValid (String str ) {
17+ String [] s = str .split (" " );
18+ return (s .length < 1 );
19+ }
20+
21+
22+
23+
24+ public static void main (String [] args ) throws DukeException , IOException , IllegalCommandException {
1025 String logo = " ____ _ \n "
1126 + "| _ \\ _ _| | _____ \n "
1227 + "| | | | | | | |/ / _ \\ \n "
@@ -15,116 +30,172 @@ public static void main(String[] args) throws DukeException {
1530 System .out .println ("Hello from\n " + logo );
1631 System .out .println ("___________________________________" );
1732 System .out .println ("Hello! I'm Duke\n What can I do for you?" );
18- System .out .println ("___________________________________ " );
33+ System .out .println ("____________________________________ " );
1934
20- Task [] tasks = new Task [100 ];
21- int counter = 0 ;
22- ArrayList <Task > collection = new ArrayList <>();
23-
24- Scanner sc = new Scanner (System .in );
2535 String input = sc .nextLine ();
2636
37+ File file = new File ("database/duke.txt" );
38+ if (file .createNewFile ()) {
39+ System .out .println ("File has been created: " + file .getName ());
40+ } else {
41+ System .out .println ("File already exists." );
42+ Scanner reader = new Scanner (file );
43+ while (reader .hasNext ()) {
44+ reader .useDelimiter (" \\ | " );
45+ Constants constant = Constants .valueOf (reader .next ().toUpperCase ());
46+ Task task ;
47+ switch (constant ) {
48+ case TODO :
49+ String tdDescription = reader .next ();
50+ Todo todo = new Todo (tdDescription );
51+ collection .add (todo );
52+ break ;
53+ case DEADLINE :
54+ String dlDescription = reader .next ();
55+ Deadline deadline = new Deadline (dlDescription );
56+ collection .add (deadline );
57+ break ;
58+ case EVENT :
59+ String evDescription = reader .next ();
60+ Event event = new Event (evDescription );
61+ collection .add (event );
62+ break ;
63+
64+ default :
65+ throw new IllegalCommandException ("Illegal command given" );
66+ }
67+ }
68+ }
69+
70+
2771
2872
2973 while (!input .equals ("bye" )) {
74+
3075 if (input .equals ("list" )) {
3176 System .out .println ("___________________________________" );
32- for (int i = 0 ; i < counter ; i ++) {
77+ for (int i = 0 ; i < collection . size () ; i ++) {
3378 System .out .println ( (i +1 ) + "."
3479 + collection .get (i ).toString ());
3580 }
3681 System .out .println ("___________________________________" );
37- input = sc .next ();
82+ input = sc .nextLine ();
3883
3984 }
4085
41- else if (input .equals ("mark" )) {
42- int number = sc . nextInt ( );
86+ if (input .startsWith ("mark" )) {
87+ int number = Integer . parseInt ( input . substring ( 5 ) );
4388 collection .get (number -1 ).mark ();
4489 System .out .println ("___________________________________" );
4590 System .out .println ("Nice! I've marked this task done: " + "\n "
4691 + "[" + collection .get (number -1 ).getStatusIcon () + "] " +
4792 collection .get (number -1 ).description );
4893 System .out .println ("___________________________________" );
49- input = sc .next ();
94+ input = sc .nextLine ();
5095 }
5196
52- else if (input .equals ("unmark" )) {
53- int number = sc . nextInt ( );
97+ if (input .equals ("unmark" )) {
98+ int number = Integer . parseInt ( input . substring ( 7 ) );
5499 collection .get (number -1 ).unmark ();
55100 System .out .println ("___________________________________" );
56101 System .out .println ("OK, I've marked this task as not done yet: " + "\n "
57102 + "[" + collection .get (number -1 ).getStatusIcon () + "] " +
58103 collection .get (number -1 ).description );
59104 System .out .println ("___________________________________" );
60- input = sc .next ();
105+ input = sc .nextLine ();
61106 }
62107
63- else if (input .equals ("deadline" )) {
64- String what = sc .nextLine ();
65- if (what .equals ("" )) throw new DukeException ("☹ OOPS!!! The description of a deadline cannot be empty." );
66- String byWhen = sc .nextLine ();
67- collection .add (new Deadline (what , byWhen ));
68- System .out .println ("___________________________________" );
69- System .out .println ("Got it. I've added this task:" + "\n "
70- + " " + collection .get (counter ).toString () + "\n "
71- + "Now you have " + (counter +1 ) + " tasks in the list." );
72- System .out .println ("___________________________________" );
73- counter ++;
74- input = sc .nextLine ();
108+ if (input .startsWith ("deadline" )) {
109+ try {
110+ if (isNotValid (input ))
111+ throw new DukeException ("☹ OOPS!!! The description of a deadline cannot be empty." );
112+ Deadline deadline = new Deadline (input );
113+ collection .add (deadline );
114+ System .out .println ("___________________________________" );
115+ System .out .println ("Got it. I've added this task:" + "\n "
116+ + " " + deadline .toString () + "\n "
117+ + "Now you have " + collection .size () + " tasks in the list." );
118+ System .out .println ("___________________________________" );
119+ } catch (DukeException e ) {
120+ System .out .println (e .getMessage ());
121+
122+ } finally {
123+ input = sc .nextLine ();
124+ }
75125 }
76126
77- else if (input .equals ("todo" )) {
78- String what = sc .nextLine ();
79- if (what .equals ("" )) throw new DukeException ("☹ OOPS!!! The description of a todo cannot be empty." );
80- collection .add (new Todo (what ));
81- System .out .println ("___________________________________" );
82- System .out .println ("Got it. I've added this task:" + "\n "
83- + " " + collection .get (counter ).toString () + "\n "
84- + "Now you have " + (counter +1 ) + " tasks in the list." );
85- System .out .println ("___________________________________" );
86- counter ++;
87- input = sc .nextLine ();
127+ if (input .startsWith ("todo" )) {
128+ try {
129+ if (isNotValid (input ))
130+ throw new DukeException ("☹ OOPS!!! The description of a todo cannot be empty." );
131+ Todo todo = new Todo (input );
132+ collection .add (todo );
133+ System .out .println ("___________________________________" );
134+ System .out .println ("Got it. I've added this task:" + "\n "
135+ + " " + todo .toString () + "\n "
136+ + "Now you have " + collection .size () + " tasks in the list." );
137+ System .out .println ("___________________________________" );
138+ } catch (DukeException e ) {
139+ System .out .println (e .getMessage ());
140+
141+ } finally {
142+ input = sc .nextLine ();
143+ }
88144 }
89145
90- else if (input .equals ("event" )) {
91- String what = sc .nextLine ();
92- if (what .equals ("" )) throw new DukeException ("☹ OOPS!!! The description of a event cannot be empty." );
93- String atWhen = sc .nextLine ();
94- collection .add (new Event (what , atWhen ));
95- System .out .println ("___________________________________" );
96- System .out .println ("Got it. I've added this task:" + "\n "
97- + " " + collection .get (counter ).toString () + "\n "
98- + "Now you have " + (counter +1 ) + " tasks in the list." );
99- System .out .println ("___________________________________" );
100- counter ++;
101- input = sc .nextLine ();
146+ if (input .startsWith ("event" )) {
147+ try {
148+ if (isNotValid ((input )))
149+ throw new DukeException ("☹ OOPS!!! The description of a event cannot be empty." );
150+ Event event = new Event (input );
151+ collection .add (event );
152+ System .out .println ("___________________________________" );
153+ System .out .println ("Got it. I've added this task:" + "\n "
154+ + " " + event .toString () + "\n "
155+ + "Now you have " + collection .size () + " tasks in the list." );
156+ System .out .println ("___________________________________" );
157+ } catch (DukeException e ) {
158+ System .out .println (e .getMessage ());
159+
160+ } finally {
161+ input = sc .nextLine ();
162+ }
102163 }
103164
104- else if (input .equals ("delete" )) {
105- int number = sc .nextInt ();
165+
166+ if (input .startsWith ("delete" )) {
167+ int number = Integer .parseInt (input .substring (7 ));
106168 Task temp = collection .get (number -1 );
107169 collection .remove (number -1 );
108- counter --;
109170 System .out .println ("___________________________________" );
110171 System .out .println ("Noted. I've removed this task:" + "\n "
111172 + " " + temp .toString () + "\n "
112- + "Now you have " + counter + " tasks in the list." );
173+ + "Now you have " + collection . size () + " tasks in the list." );
113174 System .out .println ("___________________________________" );
114- input = sc .next ();
175+ input = sc .nextLine ();
115176
116177 }
117178
118179 else {
119- System .out .println ("___________________________________" );
120- throw new DukeException (" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(" );
121-
180+ try {
181+ throw new DukeException (" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(." );
182+ } catch (DukeException e ) {
183+ System .out .println (e .getMessage ());
184+ } finally {
185+ input = sc .nextLine ();
186+ }
122187 }
123188
124189 }
190+ FileWriter fileWriter = new FileWriter (file );
191+ for (Task task : collection ) {
192+ fileWriter .write (task .fileFormat () + System .lineSeparator ());
193+ }
194+ fileWriter .close ();
125195
126196 System .out .println ("___________________________________" );
127197 System .out .println ("Bye. Hope to see you again soon!" );
128198 System .out .println ("___________________________________" );
129199 }
200+
130201}
0 commit comments