11import java .util .Scanner ;
2+
23public class Floda {
4+ private static int taskCounter = 0 ;
35 public static void main (String [] args ) {
46 String name = "Floda" ;
57 System .out .println ("Hello! I'm " + name );
68 Task [] list = new Task [100 ];
7- int taskCounter = 0 ;
89 Scanner scanner = new Scanner (System .in );
910 System .out .println ("I can keep track of a to-do list for you! Just type what you want to add to the list." );
1011 while (true ) {
@@ -28,19 +29,19 @@ public static void main(String[] args) {
2829 }
2930 break ;
3031 case "mark" :
31- handleMarkTask (line , list , taskCounter );
32+ handleMarkTask (line , list );
3233 break ;
3334 case "unmark" :
34- handleUnmarkTask (line , list , taskCounter );
35+ handleUnmarkTask (line , list );
3536 break ;
3637 case "deadline" :
37- handleDeadlineTask (line , list , taskCounter );
38+ handleDeadlineTask (line , list );
3839 break ;
3940 case "todo" :
40- handleTodoTask (line , list , taskCounter );
41+ handleTodoTask (line , list );
4142 break ;
4243 case "event" :
43- handleEventTask (line , list , taskCounter );
44+ handleEventTask (line , list );
4445 break ;
4546 default :
4647 throw new InvalidInputException ("Invalid input!" );
@@ -49,41 +50,42 @@ public static void main(String[] args) {
4950 }
5051 }
5152 }
52- private static void handleMarkTask (String line , Task [] list , int taskCounter ) {
53+
54+ private static void handleMarkTask (String line , Task [] list ) throws InvalidInputException {
5355 Scanner taskScanner = new Scanner (line );
5456 taskScanner .next ();
5557 if (taskScanner .hasNextInt ()) {
5658 int taskNumber = taskScanner .nextInt () - 1 ;
57- if (isValidTaskNumber (taskNumber , taskCounter )) {
59+ if (isValidTaskNumber (taskNumber )) {
5860 list [taskNumber ].setDone (true );
5961 System .out .println ("I have marked this task as done:\n " + list [taskNumber ]);
6062 } else {
61- System . out . println ("Invalid task number! Please check with 'list'." );
63+ throw new InvalidInputException ("Invalid task number! Please check with 'list'." );
6264 }
6365 } else {
64- System . out . println ("Invalid input! Please check with 'list'." );
66+ throw new InvalidInputException ("Invalid input! Please check with 'list'." );
6567 }
6668 taskScanner .close ();
6769 }
6870
69- private static void handleUnmarkTask (String line , Task [] list , int taskCounter ) {
71+ private static void handleUnmarkTask (String line , Task [] list ) throws InvalidInputException {
7072 Scanner taskScanner = new Scanner (line );
7173 taskScanner .next ();
7274 if (taskScanner .hasNextInt ()) {
7375 int taskNumber = taskScanner .nextInt () - 1 ;
74- if (isValidTaskNumber (taskNumber , taskCounter )) {
76+ if (isValidTaskNumber (taskNumber )) {
7577 list [taskNumber ].setDone (false );
7678 System .out .println ("I have marked this task as not done:\n " + list [taskNumber ]);
7779 } else {
78- System . out . println ("Invalid task number! Please check with 'list'." );
80+ throw new InvalidInputException ("Invalid task number! Please check with 'list'." );
7981 }
8082 } else {
81- System . out . println ("Invalid input! Please check with 'list'." );
83+ throw new InvalidInputException ("Invalid input! Please check with 'list'." );
8284 }
8385 taskScanner .close ();
8486 }
8587
86- private static void handleDeadlineTask (String line , Task [] list , int taskCounter ) {
88+ private static void handleDeadlineTask (String line , Task [] list ) throws InvalidInputException {
8789 Scanner taskScanner = new Scanner (line );
8890 taskScanner .next ();
8991 String remaining = taskScanner .nextLine ().trim ();
@@ -95,7 +97,7 @@ private static void handleDeadlineTask(String line, Task[] list, int taskCounter
9597 System .out .println ("Added: " + list [taskCounter - 1 ] + "\n Now you have " + (taskCounter ) + " items in the list!" );
9698 }
9799
98- private static void handleTodoTask (String line , Task [] list , int taskCounter ) {
100+ private static void handleTodoTask (String line , Task [] list ) throws InvalidInputException {
99101 Scanner taskScanner = new Scanner (line );
100102 taskScanner .next ();
101103 String remaining = taskScanner .nextLine ().trim ();
@@ -104,7 +106,7 @@ private static void handleTodoTask(String line, Task[] list, int taskCounter) {
104106 System .out .println ("Added: " + list [taskCounter - 1 ] + "\n Now you have " + (taskCounter ) + " items in the list!" );
105107 }
106108
107- private static void handleEventTask (String line , Task [] list , int taskCounter ) {
109+ private static void handleEventTask (String line , Task [] list ) throws InvalidInputException {
108110 Scanner taskScanner = new Scanner (line );
109111 taskScanner .next ();
110112 String remaining = taskScanner .nextLine ().trim ();
@@ -119,7 +121,7 @@ private static void handleEventTask(String line, Task[] list, int taskCounter) {
119121
120122 }
121123
122- private static boolean isValidTaskNumber (int taskNumber , int taskCounter ) {
124+ private static boolean isValidTaskNumber (int taskNumber ) {
123125 return taskNumber >= 0 && taskNumber < taskCounter ;
124126 }
125127}
0 commit comments