@@ -42,34 +42,51 @@ public void createFile() {
4242 * @return the ui message on addition of the task
4343 */
4444 public String addToList (String taskName , Ui ui ) {
45+ if (taskName .matches ("\\ btodo\\ s.*\\ b" )) {
46+ assert taskName .length () >= 6 : "addToList function is not working for todo" ;
47+ Task newTask = new ToDo (taskName .substring (5 ), false , 0 );
48+ tasks .add (newTask );
49+ return ui .taskAddMsg (newTask , tasks .size ());
50+ }
51+ if (taskName .matches ("\\ bevent\\ s.*\\ s/at\\ s.*\\ b" )) {
52+ assert taskName .length () >= 13 : "addToList function is not working for event" ;
53+ String des = taskName .substring (6 , taskName .indexOf ("/" ) - 1 );
54+ String at = taskName .substring (taskName .indexOf ("/" ) + 4 , taskName .length ());
55+ Task newTask = new Event (des , false , at , 0 );
56+ tasks .add (newTask );
57+ return ui .taskAddMsg (newTask , tasks .size ());
58+ }
59+ if (taskName .matches ("\\ bdeadline\\ s.*\\ s/by\\ s.*\\ b" )) {
60+ String des = taskName .substring (9 , taskName .indexOf ("/" ) - 1 );
61+ String by = taskName .substring (taskName .indexOf ("/" ) + 4 );
62+ Task newTask = new Deadline (des , false , by , 0 );
63+ tasks .add (newTask );
64+ return ui .taskAddMsg (newTask , tasks .size ());
65+ } else {
66+ return handleException (taskName );
67+ }
68+ }
69+
70+ /**
71+ * Handles exceptions where the user input does not match any of the task list commands.
72+ *
73+ * @param taskName description of the task to be added.
74+ * @return the error message to show to the user.
75+ */
76+ public String handleException (String taskName ) {
4577 try {
46- if (taskName .matches ("\\ btodo\\ s.*\\ b" )) {
47- assert taskName .length () >= 6 : "addToList function is not working for todo" ;
48- Task newTask = new ToDo (taskName .substring (5 ), false , 0 );
49- tasks .add (newTask );
50- return ui .taskAddMsg (newTask , tasks .size ());
51- }
52- if (taskName .matches ("\\ bevent\\ s.*\\ s/at\\ s.*\\ b" )) {
53- assert taskName .length () >= 13 : "addToList function is not working for event" ;
54- String des = taskName .substring (6 , taskName .indexOf ("/" ) - 1 );
55- String at = taskName .substring (taskName .indexOf ("/" ) + 4 , taskName .length ());
56- Task newTask = new Event (des , false , at , 0 );
57- tasks .add (newTask );
58- return ui .taskAddMsg (newTask , tasks .size ());
59- }
60- if (taskName .matches ("\\ bdeadline\\ s.*\\ s/by\\ s.*\\ b" )) {
61- String des = taskName .substring (9 , taskName .indexOf ("/" ) - 1 );
62- String by = taskName .substring (taskName .indexOf ("/" ) + 4 );
63- Task newTask = new Deadline (des , false , by , 0 );
64- tasks .add (newTask );
65- return ui .taskAddMsg (newTask , tasks .size ());
66- }
6778 if (taskName .matches ("\\ btodo\\ s+" ) || taskName .matches ("\\ btodo\\ b" )) {
6879 throw new DukeException ("Sorry please provide a task to be done!" );
80+ }
81+ if (taskName .matches ("\\ bevent\\ s.*\\ b" )) {
82+ throw new DukeException ("Sorry please provide the time of the event!" );
83+ }
84+ if (taskName .matches ("\\ bdeadline\\ s.*\\ b" )) {
85+ throw new DukeException ("Sorry please provide the date of the deadline!" );
6986 } else {
7087 throw new DukeException ("I'm sorry, I don't know what that means!" );
7188 }
72- } catch (Exception e ) {
89+ } catch (DukeException e ) {
7390 String msg = e .getMessage ();
7491 return msg ;
7592 }
0 commit comments