@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="src" path=""/>
|
||||||
|
<classpathentry kind="output" path=""/>
|
||||||
|
</classpath>
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
/Week2.class
|
||||||
|
/Week4.class
|
||||||
|
/Week4Bonus.class
|
||||||
|
/Task.class
|
||||||
|
/Week9$1.class
|
||||||
|
/Week9$2.class
|
||||||
|
/Week9$3.class
|
||||||
|
/Week9.class
|
||||||
|
/Week9Gui.class
|
||||||
|
/Task$CompareByTitle.class
|
||||||
|
/Week9$4.class
|
||||||
|
/Task$SortByTitleComparator.class
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>club-challenges</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1698871122678</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
// Represents a Task, which can be written and loaded to/from a file and are comparable to each other.
|
// Represents a Task, which can be written and loaded to/from a file and are comparable to each other.
|
||||||
public class Task {
|
public class Task {
|
||||||
private String title;
|
private String title;
|
||||||
@@ -5,6 +9,8 @@ public class Task {
|
|||||||
private String dueDate;
|
private String dueDate;
|
||||||
private boolean completed;
|
private boolean completed;
|
||||||
|
|
||||||
|
public static final Comparator<Task> BY_TITLE = new SortByTitleComparator();
|
||||||
|
|
||||||
// Constructor to create a new Task object.
|
// Constructor to create a new Task object.
|
||||||
public Task(String title, String description, String dueDate) {
|
public Task(String title, String description, String dueDate) {
|
||||||
this.title = title;
|
this.title = title;
|
||||||
@@ -38,11 +44,41 @@ public class Task {
|
|||||||
|
|
||||||
// Return if a task is complete or not.
|
// Return if a task is complete or not.
|
||||||
public void toggleComplete() {
|
public void toggleComplete() {
|
||||||
if(completed) completed = false;
|
if (completed)
|
||||||
else completed = true;
|
completed = false;
|
||||||
|
else
|
||||||
|
completed = true;
|
||||||
|
|
||||||
System.out.println("task status toggled");
|
System.out.println("task status toggled");
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement Comparable or Comparator.
|
// TODO: Implement Comparable or Comparator.
|
||||||
|
private static class SortByTitleComparator implements Comparator<Task> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compare(Task t1, Task t2) {
|
||||||
|
String[] tokens1 = t1.title.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
|
||||||
|
String[] tokens2 = t2.title.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
|
||||||
|
|
||||||
|
int length = Math.min(tokens1.length, tokens2.length);
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
if (Character.isDigit(tokens1[i].charAt(0)) && Character.isDigit(tokens2[i].charAt(0))) {
|
||||||
|
int intCompare = Integer.compare(Integer.parseInt(tokens1[i]), Integer.parseInt(tokens2[i]));
|
||||||
|
|
||||||
|
if (intCompare != 0)
|
||||||
|
return intCompare;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
int stringCompare = tokens1[i].compareTo(tokens2[i]);
|
||||||
|
|
||||||
|
if (stringCompare != 0)
|
||||||
|
return stringCompare;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Integer.compare(tokens1.length, tokens2.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-152
@@ -1,8 +1,5 @@
|
|||||||
package week9;
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
import java.awt.event.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -10,6 +7,10 @@ import java.io.FileNotFoundException;
|
|||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
public class Week9 {
|
public class Week9 {
|
||||||
private static ArrayList<Task> tasks;
|
private static ArrayList<Task> tasks;
|
||||||
@@ -19,7 +20,7 @@ public class Week9 {
|
|||||||
// 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
|
Collections.sort(tasks, Task.BY_TITLE);
|
||||||
gui.update(tasks);
|
gui.update(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,6 +38,15 @@ public class Week9 {
|
|||||||
gui.update(tasks);
|
gui.update(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// addBtn Method
|
||||||
|
private static void addBtn() {
|
||||||
|
String[] strs = new String[3];
|
||||||
|
strs[0] = JOptionPane.showInputDialog("Please Enter A Title:");
|
||||||
|
strs[1] = JOptionPane.showInputDialog("Please Enter A Discription:");
|
||||||
|
strs[2] = JOptionPane.showInputDialog("Please Enter A Due Date:");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void parse(File f) {
|
private void parse(File f) {
|
||||||
try (BufferedReader myReader = new BufferedReader(new FileReader(f))) {
|
try (BufferedReader myReader = new BufferedReader(new FileReader(f))) {
|
||||||
String line;
|
String line;
|
||||||
@@ -71,8 +81,7 @@ public class Week9 {
|
|||||||
// 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);
|
||||||
try (BufferedWriter myWriter = new BufferedWriter(
|
try (BufferedWriter myWriter = new BufferedWriter(new FileWriter(filepath))) {
|
||||||
new FileWriter(filepath))) {
|
|
||||||
myWriter.flush();
|
myWriter.flush();
|
||||||
String line;
|
String line;
|
||||||
|
|
||||||
@@ -84,8 +93,7 @@ public class Week9 {
|
|||||||
desc = task.getDescription();
|
desc = task.getDescription();
|
||||||
date = task.getDueDate();
|
date = task.getDueDate();
|
||||||
completed = task.isCompleted();
|
completed = task.isCompleted();
|
||||||
line = String.format("%s,%s,%s,%b", title, desc, date,
|
line = String.format("%s,%s,%s,%b", title, desc, date, completed);
|
||||||
completed);
|
|
||||||
|
|
||||||
myWriter.append(line);
|
myWriter.append(line);
|
||||||
myWriter.newLine();
|
myWriter.newLine();
|
||||||
@@ -108,8 +116,8 @@ public class Week9 {
|
|||||||
testData();
|
testData();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Identify the operating system of the user, and obtain the path to the
|
// Identify the operating system of the user, and obtain the path to the "tasks"
|
||||||
// "tasks" directory.
|
// directory.
|
||||||
private String getNotesDirectoryPath() {
|
private String getNotesDirectoryPath() {
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
|
||||||
@@ -133,8 +141,7 @@ 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.");
|
||||||
@@ -144,146 +151,11 @@ public class Week9 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
=======
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Week9 {
|
|
||||||
private static ArrayList<Task> tasks;
|
|
||||||
private static String filepath;
|
|
||||||
private static Week9Gui gui;
|
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
|
||||||
private static void sortByTitle() {
|
|
||||||
System.out.println("Sort By Title clicked!");
|
|
||||||
// TODO
|
|
||||||
gui.update(tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
|
||||||
private static void sortByDueDate() {
|
|
||||||
System.out.println("Sort By Due Date clicked!");
|
|
||||||
// TODO
|
|
||||||
gui.update(tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE: For sorts, the Task class needs something implemented.
|
|
||||||
private static void filterByComplete() {
|
|
||||||
System.out.println("Filter By Complete clicked!");
|
|
||||||
// TODO
|
|
||||||
gui.update(tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
//addBtn Method
|
|
||||||
private static void addBtn() {
|
|
||||||
String[] strs = new String[3];
|
|
||||||
strs[0] = JOptionPane.showInputDialog("Please Enter A Title:");
|
|
||||||
strs[1] = JOptionPane.showInputDialog("Please Enter A Discription:");
|
|
||||||
strs[2] = JOptionPane.showInputDialog("Please Enter A Due Date:");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parse(File f) throws IOException {
|
|
||||||
// TODO: Need to open the file and get each line.
|
|
||||||
// TODO: Parse the format ("TITLE, DESCRIPTION, DUE DATE, COMPLETED"),
|
|
||||||
// TODO: Create a new Task object and add it to the ArrayList.
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Writes each task to the file after a change.
|
|
||||||
private static void saveTasks() {
|
|
||||||
System.out.println("writing tasks to " + filepath);
|
|
||||||
// TODO: Write each Task stored in the ArrayList to the file.
|
|
||||||
}
|
|
||||||
|
|
||||||
///////////////////////// END OF TODO ////////////////////////////
|
|
||||||
|
|
||||||
// Initialize tasks and GUI, populate with test data (if applicable).
|
|
||||||
private void init() {
|
|
||||||
filepath = getNotesDirectoryPath();
|
|
||||||
initTasks();
|
|
||||||
testData();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Identify the operating system of the user, and obtain the path to the "tasks" directory.
|
|
||||||
private String getNotesDirectoryPath() {
|
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
|
||||||
|
|
||||||
String userHome = System.getProperty("user.home");
|
|
||||||
String tasksDir = "tasks";
|
|
||||||
|
|
||||||
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);
|
|
||||||
try {
|
|
||||||
parse(f);
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
} 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.
|
// 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++)
|
tasks.add(new Task("Title 1000", "Description ", "Due Date "));
|
||||||
|
tasks.add(new Task("100 Title", "Description ", "Due Date "));
|
||||||
|
for (int i = 20; i > 0; i--)
|
||||||
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
|
tasks.add(new Task("Title " + i, "Description " + i, "Due Date " + i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user