Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply refactoring to improve code quality #3

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 29 additions & 21 deletions src/main/java/edith/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void save(ArrayList<Task> listOfTasks) throws IOException {
* @throws IOException If an I/O error occurs while reading from the file.
* @throws EdithException If the file contains corrupted data or the data format is invalid.
*/
public ArrayList<Task> load() throws IOException, EdithException {
public ArrayList<Task> load() throws IOException {
ArrayList<Task> listOfTasks = new ArrayList<>();
File file = new File(this.filePath);
File directory = new File(file.getParent());
Expand All @@ -79,26 +79,7 @@ public ArrayList<Task> load() throws IOException, EdithException {
String taskString = taskData[2].trim();
Task task;

switch (typeOfTaskString) {
case "[T]":
task = new ToDo(taskString);
break;
case "[D]":
String[] deadlineParts = taskString.split(" /by ");
String deadlineTask = deadlineParts[0].trim();
String dueDate = deadlineParts[1].trim();
task = new Deadline(deadlineTask, dueDate);
break;
case "[E]":
String[] eventParts = taskString.split(" /from | /to ");
String eventTask = eventParts[0].trim();
String startTime = eventParts[1].trim();
String endTime = eventParts[2].trim();
task = new Event(eventTask, startTime, endTime);
break;
default:
throw new EdithException("An error occurred while parsing the Edith.task list. Data might be corrupted.", 1);
}
task = loadHelper(typeOfTaskString, taskString);

if (statusString.equals("[X]")) {
task.markTaskDone();
Expand All @@ -108,4 +89,31 @@ public ArrayList<Task> load() throws IOException, EdithException {
}
return listOfTasks;
}

public Task loadHelper(String typeOfTaskString, String taskString) throws EdithException {
Task task;

switch (typeOfTaskString) {
case "[T]":
task = new ToDo(taskString);
break;
case "[D]":
String[] deadlineParts = taskString.split(" /by ");
String deadlineTask = deadlineParts[0].trim();
String dueDate = deadlineParts[1].trim();
task = new Deadline(deadlineTask, dueDate);
break;
case "[E]":
String[] eventParts = taskString.split(" /from | /to ");
String eventTask = eventParts[0].trim();
String startTime = eventParts[1].trim();
String endTime = eventParts[2].trim();
task = new Event(eventTask, startTime, endTime);
break;
default:
throw new EdithException("An error occurred while parsing the Edith.task list. Data might be corrupted.", 1);
}

return task;
}
}
37 changes: 22 additions & 15 deletions src/main/java/edith/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ public AddCommand(String instruction) {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws EdithException {
Task task = null;
task = addTaskHelper();

try {
tasks.addTask(task);
storage.save(tasks.getListOfTasks());

StringBuilder response = new StringBuilder();
response.append("Got it. I've added this task:\n")
.append(task.toString()).append("\n")
.append("There are now ").append(tasks.getNumOfTasks()).append(" tasks in your list.");

return response.toString();
} catch (DateTimeParseException e) {
throw new EdithException(ui.invalidDateTimeError(), 1);
} catch (IOException e) {
return "An error occurred while saving updated Edith.task list.";
}
}

public Task addTaskHelper() throws EdithException {
Task task;

if (instruction.startsWith("todo ")) {
String taskString = instruction.substring(5).trim();
Expand Down Expand Up @@ -77,20 +98,6 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws EdithExcept
throw new EdithException("Invalid command for adding tasks.");
}

try {
tasks.addTask(task);
storage.save(tasks.getListOfTasks());

StringBuilder response = new StringBuilder();
response.append("Got it. I've added this task:\n")
.append(task.toString()).append("\n")
.append("There are now ").append(tasks.getNumOfTasks()).append(" tasks in your list.");

return response.toString();
} catch (DateTimeParseException e) {
throw new EdithException(ui.invalidDateTimeError(), 1);
} catch (IOException e) {
return "An error occurred while saving updated Edith.task list.";
}
return task;
}
}
3 changes: 2 additions & 1 deletion src/main/java/edith/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public DeleteCommand(int index) {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws EdithException {
if (index < 0 || index >= tasks.getNumOfTasks()) {
throw new EdithException("Task " + index + " does not exist. Please enter a valid task number.");
int num = index + 1;
throw new EdithException("Task " + num + " does not exist. Please enter a valid task number.");
}

Task taskToDelete = tasks.getTask(index);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edith/command/MarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public MarkCommand(int index) {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws EdithException {
if (index < 0 || index >= tasks.getNumOfTasks()) {
throw new EdithException("Task " + index + " does not exist. Please enter a valid task number.");
int num = index + 1;
throw new EdithException("Task " + num + " does not exist. Please enter a valid task number.");
}

Task taskToMark = tasks.getTask(index);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edith/command/UnmarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public UnmarkCommand(int index) {
@Override
public String execute(TaskList tasks, Ui ui, Storage storage) throws EdithException {
if (index < 0 || index >= tasks.getNumOfTasks()) {
throw new EdithException("Task " + index + " does not exist. Please enter a valid task number.");
int num = index + 1;
throw new EdithException("Task " + num + " does not exist. Please enter a valid task number.");
}

Task taskToUnmark = tasks.getTask(index);
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/edith/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,23 @@ public String listTasks(Ui ui) {
* @param ui The Ui object used to display the tasks.
*/
public String listTasksOnDate(String date, Ui ui) {
int index = 1;
boolean isDue = false;
boolean isStartingOn = false;

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
LocalDate localDate = LocalDate.parse(date, formatter);

int index = 1;
StringBuilder response = new StringBuilder();

response.append("Here are your tasks due by ").append(date).append(":\n");
listDueTasks(index, response, localDate);

response.append("Here are your events starting on ").append(date).append(":\n");
listEventsOnDate(index, response, localDate);

return response.toString();
}

public void listDueTasks(int index, StringBuilder response, LocalDate localDate) {
boolean isDue = false;

for (Task task : listOfTasks) {
if (task instanceof Deadline) {
Expand All @@ -141,8 +149,11 @@ public String listTasksOnDate(String date, Ui ui) {
} else {
response.append("\n");
}
}

public void listEventsOnDate(int index, StringBuilder response, LocalDate localDate) {
boolean isStartingOn = false;

response.append("Here are your events starting on ").append(date).append(":\n");
for (Task task : listOfTasks) {
if (task instanceof Event) {
Event event = (Event) task;
Expand All @@ -157,8 +168,6 @@ public String listTasksOnDate(String date, Ui ui) {
if (!isStartingOn) {
response.append("NOTHING");
}

return response.toString();
}

/**
Expand Down