Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/Kratos.java
#	src/main/java/KratosException.java
  • Loading branch information
Mahesh1772 committed Feb 23, 2024
2 parents c457b5c + f62e08a commit 3b3a2f2
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
1 change: 1 addition & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T|helllo|1
5 changes: 5 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public Deadline(String description, String by) {
public String toString() {
return "[D]" +"[" + this.getStatusIcon()+ "] " + super.toString() + " (by: " + by + ")";
}

@Override
public String toFileString() {
return "D|" + super.toFileString() + "|" + by; // Prefix with "D" to indicate Deadline
}
}

5 changes: 5 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public Event(String description, String from, String to) {
public String toString() {
return "[E]" +"[" + this.getStatusIcon()+ "] " + super.toString() + " (from: " + from + " to: " + to +")";
}

@Override
public String toFileString() {
return "E|" + super.toFileString() + "|" + from + "|" + to; // Prefix with "E" to indicate Event
}
}
64 changes: 60 additions & 4 deletions src/main/java/Kratos.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
import java.util.ArrayList;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;

public class Kratos {
public final static int MAX_TASKS = 100;
public static final String LINE = "----------------------------------------------------------------";
//static Task[] tasksList = new Task[MAX_TASKS];
static ArrayList<Task> tasksList = new ArrayList<>();
private static final String FILE_PATH = "./data/tasks.txt";

// Method to save tasks to a file
public static void saveTasksToFile() {
try {
File file = new File(FILE_PATH);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); // Create directories if they don't exist
}
file.createNewFile(); // Create the file if it doesn't exist
try (FileWriter writer = new FileWriter(file)) {
for (int i = 0; i < tasksList.size(); i++) {
writer.write(tasksList.get(i).toFileString() + "\n");
}
}
} catch (IOException e) {
KratosException.handleException(e, "Error saving tasks to file: " + e.getMessage());
}
}

// Method to load tasks from a file
// Method to load tasks from a file
public static void loadTasksFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
String line;
while ((line = reader.readLine()) != null) {
Task task = Task.fromString(line);
if (task != null) {
tasksList.add(task);
}
}
} catch (FileNotFoundException e) {
// Handle the case where the file doesn't exist
KratosException.handleException(e, "File not found. Creating a new file...");
File file = new File(FILE_PATH);
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs(); // Create directories if they don't exist
}
file.createNewFile();
} catch (IOException ioException) {
KratosException.handleException(ioException, "Error creating a new file: " + ioException.getMessage());
}
} catch (IOException e) {
KratosException.handleException(e, "Error loading tasks from file: " + e.getMessage());
}
}



//public static int count = 0;
Expand Down Expand Up @@ -99,10 +152,12 @@ public static void displayTasks(int count) {
// Main method
public static void main(String[] args) {
greet();
loadTasksFromFile(); // Load tasks from file when the program boots up
String userInput;
Scanner in = new Scanner(System.in);
try {
while (true) {
String userInput = in.nextLine().trim();
userInput = in.nextLine().trim();
switch (userInput) {
case "bye":
end();
Expand All @@ -117,6 +172,7 @@ public static void main(String[] args) {
}
} finally {
in.close();
saveTasksToFile();
}
}

Expand Down Expand Up @@ -154,9 +210,9 @@ private static void deleteEvent(String userInput) {
System.out.println(LINE);
System.out.println(displayString);
System.out.printf(" %s%n", tasksList.get(taskNumber).toString());
tasksList.remove(taskNumber);
System.out.println(tasksList.size() + " tasks linger, shadows yet unvanquished. How will you face them?");
System.out.println(LINE);
tasksList.remove(taskNumber);
}

private static void addEvent(String userInput) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/KratosException.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;

public class KratosException extends Exception {
public static void handleException(Exception e, String command) {
if (e instanceof IllegalArgumentException) {
Expand All @@ -6,6 +9,10 @@ public static void handleException(Exception e, String command) {
blankAfterTask(command);
} else if (e instanceof IndexOutOfBoundsException) {
taskOutOfBounds();
} else if (e instanceof IOException) {
System.out.println(command);
} else if (e instanceof FileNotFoundException) {
System.out.println(command);
} else {
otherIncurredException();
}
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,52 @@ public void unmarkTask() {
public String toString() {
return description;
}

/**
* Converts the task to a string representation that can be written to a file.
*
* @return A string representation of the task for file storage.
*/
public String toFileString() {
// Implement how you want to represent the task as a string for file storage
// For example, you can concatenate the description and status with a delimiter
return description + "|" + (isDone ? "1" : "0"); // Example format: "description|status"
}

/**
* Parses a string from a file into a Task object.
*
* @param fileString The string read from the file representing a task.
* @return The Task object parsed from the string.
*/
public static Task fromString(String fileString) {
String[] parts = fileString.split("\\|");
String taskType = parts[0]; // Extract task type
String description = parts[1];
boolean isDone = parts[2].equals("1"); // Extract status

Task task;
switch (taskType) {
case "D":
task = new Deadline(description, parts[3]); // Create Deadline task
break;
case "E":
task = new Event(description, parts[3], parts[4]); // Create Event task
break;
case "T":
task = new Todo(description); // Create Todo task
break;
default:
// Handle unknown task type
task = null;
break;
}

if (task != null && isDone) {
task.markTask(); // Mark task as done if status is '1'
}

return task;
}

}
5 changes: 5 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ public Todo(String description) {
public String toString() {
return "[T]" +"[" + this.getStatusIcon()+ "] " + super.toString();
}

@Override
public String toFileString() {
return "T|" + super.toFileString(); // Prefix with "T" to indicate Todo
}
}

0 comments on commit 3b3a2f2

Please sign in to comment.