Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT

data/
12 changes: 12 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class Deadline extends Task {
protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

public String toString() {
return String.format("[D]%s (by: %s)", super.toString(), this.by);
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

14 changes: 14 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Event extends Task {
protected String from;
protected String to;

public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}

public String toString() {
return String.format("[E]%s (from: %s to: %s)", super.toString(), this.from, this.to);
}
}
46 changes: 46 additions & 0 deletions src/main/java/InputParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.util.HashMap;

public class InputParser {
public static final String COMMAND = "command";
public static final String ARGUMENT = "argument";

public static HashMap<String, String> parseCommands(String input) {
HashMap<String, String> commandArguments = new HashMap<>();
String[] splitInput = input.split(" ");

// check if input is empty
if (splitInput.length == 0) {
commandArguments.put(InputParser.COMMAND, "");
return commandArguments;
}

// set first element as command
commandArguments.put(InputParser.COMMAND, splitInput[0]);

String argumentDescription = InputParser.ARGUMENT;
StringBuilder argument = new StringBuilder();

// parse remaining input
for (int i = 1; i < splitInput.length; i++) {
String arg = splitInput[i];

if (arg.startsWith("/")) {
if (!argumentDescription.isEmpty()) {
commandArguments.put(argumentDescription, argument.toString().strip());
}

argumentDescription = arg;
argument.setLength(0);
} else {
argument.append(" ").append(arg);
}
}

// add last argument
if (!argument.isEmpty()) {
commandArguments.put(argumentDescription, argument.toString().strip());
}

return commandArguments;
}
}
58 changes: 58 additions & 0 deletions src/main/java/SaveTaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class SaveTaskList {
static Path directoryPath = Path.of("./data");
static Path filePath = directoryPath.resolve("data.txt");

public static void saveTasks(ArrayList<Task> taskList) {
try {
if (!Files.exists(directoryPath)) {
Files.createDirectories(directoryPath);
System.out.println("Directory created: " + directoryPath);
}

if (!Files.exists(filePath)) {
Files.createFile(filePath);
System.out.println("File created: " + filePath);
}

try (var writer = new FileWriter(filePath.toFile())) {
for (var item : taskList) {
writer.write(item + System.lineSeparator());
}
System.out.println("Task list data saved to file: " + filePath);
}

} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
}
}

public static List<String> loadTasks() {
List<String> taskList;

if (!Files.exists(directoryPath)) {
System.out.println("Data directory does not exist");
}

try {
if (Files.exists(filePath)) {
taskList = Files.readAllLines(filePath);
return taskList;
} else {
System.out.println("Data file does not exist: " + filePath);
}
} catch (IOException e) {
System.out.println("An error occurred while reading the file: " + e.getMessage());
}
return null;
}
}



29 changes: 29 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Task {
protected String description;
protected boolean isDone;

@telferang telferang Sep 4, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boolean variable name "isDone" is well-named, as it clearly indicates its purpose and state.


public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}
Comment on lines +25 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can make the code more explicit here instead of using ternary if operator?


public void markAsDone() {
this.isDone = true;
}

public void markAsNotDone() {
this.isDone = false;
}

public String getDescription() {
return description;
}

public String toString() {
return String.format("[%s] %s", this.getStatusIcon(), this.getDescription());
}
}
192 changes: 192 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

public class TaskList {
public ArrayList<Task> taskList;
protected int taskIndex;
public static final int INVALID_INDEX = -1;

public TaskList() {
this.taskList = new ArrayList<>();
}

public void addTask(Task task) {
System.out.println("--------------------------------------------");
this.taskList.add(task);
System.out.println("Okay! I have added this task: ");
System.out.println(task.toString());
System.out.printf("You currently have %d tasks in your list \n", taskList.size());
System.out.println("--------------------------------------------");
}

public void deleteTask(HashMap<String, String> commandArguments) {
String argument = commandArguments.get("argument");
getTaskIndex(argument);
Task task = taskList.get(this.taskIndex);
System.out.println("--------------------------------------------");
this.taskList.remove(task);
System.out.println("Okay! I have removed this task: ");
System.out.println(task.toString());
System.out.printf("You currently have %d tasks in your list \n", taskList.size());
System.out.println("--------------------------------------------");
}

public void listTasks() {
System.out.println("--------------------------------------------");
System.out.println("Here are your current tasks: ");
for (int i = 0; i < taskList.size(); i++) {
System.out.println(i + 1 + "." + this.taskList.get(i));
}
System.out.println("--------------------------------------------");
}

public void addToDo(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskDescriptionException {
String argument = commandArguments.get("argument");
if (argument == null) {
throw new TulipTaskException.InvalidTaskDescriptionException();
}

ToDo task = new ToDo(commandArguments.get("argument"));
addTask(task);
}

public void addDeadline(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidDeadlineException, TulipTaskException.InvalidTaskDescriptionException {
String argument = commandArguments.get("argument");
String by = commandArguments.get("/by");

if (argument == null) {
throw new TulipTaskException.InvalidTaskDescriptionException();
}

if (by == null) {
throw new TulipTaskException.InvalidDeadlineException();
}

Deadline task = new Deadline(argument, by);
addTask(task);
}

public void addEvent(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskDescriptionException, TulipTaskException.InvalidStartDateException, TulipTaskException.InvalidEndDateException {
String argument = commandArguments.get("argument");
String from = commandArguments.get("/from");
String to = commandArguments.get("/to");

if (argument == null) {
throw new TulipTaskException.InvalidTaskDescriptionException();
}

if (from == null) {
throw new TulipTaskException.InvalidStartDateException();
}

if (to == null) {
throw new TulipTaskException.InvalidEndDateException();
}

Event task = new Event(argument, from, to);
addTask(task);
}

public void getTaskIndex(String indexString) {
int index = Integer.parseInt(indexString) - 1;

if (index < 0 || index > taskList.size() - 1) {
this.taskIndex = INVALID_INDEX;
return;
}

this.taskIndex = index;
}

public void markTaskAsDone(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskIndexException {
String argument = commandArguments.get("argument");
getTaskIndex(argument);

if (this.taskIndex == INVALID_INDEX) {
throw new TulipTaskException.InvalidTaskIndexException();
}

Task task = taskList.get(this.taskIndex);
task.markAsDone();
System.out.println("--------------------------------------------");
System.out.println("Great job! I have marked this task as done: ");
System.out.println(task);
System.out.println("--------------------------------------------");
}

public void markTaskAsNotDone(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskIndexException {
String argument = commandArguments.get("argument");
getTaskIndex(argument);

if (this.taskIndex == INVALID_INDEX) {
throw new TulipTaskException.InvalidTaskIndexException();
}

Task task = taskList.get(this.taskIndex);
task.markAsNotDone();
System.out.println("--------------------------------------------");
System.out.println("Okay, I have marked this task as not done: ");
System.out.println(task);
System.out.println("--------------------------------------------");
}

public void saveTaskToFile() {
System.out.println("--------------------------------------------");
SaveTaskList.saveTasks(this.taskList);
System.out.println("--------------------------------------------");
}

public void loadTaskFromFile() {
List<String> list = SaveTaskList.loadTasks();
for (int i = 0; i < Objects.requireNonNull(list).size(); i++) {
parseTask(list.get(i));
}
System.out.println("--------------------------------------------");
System.out.println("Tasks have been successfully loaded!");
System.out.println("--------------------------------------------");
}

public void parseTask(String line) {
String type = line.substring(1, 2);
boolean completed = line.charAt(4) == 'X';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the naming of your boolean can be improved to sound like a boolean!


switch (type) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your switch statements, maybe you could follow the style mentioned here.

case "T" -> {
String description = line.substring(7);
ToDo task = new ToDo(description);
this.taskList.add(task);
if (completed) {
task.markAsDone();
}

}
case "D" -> {
int deadlineIndex = line.indexOf("(by:");
String description = line.substring(7, deadlineIndex).trim();
String deadline = line.substring(deadlineIndex + 5, line.length() - 1);
Deadline task = new Deadline(description, deadline);
this.taskList.add(task);
if (completed) {
task.markAsDone();
}

}
case "E" -> {
int eventIndex = line.indexOf("(from:");
String description = line.substring(7, eventIndex).trim();
String timeInfo = line.substring(eventIndex + 6, line.length() - 1); // Extract time info

String[] timeParts = timeInfo.split(" to: ");
String eventStart = timeParts[0].trim();
String eventEnd = timeParts[1].trim();
Event task = new Event(description, eventStart, eventEnd);
this.taskList.add(task);
if (completed) {
task.markAsDone();
}
}
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class ToDo extends Task {
public ToDo(String description) {
super(description);
}

public String toString() {
return String.format("[T]%s", super.toString());
}
}
Loading