11package burger .list ;
22
33import java .util .ArrayList ;
4- import java .util .IllegalFormatException ;
54
65import burger .BurgerException ;
76import burger .task .Deadline ;
109import burger .task .Todo ;
1110
1211import static burger .Burger .printLine ;
12+ import static burger .BurgerException .*;
1313
1414public class List {
1515
16- private final ArrayList <Task > todoList ;
16+ private final ArrayList <Task > taskList ;
1717
1818 public int totalTasks ;
1919 public List () {
20- todoList = new ArrayList <>();
20+ taskList = new ArrayList <>();
2121 totalTasks = 0 ;
2222 }
2323 /**
2424 * Returns boolean value base on whether the input matches one of the commands.
2525 *
26+ *
2627 * @param textArray user input in array type.
2728 * @return true if it's a valid command and false otherwise.
29+ * @throws ArrayIndexOutOfBoundsException when the description after the command is empty.
2830 */
2931 public boolean isValidCommand (String [] textArray ) {
3032 boolean isValid = true ;
31- String command = textArray [0 ];
32- switch (command ) { // assume inputs for commands are correct
33- case "mark" :
34- // fallthrough
35- case "unmark" :
36- int idx = Integer .parseInt (textArray [1 ]) - 1 ;
37- getMark (idx , command );
38- break ;
39- case "todo" :
40- addTodo (textArray );
41- break ;
42- case "deadline" :
43- addDeadline (textArray );
44- break ;
45- case "event" :
46- addEvent (textArray );
47- break ;
48- default :
49- isValid = false ;
50- break ;
33+ try {
34+ String command = textArray [0 ];
35+ switch (command ) {
36+ case "mark" :
37+ // fallthrough
38+ case "unmark" :
39+ int idx = Integer .parseInt (textArray [1 ]) - 1 ;
40+ getMark (idx , command );
41+ break ;
42+ case "todo" :
43+ addTodo (textArray );
44+ break ;
45+ case "deadline" :
46+ addDeadline (textArray );
47+ break ;
48+ case "event" :
49+ addEvent (textArray );
50+ break ;
51+ default :
52+ isValid = false ;
53+ break ;
54+ }
55+ return isValid ;
56+ } catch (ArrayIndexOutOfBoundsException e ) {
57+ printEmptyDescription ();
58+ return isValid ;
5159 }
52- return isValid ;
60+
5361 }
5462
5563 /**
@@ -58,15 +66,12 @@ public boolean isValidCommand(String[] textArray) {
5866 *
5967 * @param idx index of task.
6068 * @return task.
61- * @throws ArrayIndexOutOfBoundsException idx is out of bounds.
69+ * @throws IndexOutOfBoundsException idx is out of bounds.
6270 */
6371 public Task getTask (int idx ) {
6472 try {
65- return this .todoList .get (idx );
66- } catch (ArrayIndexOutOfBoundsException e ) {
67- printLine ();
68- System .out .println ("UwU? Do you know how to count?" );
69- printLine ();
73+ return this .taskList .get (idx );
74+ } catch (IndexOutOfBoundsException e ) {
7075 return null ;
7176 }
7277 }
@@ -76,68 +81,56 @@ public Task getTask(int idx) {
7681 * @param todo the todo input by the user.
7782 */
7883 public void addTodo (String [] todo ) {
79- try {
80- if (todo .length == 1 ) {
81- throw new BurgerException ();
82- }
83- StringBuilder todoText = new StringBuilder (todo [1 ]);
84- for (int i = 2 ; i < todo .length ; i ++) {
85- todoText .append (' ' ).append (todo [i ]);
86- }
87- Task newTodo = new Todo (todoText .toString ());
88- addTask (newTodo );
89- } catch (BurgerException e ) {
90- printEmptyDescription ();
84+ StringBuilder todoText = new StringBuilder (todo [1 ]);
85+ for (int i = 2 ; i < todo .length ; i ++) {
86+ todoText .append (' ' ).append (todo [i ]);
9187 }
88+ Task newTodo = new Todo (todoText .toString ());
89+ addTask (newTodo );
9290 }
9391
9492 /**
9593 * Adds a new event to the list.
9694 *
9795 * @param event the event input by the user.
98- * @throws ArrayIndexOutOfBoundsException if text is empty after the "event" command.
99- * @throws IllegalFormatException if event format is not followed.
96+ * @throws ArrayIndexOutOfBoundsException if deadline format is not followed.
10097 */
10198 public void addEvent (String [] event ) {
10299 try {
103- if (event .length == 1 ) {
104- throw new BurgerException ();
105- }
106100 StringBuilder eventText = new StringBuilder (event [1 ]);
107101 int i = 2 ;
108- while (!event [i ].startsWith ("/" )) {
102+ while (!event [i ].startsWith ("/from " )) {
109103 eventText .append (' ' ).append (event [i ]);
110104 i ++;
111105 }
112106 eventText .append (' ' ).append ("(from: " );
113107 i ++;
114- while (!event [i ].startsWith ("/" )) {
115- eventText .append (' ' ). append ( event [i ]);
108+ while (!event [i ].startsWith ("/to " )) {
109+ eventText .append (event [i ]). append ( ' ' );
116110 i ++;
117111 }
112+ eventText .append ("to: " );
118113 i ++;
119- eventText .append (' ' ).append ("to: " ).append (event [i ]).append (')' );
114+ while (i < event .length - 1 ) {
115+ eventText .append (event [i ]).append (' ' );
116+ i ++;
117+ }
118+ eventText .append (event [i ]).append (')' );
120119 Task newEvent = new Event (eventText .toString ());
121120 addTask (newEvent );
122121 } catch (ArrayIndexOutOfBoundsException e ) {
123- printInvalidInputMessage ();
124- } catch (BurgerException e ) {
125- printEmptyDescription ();
122+ printInvalidEventInputMessage ();
126123 }
127124 }
128125
129126 /**
130127 * Adds a new deadline to the list.
131128 *
132129 * @param deadline the deadline input by the user.
133- * @throws ArrayIndexOutOfBoundsException if text is empty after the "deadline" command.
134- * @throws IllegalFormatException if deadline format is not followed.
130+ * @throws ArrayIndexOutOfBoundsException if deadline format is not followed.
135131 */
136132 public void addDeadline (String [] deadline ) {
137133 try {
138- if (deadline .length == 1 ) {
139- throw new BurgerException ();
140- }
141134 StringBuilder deadlineText = new StringBuilder (deadline [1 ]);
142135 int i = 2 ;
143136 while (!deadline [i ].startsWith ("/" )) {
@@ -154,34 +147,20 @@ public void addDeadline(String[] deadline) {
154147 Task newDeadline = new Deadline (deadlineText .toString ());
155148 addTask (newDeadline );
156149 } catch (ArrayIndexOutOfBoundsException e ) {
157- printInvalidInputMessage ();
158- } catch (BurgerException e ) {
159- printEmptyDescription ();
150+ printInvalidDeadlineInputMessage ();
160151 }
161152 }
162153
163- private static void printInvalidInputMessage () {
164- printLine ();
165- System .out .println ("Oi, the input is wrong!" );
166- printLine ();
167- }
168-
169- private void printEmptyDescription () {
170- printLine ();
171- System .out .println ("UwU~ Where is your description?" );
172- printLine ();
173- }
174-
175154 /**
176155 * Adds task to the list and prints out a message.
177156 *
178157 * @param task Task that is being added.
179158 */
180159 public void addTask (Task task ) {
181- this .todoList .add (task );
160+ this .taskList .add (task );
182161 printLine ();
183162 System .out .println ("Got it. I've added this task:" );
184- this .todoList .get (totalTasks ).printTask ();
163+ this .taskList .get (totalTasks ).printTask ();
185164 this .totalTasks ++;
186165 System .out .println ();
187166 System .out .println ("Now you have " + totalTasks + " tasks in the list." );
@@ -192,36 +171,51 @@ public void addTask(Task task) {
192171 *
193172 * @param idx the index of the task.
194173 * @param command Command to "mark" or "unmark" the task.
174+ * @throws BurgerException if index input is out of the range of list of tasks.
195175 */
196176 public void getMark (int idx , String command ) {
197- Task currTask = getTask (idx );
198- if (command .equals ("mark" )) {
199- System .out .println ("Nice! I've marked this task as done:" );
200- currTask .markDone ();
201- } else {
202- System .out .println ("OK, I've marked this task as not done yet:" );
203- currTask .unmarkDone ();
177+ try {
178+ Task currTask = getTask (idx );
179+ if (currTask == null ) {
180+ throw new BurgerException ();
181+ }
182+ if (command .equals ("mark" )) {
183+ System .out .println ("Nice! I've marked this task as done:" );
184+ currTask .markDone ();
185+ } else {
186+ System .out .println ("OK, I've marked this task as not done yet:" );
187+ currTask .unmarkDone ();
188+ }
204189 printTaskWithLine (idx );
190+ } catch (BurgerException e ) {
191+ printOutOfIndexMessage ();
205192 }
206- printTaskWithLine ( idx );
193+
207194 }
195+
208196 /**
209197 * Prints the task with a horizontal line.
210198 *
211199 * @param idx Index of the task.
212200 */
213201 public void printTaskWithLine (int idx ) {
214- this .todoList .get (idx ).printTask ();
202+ this .taskList .get (idx ).printTask ();
215203 System .out .println ();
216204 printLine ();
217205 }
218206
219- public void printTodoList () {
207+ /**
208+ * Prints task list.
209+ */
210+ public void printTaskList () {
220211 printLine ();
221212 System .out .println ("Here are the tasks in your list:" );
222- for (int i = 0 ; i < todoList .size (); i ++) {
213+ if (taskList .isEmpty ()) {
214+ System .out .println ("Don't be lazy... start doing something" );
215+ }
216+ for (int i = 0 ; i < taskList .size (); i ++) {
223217 System .out .print (i +1 + ". " );
224- todoList .get (i ).printTask ();
218+ taskList .get (i ).printTask ();
225219 System .out .println ();
226220 }
227221 printLine ();
0 commit comments