parse() and saveTaks() finished
This commit is contained in:
+75
-10
@@ -1,7 +1,15 @@
|
|||||||
|
package week9;
|
||||||
|
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class Week9 {
|
public class Week9 {
|
||||||
private static ArrayList<Task> tasks;
|
private static ArrayList<Task> tasks;
|
||||||
@@ -30,15 +38,65 @@ public class Week9 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void parse(File f) {
|
private void parse(File f) {
|
||||||
// TODO: Need to open the file and get each line.
|
try (BufferedReader myReader = new BufferedReader(new FileReader(f))) {
|
||||||
// TODO: Parse the format ("TITLE, DESCRIPTION, DUE DATE, COMPLETED"),
|
String line;
|
||||||
// TODO: Create a new Task object and add it to the ArrayList.
|
String[] data;
|
||||||
|
|
||||||
|
String title, desc, date;
|
||||||
|
boolean completed;
|
||||||
|
Task task;
|
||||||
|
|
||||||
|
while ((line = myReader.readLine()) != null) {
|
||||||
|
data = line.split(",");
|
||||||
|
title = data[0];
|
||||||
|
desc = data[1];
|
||||||
|
date = data[2];
|
||||||
|
|
||||||
|
completed = "true".contentEquals(data[3]);
|
||||||
|
task = new Task(title, desc, date);
|
||||||
|
if (completed)
|
||||||
|
task.toggleComplete();
|
||||||
|
|
||||||
|
tasks.add(task);
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("File error! File not found.");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
System.out.println("IOException apparently");
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Writes each task to the file after a change.
|
// Writes each task to the file after a change.
|
||||||
private static void saveTasks() {
|
private static void saveTasks() {
|
||||||
System.out.println("writing tasks to " + filepath);
|
System.out.println("writing tasks to " + filepath);
|
||||||
// TODO: Write each Task stored in the ArrayList to the file.
|
try (BufferedWriter myWriter = new BufferedWriter(
|
||||||
|
new FileWriter(filepath))) {
|
||||||
|
myWriter.flush();
|
||||||
|
String line;
|
||||||
|
|
||||||
|
String title, desc, date;
|
||||||
|
boolean completed;
|
||||||
|
|
||||||
|
for (Task task : tasks) {
|
||||||
|
title = task.getTitle();
|
||||||
|
desc = task.getDescription();
|
||||||
|
date = task.getDueDate();
|
||||||
|
completed = task.isCompleted();
|
||||||
|
line = String.format("%s,%s,%s,%b", title, desc, date,
|
||||||
|
completed);
|
||||||
|
|
||||||
|
myWriter.append(line);
|
||||||
|
myWriter.newLine();
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("File error! File not found.");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
System.out.println("IOException apparently");
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////// END OF TODO ////////////////////////////
|
///////////////////////// END OF TODO ////////////////////////////
|
||||||
@@ -50,7 +108,8 @@ public class Week9 {
|
|||||||
testData();
|
testData();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identify the operating system of the user, and obtain the path to the "tasks" directory.
|
// Identify the operating system of the user, and obtain the path to the
|
||||||
|
// "tasks" directory.
|
||||||
private String getNotesDirectoryPath() {
|
private String getNotesDirectoryPath() {
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
|
||||||
@@ -65,7 +124,8 @@ public class Week9 {
|
|||||||
return userHome + "/" + tasksDir;
|
return userHome + "/" + tasksDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the tasks file if it does not already exist. Load tasks from the existing file otherwise.
|
// Create the tasks file if it does not already exist. Load tasks from the
|
||||||
|
// existing file otherwise.
|
||||||
private void initTasks() {
|
private void initTasks() {
|
||||||
File f = new File(filepath);
|
File f = new File(filepath);
|
||||||
tasks = new ArrayList<Task>();
|
tasks = new ArrayList<Task>();
|
||||||
@@ -73,7 +133,8 @@ public class Week9 {
|
|||||||
if (f.isFile()) {
|
if (f.isFile()) {
|
||||||
System.out.println("loading tasks from " + filepath);
|
System.out.println("loading tasks from " + filepath);
|
||||||
parse(f);
|
parse(f);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
try {
|
try {
|
||||||
f.createNewFile();
|
f.createNewFile();
|
||||||
System.out.println(filepath + " has been created.");
|
System.out.println(filepath + " has been created.");
|
||||||
@@ -83,10 +144,13 @@ public class Week9 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test tasks, not read from a file and are not intended to be saved to a file.
|
// Test tasks, not read from a file and are not intended to be saved to a
|
||||||
|
// file.
|
||||||
private void testData() {
|
private void testData() {
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
|
tasks.add(new Task("Title " + i, "Description " + i,
|
||||||
|
"Due Date " + i));
|
||||||
|
saveTasks();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -109,7 +173,8 @@ public class Week9 {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate, filterByCompleted);
|
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate,
|
||||||
|
filterByCompleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user