parse() and saveTaks() finished
This commit is contained in:
+155
-90
@@ -1,115 +1,180 @@
|
|||||||
|
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;
|
||||||
private static String filepath;
|
private static String filepath;
|
||||||
private static Week9Gui gui;
|
private static Week9Gui gui;
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
// NOTE: For sorts, the Task class needs something implemented.
|
||||||
private static void sortByTitle() {
|
private static void sortByTitle() {
|
||||||
System.out.println("Sort By Title clicked!");
|
System.out.println("Sort By Title clicked!");
|
||||||
// TODO
|
// TODO
|
||||||
gui.update(tasks);
|
gui.update(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
// NOTE: For sorts, the Task class needs something implemented.
|
||||||
private static void sortByDueDate() {
|
private static void sortByDueDate() {
|
||||||
System.out.println("Sort By Due Date clicked!");
|
System.out.println("Sort By Due Date clicked!");
|
||||||
// TODO
|
// TODO
|
||||||
gui.update(tasks);
|
gui.update(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
// NOTE: For sorts, the Task class needs something implemented.
|
||||||
private static void filterByComplete() {
|
private static void filterByComplete() {
|
||||||
System.out.println("Filter By Complete clicked!");
|
System.out.println("Filter By Complete clicked!");
|
||||||
// TODO
|
// TODO
|
||||||
gui.update(tasks);
|
gui.update(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
}
|
|
||||||
|
|
||||||
// Writes each task to the file after a change.
|
String title, desc, date;
|
||||||
private static void saveTasks() {
|
boolean completed;
|
||||||
System.out.println("writing tasks to " + filepath);
|
Task task;
|
||||||
// TODO: Write each Task stored in the ArrayList to the file.
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////// END OF TODO ////////////////////////////
|
while ((line = myReader.readLine()) != null) {
|
||||||
|
data = line.split(",");
|
||||||
|
title = data[0];
|
||||||
|
desc = data[1];
|
||||||
|
date = data[2];
|
||||||
|
|
||||||
// Initialize tasks and GUI, populate with test data (if applicable).
|
completed = "true".contentEquals(data[3]);
|
||||||
private void init() {
|
task = new Task(title, desc, date);
|
||||||
filepath = getNotesDirectoryPath();
|
if (completed)
|
||||||
initTasks();
|
task.toggleComplete();
|
||||||
testData();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Identify the operating system of the user, and obtain the path to the "tasks" directory.
|
tasks.add(task);
|
||||||
private String getNotesDirectoryPath() {
|
}
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("File error! File not found.");
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
System.out.println("IOException apparently");
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String userHome = System.getProperty("user.home");
|
// Writes each task to the file after a change.
|
||||||
String tasksDir = "tasks";
|
private static void saveTasks() {
|
||||||
|
System.out.println("writing tasks to " + filepath);
|
||||||
|
try (BufferedWriter myWriter = new BufferedWriter(
|
||||||
|
new FileWriter(filepath))) {
|
||||||
|
myWriter.flush();
|
||||||
|
String line;
|
||||||
|
|
||||||
if (os.contains("win")) // Windows
|
String title, desc, date;
|
||||||
return userHome + "\\" + tasksDir;
|
boolean completed;
|
||||||
else if (os.contains("mac")) // MacOS
|
|
||||||
return userHome + "/Library/Application Support/" + tasksDir;
|
|
||||||
else // Linux/other
|
|
||||||
return userHome + "/" + tasksDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the tasks file if it does not already exist. Load tasks from the existing file otherwise.
|
for (Task task : tasks) {
|
||||||
private void initTasks() {
|
title = task.getTitle();
|
||||||
File f = new File(filepath);
|
desc = task.getDescription();
|
||||||
tasks = new ArrayList<Task>();
|
date = task.getDueDate();
|
||||||
|
completed = task.isCompleted();
|
||||||
|
line = String.format("%s,%s,%s,%b", title, desc, date,
|
||||||
|
completed);
|
||||||
|
|
||||||
if(f.isFile()) {
|
myWriter.append(line);
|
||||||
System.out.println("loading tasks from " + filepath);
|
myWriter.newLine();
|
||||||
parse(f);
|
}
|
||||||
} else {
|
} catch (FileNotFoundException e) {
|
||||||
try {
|
System.out.println("File error! File not found.");
|
||||||
f.createNewFile();
|
e.printStackTrace();
|
||||||
System.out.println(filepath + " has been created.");
|
} catch (IOException e1) {
|
||||||
} catch(Exception e) {
|
System.out.println("IOException apparently");
|
||||||
System.out.println(e);
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Test tasks, not read from a file and are not intended to be saved to a file.
|
///////////////////////// END OF TODO ////////////////////////////
|
||||||
private void testData() {
|
|
||||||
for(int i = 0; i < 10; i++)
|
|
||||||
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
// Initialize tasks and GUI, populate with test data (if applicable).
|
||||||
new Week9().init();
|
private void init() {
|
||||||
ActionListener sortByTitle = new ActionListener() {
|
filepath = getNotesDirectoryPath();
|
||||||
public void actionPerformed(ActionEvent e) {
|
initTasks();
|
||||||
sortByTitle();
|
testData();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
ActionListener sortByDueDate = new ActionListener() {
|
// Identify the operating system of the user, and obtain the path to the
|
||||||
public void actionPerformed(ActionEvent e) {
|
// "tasks" directory.
|
||||||
sortByDueDate();
|
private String getNotesDirectoryPath() {
|
||||||
}
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
};
|
|
||||||
|
|
||||||
ActionListener filterByCompleted = new ActionListener() {
|
String userHome = System.getProperty("user.home");
|
||||||
public void actionPerformed(ActionEvent e) {
|
String tasksDir = "tasks";
|
||||||
filterByComplete();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate, filterByCompleted);
|
if (os.contains("win")) // Windows
|
||||||
}
|
return userHome + "\\" + tasksDir;
|
||||||
|
else if (os.contains("mac")) // MacOS
|
||||||
|
return userHome + "/Library/Application Support/" + tasksDir;
|
||||||
|
else // Linux/other
|
||||||
|
return userHome + "/" + tasksDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the tasks file if it does not already exist. Load tasks from the
|
||||||
|
// existing file otherwise.
|
||||||
|
private void initTasks() {
|
||||||
|
File f = new File(filepath);
|
||||||
|
tasks = new ArrayList<Task>();
|
||||||
|
|
||||||
|
if (f.isFile()) {
|
||||||
|
System.out.println("loading tasks from " + filepath);
|
||||||
|
parse(f);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
try {
|
||||||
|
f.createNewFile();
|
||||||
|
System.out.println(filepath + " has been created.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test tasks, not read from a file and are not intended to be saved to a
|
||||||
|
// file.
|
||||||
|
private void testData() {
|
||||||
|
for (int i = 0; i < 10; i++)
|
||||||
|
tasks.add(new Task("Title " + i, "Description " + i,
|
||||||
|
"Due Date " + i));
|
||||||
|
saveTasks();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Week9().init();
|
||||||
|
ActionListener sortByTitle = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sortByTitle();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ActionListener sortByDueDate = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
sortByDueDate();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
ActionListener filterByCompleted = new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
filterByComplete();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate,
|
||||||
|
filterByCompleted);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user