week 2 challenge
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
/Task$SortByTitleComparator.class
|
||||
/Task.class
|
||||
/Week9.class
|
||||
/Week9Gui.class
|
||||
/Week9$1.class
|
||||
/Week9$2.class
|
||||
/Week9$3.class
|
||||
/Week9$4.class
|
||||
@@ -0,0 +1,85 @@
|
||||
package fall23.Week9;
|
||||
|
||||
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.
|
||||
public class Task {
|
||||
private String title;
|
||||
private String description;
|
||||
private String dueDate;
|
||||
private boolean completed;
|
||||
|
||||
public static final Comparator<Task> BY_TITLE = new SortByTitleComparator();
|
||||
|
||||
// Constructor to create a new Task object.
|
||||
public Task(String title, String description, String dueDate) {
|
||||
this.title = title;
|
||||
this.description = description;
|
||||
this.dueDate = dueDate;
|
||||
this.completed = false;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public String getDueDate() {
|
||||
return dueDate;
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
// Edit a task by providing new information.
|
||||
public void editTask(String newTitle, String newDescription, String newDueDate) {
|
||||
this.title = newTitle;
|
||||
this.description = newDescription;
|
||||
this.dueDate = newDueDate;
|
||||
}
|
||||
|
||||
// Return if a task is complete or not.
|
||||
public void toggleComplete() {
|
||||
if (completed)
|
||||
completed = false;
|
||||
else
|
||||
completed = true;
|
||||
|
||||
System.out.println("task status toggled");
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package fall23.Week9;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
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!");
|
||||
Collections.sort(tasks, Task.BY_TITLE);
|
||||
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) {
|
||||
try (BufferedReader myReader = new BufferedReader(new FileReader(f))) {
|
||||
String line;
|
||||
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.
|
||||
private static void saveTasks() {
|
||||
System.out.println("writing tasks to " + filepath);
|
||||
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 ////////////////////////////
|
||||
|
||||
// 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);
|
||||
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() {
|
||||
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));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
ActionListener addBtn = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
addBtn();
|
||||
}
|
||||
};
|
||||
|
||||
gui = new Week9Gui(tasks, sortByTitle, sortByDueDate, filterByCompleted, addBtn);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package fall23.Week9;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.UIManager;
|
||||
|
||||
import java.awt.event.*;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
public class Week9Gui {
|
||||
private JFrame frame = new JFrame("SLCC Programming Club Challenge - Task Manager");
|
||||
|
||||
private ActionListener titleSort;
|
||||
private ActionListener dueDateSort;
|
||||
private ActionListener completeFilter;
|
||||
private ActionListener addBtn;
|
||||
|
||||
public Week9Gui(ArrayList<Task> tasks, ActionListener titleSort, ActionListener dueDateSort, ActionListener completeFilter, ActionListener addBtn) {
|
||||
this.titleSort = titleSort;
|
||||
this.dueDateSort = dueDateSort;
|
||||
this.completeFilter = completeFilter;
|
||||
this.addBtn = addBtn;
|
||||
initGui(tasks);
|
||||
}
|
||||
|
||||
// Setup the basic GUI layout.
|
||||
private void initGui(ArrayList<Task> tasks) {
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frame.getContentPane().setLayout(new BorderLayout());
|
||||
frame.setLocationRelativeTo(null);
|
||||
frame.setPreferredSize(new Dimension(600, 450));
|
||||
frame.setSize(frame.getPreferredSize());
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error with cross platform look & feel.");
|
||||
}
|
||||
|
||||
frame.getContentPane().add(buttonsView(), BorderLayout.PAGE_END);
|
||||
frame.getContentPane().add(view(tasks));
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
// Displays sort and filter buttons
|
||||
private JPanel buttonsView() {
|
||||
JButton sortByTitle = new JButton("Title");
|
||||
sortByTitle.addActionListener(titleSort);
|
||||
|
||||
JButton sortByDueDate = new JButton("Due Date");
|
||||
sortByDueDate.addActionListener(dueDateSort);
|
||||
|
||||
JButton filterByComplete = new JButton("Complete");
|
||||
filterByComplete.addActionListener(completeFilter);
|
||||
|
||||
JButton addBtn = new JButton("Add");
|
||||
addBtn.addActionListener(this.addBtn);
|
||||
|
||||
|
||||
|
||||
JPanel p = new JPanel(new GridLayout(0, 4));
|
||||
p.add(new JLabel("Sort by:"));
|
||||
p.add(new JLabel("Sort by:"));
|
||||
p.add(new JLabel("Filter by:"));
|
||||
p.add(new JLabel("Add to:"));
|
||||
p.add(sortByTitle);
|
||||
p.add(sortByDueDate);
|
||||
p.add(filterByComplete);
|
||||
p.add(addBtn);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
// Displays tasks and their completion status.
|
||||
private JPanel view(ArrayList<Task> tasks) {
|
||||
JPanel v = new JPanel(new GridLayout(tasks.size(), 4));
|
||||
|
||||
for(Task t : tasks) {
|
||||
v.add(new JLabel(t.getTitle()));
|
||||
v.add(new JLabel(t.getDescription()));
|
||||
v.add(new JLabel(t.getDueDate()));
|
||||
JCheckBox toggleComplete = new JCheckBox("Completed", t.isCompleted());
|
||||
toggleComplete.addActionListener(
|
||||
e -> { t.toggleComplete(); }
|
||||
);
|
||||
v.add(toggleComplete);
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
// Clear everything and refresh the JFrame to update information.
|
||||
public void update(ArrayList<Task> tasks) {
|
||||
frame.getContentPane().removeAll();
|
||||
frame.getContentPane().add(buttonsView(), BorderLayout.PAGE_END);
|
||||
frame.getContentPane().add(view(tasks));
|
||||
frame.repaint();
|
||||
frame.revalidate();
|
||||
frame.pack();
|
||||
System.out.println("Updated UI");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user