1+ import java .io .File ;
2+ import java .io .FileWriter ;
3+ import java .io .IOException ;
14import java .util .ArrayList ;
5+ import java .util .Scanner ;
6+
27public class TaskList {
38 private final ArrayList <Task > tasks ;
9+ private static final String FILE_PATH = "./data/duke.txt" ;
410
511 public TaskList () {
612 this .tasks = new ArrayList <>();
13+ loadTasks (); // Ensure tasks are loaded when an instance is created
14+ }
15+
16+ private void loadTasks () {
17+ File file = new File (FILE_PATH );
18+ if (!file .exists ()) {
19+ return ; // Skip loading if file does not exist
20+ }
21+ try (Scanner scanner = new Scanner (file )) {
22+ while (scanner .hasNextLine ()) {
23+ String line = scanner .nextLine ();
24+ Task task = parseLineToTask (line );
25+ if (task != null ) {
26+ tasks .add (task );
27+ }
28+ }
29+ } catch (IOException e ) { // Catching IOException which covers FileNotFoundException
30+ System .out .println ("Unable to read tasks from file: " + e .getMessage ());
31+ }
32+ }
33+
34+ private Task parseLineToTask (String line ) {
35+ String [] parts = line .split (" \\ | " );
36+ String type = parts [0 ];
37+ boolean isDone = parts [1 ].equals ("1" );
38+ String description = parts [2 ];
39+ Task task ;
40+
41+ switch (type ) {
42+ case "T" :
43+ task = new Todo (description );
44+ break ;
45+ case "D" :
46+ String by = parts .length > 3 ? parts [3 ] : "" ;
47+ task = new DeadLine (description , by );
48+ break ;
49+ case "E" :
50+ String from = parts .length > 3 ? parts [3 ] : "" ;
51+ String to = parts .length > 4 ? parts [4 ] : "" ;
52+ task = new Event (description , from , to );
53+ break ;
54+ default :
55+ System .out .println ("Unknown task type in file: " + type );
56+ return null ; // Return null for unknown task types
57+ }
58+ task .isDone = isDone ;
59+ return task ;
760 }
861
62+
63+
64+
965 public void addTask (String userInput ) throws HandleException {
10- // Similar to addTask method in TaskManager, but refactored for this class
1166 Task task = TaskFactory .createTask (userInput );
1267 if (task != null ) {
1368 tasks .add (task );
69+ saveTasks (); // Save the task list to file after adding a new task
1470 System .out .println ("Got it. I've added this task:" );
1571 System .out .println (" " + task );
1672 System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
1773 }
1874 }
1975
76+
2077 public void deleteTask (int taskIndex ) throws HandleException {
2178 if (taskIndex < 1 || taskIndex > tasks .size ()) {
2279 throw new HandleException ("OOPS!!! The task number is invalid." );
@@ -27,10 +84,28 @@ public void deleteTask(int taskIndex) throws HandleException {
2784 System .out .println ("Now you have " + tasks .size () + " tasks in the list." );
2885 }
2986
87+ public void saveTasks () {
88+ try {
89+ File file = new File (FILE_PATH );
90+ if (!file .getParentFile ().mkdirs () && !file .getParentFile ().exists ()) {
91+ System .out .println ("Failed to create directories for the task file." );
92+ return ;
93+ }
94+ try (FileWriter writer = new FileWriter (file , false )) {
95+ for (Task task : tasks ) {
96+ writer .write (task .toFileFormat () + System .lineSeparator ());
97+ }
98+ }
99+ } catch (IOException e ) {
100+ System .out .println ("Unable to save tasks to file: " + e .getMessage ());
101+ }
102+ }
103+
30104 public void listTasks () {
31105 System .out .println ("Here are the tasks in your list:" );
32106 for (int i = 0 ; i < tasks .size (); i ++) {
33107 System .out .println ((i + 1 ) + "." + tasks .get (i ));
34108 }
35109 }
36110}
111+
0 commit comments