Skip to content

Commit

Permalink
resolve merging issues with branch-Level-6
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitcube committed Oct 5, 2023
2 parents 22e6d32 + d9e74d0 commit a185c4a
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/dukey.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1.[T][ ] kill akshay
2.[T][ ] kill minjun
3.[D][ ] kill (by: 9)
69 changes: 69 additions & 0 deletions docs/psuedo.txt
Original file line number Diff line number Diff line change
@@ -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
93 changes: 90 additions & 3 deletions src/main/java/Dukey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@

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<Task> 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) {
Expand Down Expand Up @@ -54,6 +64,7 @@ private static void addEvent(String line, ArrayList<Task> tasks) {
private static void addDeadline(String line, ArrayList<Task> tasks) {
String[] words = line.split("/by");
String[] words2 = line.split(" ");
// int indexOfDescriptionEnd = line.indexOf("/by");
try {
String description = words2[1];
String by = words[1];
Expand Down Expand Up @@ -89,11 +100,84 @@ public static void printTaskList(ArrayList<Task> tasks) {
}


public static void main(String[] args) {
// appends the last index of the task array into the file
private static void taskArrayToFile(String filePath, ArrayList<Task> 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<Task> 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<Task> 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<Task> 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<Task> 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<Task> 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.createNewFile()) {
System.out.println("File created");
} else {
System.out.println("File already exists");
}
try {
fileToTaskArray(file2, tasks);
} catch (IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
}
while (true) {
line = in.nextLine();
String[] words = line.split(" ");
Expand All @@ -113,12 +197,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;
case "delete":
deleteTask(line, tasks);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Dukey

7 changes: 7 additions & 0 deletions src/main/java/Tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public void setDone() {
System.out.println("Nice! I've marked this task as done:\n\t " + this);
System.out.println("_____________________________________________________");
}

public void setType(char letter) {
this.type = letter;
}
// defines constructor for the task class
public Task(String description) {
this.description = description;
Expand Down Expand Up @@ -58,6 +62,7 @@ public void printNewTask() {
}
}

<<<<<<< HEAD
public void printDeleteTask() {
System.out.println("_____________________________________________________");
int num_tasks = getNumTasks() - 1;
Expand All @@ -69,5 +74,7 @@ public void printDeleteTask() {
}
System.out.println("_____________________________________________________");
}
=======
>>>>>>> branch-Level-7

}

0 comments on commit a185c4a

Please sign in to comment.