week 2 challenge
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/App.class
|
||||
@@ -0,0 +1,158 @@
|
||||
package fall23.Week11;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* The following iterative sequence is defined for the set of positive integers:
|
||||
* n -> n/2 (if n is even)
|
||||
* n -> 3n + 1 (if n is odd)
|
||||
*
|
||||
* Using the rule above and starting with 13, we generate the following sequence:
|
||||
* 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
*
|
||||
* This sequence contains 10 terms. Although it hasn't been proven yet (see Collatz Problem),
|
||||
* it is thought that all starting numbers finish at 1.
|
||||
*
|
||||
* Which starting number, under 1 million, produces the longest chain?
|
||||
* NOTE: Once the chain starts, the terms may go above 1 million.
|
||||
*
|
||||
* CREDIT: Problem 14 of Project Euler
|
||||
*
|
||||
* @return starting number of the longest Collatz Sequence
|
||||
*/
|
||||
private static int collatzSequence() {
|
||||
int n = 1;
|
||||
int maxChain = 0;
|
||||
int correctNum = 0;
|
||||
|
||||
while (n < 1_000_000) {
|
||||
int chainLength = collatz(n, 0);
|
||||
|
||||
if (chainLength > maxChain) {
|
||||
maxChain = chainLength;
|
||||
correctNum = n;
|
||||
}
|
||||
|
||||
n--;
|
||||
}
|
||||
|
||||
System.out.println(correctNum);
|
||||
return correctNum;
|
||||
}
|
||||
|
||||
private static int collatz(int n, int chainLength) {
|
||||
if(n == 1) return ++chainLength; // Base case and final link.
|
||||
|
||||
if(n % 2 == 0) n /= 2;
|
||||
else n = (3 * n) + 1;
|
||||
|
||||
++chainLength;
|
||||
|
||||
return collatz(n, chainLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Background:
|
||||
* January 1 1900 was a Monday.
|
||||
* April, June, September, and November have 30 days.
|
||||
* January, March, May, July, August, October, and December have 31 days.
|
||||
* February has 28 days and on leap years 29.
|
||||
* A leap year occurs on any year evenly divisible by 4, but not on a century unless
|
||||
* it is divisble by 400.
|
||||
*
|
||||
* How many Sundays fell on the first of the month during the 20th century?
|
||||
* (January 1 1901 to December 31 2000).
|
||||
*
|
||||
* CREDIT: Problem 19 of Project Euler
|
||||
*
|
||||
* @return the number of Sundays
|
||||
*/
|
||||
private static int countingSundays() {
|
||||
return 1; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Using names.txt, a text file containing more than five-thousand first names,
|
||||
* begin by sorting it in alphabetical order. Then work out the alphabetical value
|
||||
* of each name, multiply this value by its alphabetical position in the list to obtain
|
||||
* a name score.
|
||||
*
|
||||
* For example, when the list is sorted into alphabetical order, COLIN, which is worth
|
||||
* 3 + 15 + 12 + 9 + 14 = 53
|
||||
* is the 938th name in the list. So COLIN would have a score of
|
||||
* 938 x 53 = 49714
|
||||
*
|
||||
* What is the total of all the name scores in the file?
|
||||
*
|
||||
* CREDIT: Problem 22 of Project Euler
|
||||
*
|
||||
* @return the total of the name scores.
|
||||
*/
|
||||
private static int nameScores() {
|
||||
ArrayList<String> names = readNames();
|
||||
int sum = 0;
|
||||
|
||||
Collections.sort(names);
|
||||
|
||||
int counter = 1;
|
||||
for (String name : names) {
|
||||
int nameScore = 0;
|
||||
char[] n = name.toCharArray();
|
||||
for (char c : n) {
|
||||
nameScore += (c - 64);
|
||||
}
|
||||
|
||||
nameScore *= counter;
|
||||
sum+= nameScore;
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
System.out.println("sum of name scores: " + sum);
|
||||
return sum;
|
||||
}
|
||||
|
||||
private static ArrayList<String> readNames() {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
|
||||
File f = new File("names.txt");
|
||||
|
||||
try (Scanner scan = new Scanner(f)) {
|
||||
String str = scan.nextLine();
|
||||
|
||||
String[] strings = str.split(",");
|
||||
|
||||
for (String s : strings)
|
||||
list.add(s.substring(1, s.length() - 1));
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error reading file.");
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static void printNames(ArrayList<String> names) {
|
||||
for (String name : names)
|
||||
System.out.println(name);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
//System.out.println(collatz(13, 0));
|
||||
if(collatzSequence() != 837799) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Collatz Sequence solution is correct!");
|
||||
|
||||
/*
|
||||
if(countingSundays() != 171) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Counting Sundays solution is correct!");
|
||||
|
||||
if(nameScores() != 871198282) System.out.println("Your solution is incorrect...");
|
||||
else System.out.println("Your Name Scores solution is correct!");
|
||||
*/
|
||||
}
|
||||
}
|
||||
+5163
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/zsh
|
||||
|
||||
javac *.java
|
||||
java App
|
||||
|
||||
rm *.class
|
||||
@@ -0,0 +1,4 @@
|
||||
/SNAFU$Input$SNAFUIterator.class
|
||||
/SNAFU$Input.class
|
||||
/SNAFU.class
|
||||
/SNAFUChallenge.class
|
||||
@@ -0,0 +1,128 @@
|
||||
1-201-=2
|
||||
210--1211001
|
||||
120
|
||||
1==1-=1=-1=-=1-==-
|
||||
1--
|
||||
1-1-2221=-
|
||||
1021=
|
||||
2-=-21==02
|
||||
1-
|
||||
1-222122-0==020
|
||||
1-20122---12=2112-2=
|
||||
221
|
||||
12=220-2-=1=
|
||||
21
|
||||
1-20
|
||||
1-0=-0-2---
|
||||
10-=221=-0102
|
||||
212-020-1=00
|
||||
201=020=-1=
|
||||
2=0=01=2==
|
||||
2=1=0=11-2===20--
|
||||
2-0-221
|
||||
1--10=--1-0-2
|
||||
100=-11222=1
|
||||
11-=1
|
||||
11=--=1012--02-
|
||||
121
|
||||
1=11=1
|
||||
1==2---002--11=-1-
|
||||
11=0==--0
|
||||
2==0110=2=1==
|
||||
221=-=-=-02
|
||||
12-10
|
||||
2=
|
||||
2-0-=2
|
||||
1=0-0=2
|
||||
10==-2210=-=202
|
||||
2=-21
|
||||
2=-=1220
|
||||
201=2==1
|
||||
1==1=00-1-=
|
||||
1200-00-00-21-=-=0
|
||||
1=0=-22-200=-2-=--
|
||||
1==-=--12-1112-22
|
||||
1=0=-00====102
|
||||
20222010-1
|
||||
1---
|
||||
20--==2-201-
|
||||
1=1
|
||||
120=02=-=1-0=1
|
||||
1=-210-1=1
|
||||
1110--2
|
||||
1102-1=2----201=-
|
||||
1=01=2-01
|
||||
12=2--0-1001
|
||||
12=1
|
||||
102
|
||||
2=1=-11
|
||||
12=001022
|
||||
2=1=1=-01==
|
||||
101---=1--
|
||||
10=00001-0122=0
|
||||
12=-=--11=112=0202
|
||||
1-122====012100
|
||||
212=020=1022-0
|
||||
10=22=2
|
||||
1===212-02=1
|
||||
2=-2===-01010-121
|
||||
122=10
|
||||
2-0-1=-0--1001=-2
|
||||
1121
|
||||
1-21--100=1=2=1=2-
|
||||
1==0
|
||||
1=-0=0-2=0-=1===0
|
||||
2-02-==
|
||||
20-=-2022===-222
|
||||
1=1100-1
|
||||
12=-1=-02-2-20-=1-=
|
||||
10=
|
||||
1=1-==2
|
||||
101-=01221-==-2-=-
|
||||
12-=111=2=11-=1212
|
||||
12==202=121
|
||||
22022=000-121001-
|
||||
12=2
|
||||
1==0==1--1
|
||||
222
|
||||
10
|
||||
12-0122=01-
|
||||
10-0=01120
|
||||
210-0===-011=-=12=
|
||||
1=-1=0=2-102--0
|
||||
10111000--0-===0
|
||||
1=0-=-2-
|
||||
2-==22100
|
||||
1-2=-2-1-020=-2-
|
||||
1=001-1=02=--2
|
||||
1==0-211-21=-
|
||||
1-02=2=--221=-012==
|
||||
1-011-221-0=
|
||||
10-20=
|
||||
1=-0--=
|
||||
10-=022101=1--22
|
||||
2-0-1202==-==-
|
||||
1=0-
|
||||
11=
|
||||
2200
|
||||
2-22-212==-0==2=
|
||||
1012
|
||||
1=2=12===2-=
|
||||
1==211-02-
|
||||
1-001==2101220
|
||||
2-001
|
||||
10-=2-=
|
||||
102110==21=12--02
|
||||
1-=1122212002=010
|
||||
1-10-201=221=021-
|
||||
2-00=2221
|
||||
2--1
|
||||
11=12=0
|
||||
2-21=-
|
||||
20--2-1=22220-0
|
||||
2=22=1-02=21
|
||||
10==10112=1
|
||||
1-2=0=0=02==
|
||||
2201=2
|
||||
10-=00121
|
||||
10-012-12
|
||||
@@ -0,0 +1,85 @@
|
||||
package fall23.Week13;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Provides code for the input and testing of the SNAFU Challenge. This
|
||||
* challenge and input is from Day 25, year 2022 from AdventOfCode.com.
|
||||
*/
|
||||
public class SNAFU {
|
||||
/**
|
||||
*
|
||||
* @return returns an Iterable form of the input.
|
||||
*/
|
||||
public static Iterable<String> iterableInput() {
|
||||
return new Input();
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes that implement Iterable tell Java that the class can be trusted to
|
||||
* have an Iterator. A very common and nice use is in for loops. See, Arrays and
|
||||
* a number of other datastructures implement Iterable, and you can stick any
|
||||
* Iterable class in the for-each loop.
|
||||
*/
|
||||
private static class Input implements Iterable<String> {
|
||||
/**
|
||||
* This is the only method required by Iterable.
|
||||
*/
|
||||
@Override
|
||||
public Iterator<String> iterator() {
|
||||
return new SNAFUIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the manual version of the for each loop. There are two methods
|
||||
* inside. Next() gives the next input (and iterates). HasNext() is called
|
||||
* before to check if Next() can be safely run. The for loop basically keeps
|
||||
* calling Next() while HasNext() is true. Next() is actually the input given to
|
||||
* the left half of the for each loop.
|
||||
*
|
||||
* In this case, this Iterator keeps going until the next line it has ready is
|
||||
* null.
|
||||
*/
|
||||
private class SNAFUIterator implements Iterator<String> {
|
||||
private final static String PATH = "Input.txt";
|
||||
|
||||
private BufferedReader reader;
|
||||
private String currLine;
|
||||
|
||||
private SNAFUIterator() {
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader(PATH));
|
||||
iterateToNextLine();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("File Not Found");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void iterateToNextLine() {
|
||||
try {
|
||||
currLine = reader.readLine();
|
||||
} catch (IOException e) {
|
||||
System.out.println("Error With Reader");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return (currLine != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String next() {
|
||||
String returnStr = currLine;
|
||||
iterateToNextLine();
|
||||
return returnStr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package fall23.Week13;
|
||||
|
||||
/**
|
||||
* Full Instructions and Explanation: https://adventofcode.com/2022/day/25
|
||||
*
|
||||
* Find the SNAFU sum of the SNAFUInput.txt file.
|
||||
*/
|
||||
public class SNAFUChallenge {
|
||||
public static void main(String[] args) {
|
||||
String expectedSnafu = "2-02===-21---2002==0";
|
||||
|
||||
long sum = 0;
|
||||
for (String lineOfSNAFU : SNAFU.iterableInput()) {
|
||||
System.out.println(lineOfSNAFU);
|
||||
sum += decode(lineOfSNAFU);
|
||||
}
|
||||
|
||||
System.out.println("=== RESULT ===");
|
||||
System.out.println("result : " + encode(sum));
|
||||
System.out.println("expected result : " + expectedSnafu);
|
||||
}
|
||||
|
||||
private static String encode(long num) {
|
||||
return ""; // TODO: Convert a long into SNAFU.
|
||||
}
|
||||
|
||||
/**
|
||||
* The easy part. Go right to left on the SNAFU string and add the number to the
|
||||
* sum. Translates from SNAFU base-5 to base-10.
|
||||
*
|
||||
* @param SNAFU The SNAFU base-5 number code to decode.
|
||||
* @return The base-10 result
|
||||
*/
|
||||
private static long decode(String SNAFU) {
|
||||
return 0l; // TODO: Convert a String of SNAFU into a long.
|
||||
}
|
||||
}
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/zsh
|
||||
|
||||
javac *.java
|
||||
java SNAFUChallenge
|
||||
|
||||
rm *.class
|
||||
@@ -0,0 +1 @@
|
||||
/Week2.class
|
||||
@@ -0,0 +1,50 @@
|
||||
package fall23.Week2;
|
||||
|
||||
/**
|
||||
* Sum of Multiples of 3 or 5.
|
||||
*
|
||||
* In this week's club challenge, find the sum of each multiple of either 3 or 5, with the condition that the multiple is less than 1000.
|
||||
* eg. For multiples of 3 or 5 below 10, the answer would be the 3 + 5 + 6 + 9 = 23.
|
||||
*
|
||||
* Credit: Project Euler Problem 1.
|
||||
*/
|
||||
public class Week2 {
|
||||
|
||||
/**
|
||||
* HINT: Solution 1 uses 1000 iterations in 1 loop.
|
||||
*/
|
||||
public static int problem1() {
|
||||
int sum = 0;
|
||||
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
if(i % 5 == 0 || i % 3 == 0) sum += i;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* BONUS CHALLENGE: Find a more efficient solution for problem 1 (ie. a solution that uses less than 1000 iterations).
|
||||
*/
|
||||
public static int problem2() {
|
||||
int sum = 0;
|
||||
for(int i = 5; i < 1000; i+=5)
|
||||
sum += i;
|
||||
|
||||
for(int i = 3; i < 1000; i+=3)
|
||||
if(i % 5 != 0) sum += i;
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test driver should not be edited!
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int sum1 = problem1();
|
||||
int sum2 = problem2();
|
||||
|
||||
System.out.println(sum1 == 233168 ? "Problem 1 is correct!" : "Problem 1 (" + sum1 + ") is not correct...");
|
||||
System.out.println(sum2 == 233168 ? "Problem 2 is correct!" : "Problem 2 (" + sum2 + ") is not correct...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
/Week4.class
|
||||
/Week4Bonus.class
|
||||
@@ -0,0 +1,53 @@
|
||||
package fall23.Week4;
|
||||
|
||||
/**
|
||||
* In this week's club bonus challenges, you will be tasked with solving problems relating to the Fibonacci Sequence.
|
||||
*
|
||||
* For those unfamiliar, the Fibonacci Seuqence is generated by adding the prior two terms. Starting with 1 & 2, The
|
||||
* first 10 terms are 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
|
||||
*/
|
||||
public class Week4 {
|
||||
|
||||
/**
|
||||
* Only consider terms of the Fibonacci Sequence below 4 million. Find the sum of only the even terms.
|
||||
* eg. The first 5 terms counted would be 2, 8, 34, 144, 610, . . ., and the sum of those terms is 366.
|
||||
*
|
||||
* Credit: Project Euler Problem 2.
|
||||
* HINT: If your algorithm works for terms below 200, it'll work for the terms below any value.
|
||||
*/
|
||||
public static int problem1() {
|
||||
return -1; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an algorithm that will calculate the nth EVEN term of the Fibonacci Sequence.
|
||||
*
|
||||
* HINT: This only requires a minor modification to your solution for Problem1.
|
||||
*/
|
||||
public static int problem2(int n) {
|
||||
return -1; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Write an algorithm that finds the first EVEN term of the Fibonacci Sequence that has n digits.
|
||||
* eg. if n = 3, value = 144.
|
||||
*
|
||||
* Then, find the average of all of the EVEN terms up to this value.
|
||||
*/
|
||||
public static double problem3(int n) {
|
||||
return -1.0; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Test driver should not be edited!
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
int sum1 = problem1();
|
||||
int sum2 = problem2(10);
|
||||
double avg3 = problem3(5);
|
||||
|
||||
System.out.println(sum1 == 4613732 ? "Problem 1 is correct!" : "Problem 1 (" + sum1 + ") is not correct...");
|
||||
System.out.println(sum2 == 196418 ? "Problem 2 is correct!" : "Problem 2 (" + sum2 + ") is not correct...");
|
||||
System.out.println(avg3 == 2046.0 ? "Problem 3 is correct!" : "Problem 3 (" + avg3 + ") is not correct...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package fall23.Week4;
|
||||
|
||||
/**
|
||||
* In this week's club bonus challenge, you will be tasked with solving grid traversal problem.
|
||||
*/
|
||||
public class Week4Bonus {
|
||||
|
||||
/**
|
||||
* In a 2x2 grid, there are exactly 6 routes from the top-left corner to the bottom-right corner.
|
||||
* Find an algorithm to find the number of routes in a 20x20 grid.
|
||||
*
|
||||
* Credit: Project Euler Problem 15.
|
||||
*
|
||||
* HINT: Recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion...
|
||||
*/
|
||||
public static long problem1() {
|
||||
return -1; // TODO
|
||||
}
|
||||
|
||||
/**
|
||||
* Test driver should not be edited!
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
long ans1 = problem1();
|
||||
|
||||
System.out.println(ans1 == 137846528820L ? "Problem 1 is correct!" : "Problem 1 (" + ans1 + ") is not correct...");
|
||||
}
|
||||
}
|
||||
@@ -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