From 47c89fa889a52f044d1bf1b4435732e4ad91c9d7 Mon Sep 17 00:00:00 2001 From: rohit Date: Thu, 5 Oct 2023 16:57:25 +0800 Subject: [PATCH] 2 functions added for saving tasks to file and transferring tasks from file to array --- docs/dukey.txt | 1 + docs/psuedo.txt | 69 +++++++++++++++++ src/main/java/Dukey.java | 115 +++++++++++++++++++++++++++-- src/main/java/META-INF/MANIFEST.MF | 3 + src/main/java/Tasks/Task.java | 6 ++ 5 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 docs/dukey.txt create mode 100644 docs/psuedo.txt create mode 100644 src/main/java/META-INF/MANIFEST.MF diff --git a/docs/dukey.txt b/docs/dukey.txt new file mode 100644 index 000000000..38507717c --- /dev/null +++ b/docs/dukey.txt @@ -0,0 +1 @@ +1.[T][ ] kill akshay diff --git a/docs/psuedo.txt b/docs/psuedo.txt new file mode 100644 index 000000000..a1451c4a6 --- /dev/null +++ b/docs/psuedo.txt @@ -0,0 +1,69 @@ +[E][ ] rohit attend cs2113 lecture Friday 22 Sep 2023 (from: 4pm to: 6pm) + +how to merge files? + + +save function: +assume task list is empty: + 1. every new task is added to text file + 2. text file is hardcoded in + subtasks: + 1. create file, done + 2. create filepath, done + 3. only want valid inputs into dukey, so it just copies the task array onto dukey. OKAY + -> a function that after each iteration of the loop, updates dukey by what is inside the task array + task array, array of items that are of the class 'tasks', so write a function that goes through each + item in the array and then prints the task into dukey.txt, where should this function be? OKAY. + + **Ok now, I change fw.write to append, which means i only add the last element of the task array.** + + +assume opening a pre-existing task list: + 1. every file in the task list should be added to the task arrays. + yep, how tf do i do this. -> make a function fileToTaskArray + iterate through each line of text in file, add the subclass and its properties accordingly into taskarray + write function to print out the file first, then focus on the taskarray, WORKS! + add the subclass and its properties accordingly into taskarray -> for each line, use the same function, + dont care that they have outputs rn, + 2. the remaining function should work as normal +assume no files to open: + 1. handle file doesnt exist case -> how? + create new file, add the link to the dukey + 2. handle folder doesnt exist case -> how? + +set done, and mark is not working WORKS +narrow the issue, when a perfect line from the file is input into the task file not solved but not important + +ok so what is the flow now. +1. run program +2. open file (if file exists) +3. copy the shit in the file into array +4. for every new insertion into the array, append it into the file +5. have to modify this for deadline and event + + +bugs: +- something wrong with the indexing +- + +ok deadline +1. do i need to fix the description now?. no +2. just do the file tasks first +task arry to file function work for all :)) +file to task array +parse deadline + +goals: +1. save should work +2. implement the fkin make new file if file doesnt exist + +killminjun (by: 9pm on tuesday) -> parse into description and due +use split -> split by: (end of this index) until ')' use indexat to find this +make a duplicate +use split -> (by: (start of this index) + +3.[D][ ] killminjun (by: 9) + + +4.[E][ ] killmj (from: 5 to: 9pm ) +substring from index 8 to (from \ No newline at end of file diff --git a/src/main/java/Dukey.java b/src/main/java/Dukey.java index 5cb8910d5..a2881c3bb 100644 --- a/src/main/java/Dukey.java +++ b/src/main/java/Dukey.java @@ -1,17 +1,25 @@ - - import Tasks.*; import dukey.*; import java.util.ArrayList; import java.util.Scanner; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + public class Dukey { private static void addTodo(String line, ArrayList tasks) { String[] words = line.split(" "); - String description = null; + String description = ""; + String filePath = "./docs/dukey.txt"; try { - description = words[1]; + for (int i = 1; i < words.length; i++) { + description += words[i] + " "; + } tasks.add(new Todo(description)); tasks.get(tasks.size() - 1).printNewTask(); } catch(IndexOutOfBoundsException e) { @@ -41,6 +49,7 @@ private static void addEvent(String line, ArrayList tasks) { private static void addDeadline(String line, ArrayList tasks) { String[] words = line.split("/by"); String[] words2 = line.split(" "); + // int indexOfDescriptionEnd = line.indexOf("/by"); try { String description = words2[1]; String by = words[1]; @@ -74,12 +83,103 @@ public static void printTaskList(ArrayList tasks) { } } - public static void main(String[] args) { + private static void printFileContents(String filePath) throws FileNotFoundException { + File f = new File(filePath); // create a File for the given file path + Scanner s = new Scanner(f); // create a Scanner using the File as the source + while (s.hasNext()) { + System.out.println(s.nextLine()); + } + } + + private static void writeToFile(String filePath, String textToAdd) throws IOException { + FileWriter fw = new FileWriter(filePath, true); + fw.write(textToAdd); + fw.close(); + } + + // appends the last index of the task array into the file + private static void taskArrayToFile(String filePath, ArrayList tasks) throws IOException { + FileWriter fw = new FileWriter(filePath, true); + int index = tasks.size() - 1; + Task lastTask = tasks.get(tasks.size() - 1); + fw.write((index + 1) + "." + lastTask); + fw.write("\n"); + fw.close(); + } + private static void fileToTaskArray(String filePath, ArrayList tasks) throws IOException { + File f = new File(filePath); // create a File for the given file path + Scanner list = new Scanner(f); // create a Scanner using the File as the source + while (list.hasNext()) { + String line = list.nextLine(); + char type = line.charAt(3); + switch (type) { + case 'T': + addTodoFromFile(line, tasks); + break; + case 'D': + addDeadlineFromFile(line, tasks); + break; + case 'E': + addEventFromFile(line, tasks); + break; + default: + break; + } + } + } + + private static void addTodoFromFile(String line, ArrayList tasks) throws IOException { + Task element = new Todo(line.substring(7)); + if (line.charAt(6) == 'X') { + element.setDone(); + } + tasks.add(element); + } + + private static void addDeadlineFromFile(String line, ArrayList tasks) throws IOException { + int endOfDescriptionIndex = line.indexOf("(by"); + String description = line.substring(8, endOfDescriptionIndex); + int startOfDueIndex = line.indexOf(':'); + int endOfDueIndex = line.indexOf(')'); + String due = line.substring(startOfDueIndex + 1, endOfDueIndex); + Task element = new Deadline(description, due); + tasks.add(element); + } + + private static void addEventFromFile(String line, ArrayList tasks) throws IOException { + int endOfDescriptionIndex = line.indexOf("(from:"); + String description = line.substring(8, endOfDescriptionIndex); + int startOfFromIndex = endOfDescriptionIndex + 7; + int endOfFromIndex = line.indexOf("to:"); + String from = line.substring(startOfFromIndex, endOfFromIndex); + String to = line.substring(endOfFromIndex + 4, line.indexOf(')')); + Task element = new Event(from, to, description); + tasks.add(element); + } + + public static void main(String[] args) throws IOException { System.out.println("Hey! I'm Dukey, your virtual assistant!\nWhat can I do for you?\n"); Scanner in = new Scanner(System.in); String line; ArrayList tasks = new ArrayList<>(); - + String file2 = "./docs/dukey.txt"; + File file = new File(file2); + // Check if the file or directory exists, and create it if it doesn't + if (!file.exists()) { + try { + if (file.getParentFile() != null && !file.getParentFile().exists()) { + file.getParentFile().mkdirs(); + } + file.createNewFile(); // Create the file + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } + } + try { + fileToTaskArray(file2, tasks); + } catch (IOException e) { + System.out.println("Something went wrong: " + e.getMessage()); + } while (true) { line = in.nextLine(); String[] words = line.split(" "); @@ -99,12 +199,15 @@ public static void main(String[] args) { break; case "deadline": addDeadline(line, tasks); + taskArrayToFile(file2, tasks); break; case "event": addEvent(line, tasks); + taskArrayToFile(file2, tasks); break; case "todo": addTodo(line, tasks); + taskArrayToFile(file2, tasks); break; default: if (line.trim().isEmpty()) { diff --git a/src/main/java/META-INF/MANIFEST.MF b/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 000000000..b6ea9ac99 --- /dev/null +++ b/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Dukey + diff --git a/src/main/java/Tasks/Task.java b/src/main/java/Tasks/Task.java index 4e93de6c8..251d1bb41 100644 --- a/src/main/java/Tasks/Task.java +++ b/src/main/java/Tasks/Task.java @@ -26,6 +26,10 @@ public void setDone() { this.isDone = true; System.out.println("Nice! I've marked this task as done:\n\t " + this); } + + public void setType(char letter) { + this.type = letter; + } // defines constructor for the task class public Task(String description) { this.description = description; @@ -56,4 +60,6 @@ public void printNewTask() { System.out.println(" task in the list."); } } + + }