Skip to content

Commit

Permalink
Merge pull request #1 from ChangruHenryQian/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Improve code quality
  • Loading branch information
ChangruHenryQian authored Sep 15, 2023
2 parents 3b3712a + 59658eb commit e20422e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 27 deletions.
2 changes: 2 additions & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
T | 1 | daf
T | 1 | 2rd
54 changes: 42 additions & 12 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,13 @@ public String parse() {
} else if (command.startsWith("mark")) {
return this.taskList.markTask(Integer.valueOf(command.split(" ")[1]) - 1);
} else if (command.startsWith("todo")) {
if (command.split(" ", 2).length == 1) {
throw new DukeException(" OOPS!!! The description of a todo cannot be empty.");
}
ToDo newToDo = new ToDo(command.split(" ", 2)[1]);
ToDo newToDo = createToDoFromCommand();
return this.taskList.addTask(newToDo);
} else if (command.startsWith("deadline")) {
LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]);
String name = command.split(" /by ", 2)[0].split(" ", 2)[1];
Deadline newDeadline = new Deadline(name, deadline);
Deadline newDeadline = createDeadlineFromCommand();
return this.taskList.addTask(newDeadline);
} else if (command.startsWith("event")) {
LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1]
.split(" /to ", 2)[0]);
LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]);
String name = command.split(" /from ", 2)[0].split(" ", 2)[1];
Event newEvent = new Event(name, startTime, endTime);
Event newEvent = createEventFromCommand();
return this.taskList.addTask(newEvent);
} else if (command.startsWith("delete")) {
return this.taskList.deleteTask(Integer.valueOf(command.split(" ")[1]) - 1);
Expand All @@ -63,6 +54,45 @@ public String parse() {
return "Invalid command.";
}

/**
* Creates the new Event from the command.
*
* @return The new Event.
*/
private Event createEventFromCommand() {
LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1]
.split(" /to ", 2)[0]);
LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]);
String name = command.split(" /from ", 2)[0].split(" ", 2)[1];
Event newEvent = new Event(name, startTime, endTime);
return newEvent;
}

/**
* Creates the new Deadline from the command.
*
* @return The new Deadline.
*/
private Deadline createDeadlineFromCommand() {
LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]);
String name = command.split(" /by ", 2)[0].split(" ", 2)[1];
Deadline newDeadline = new Deadline(name, deadline);
return newDeadline;
}

/**
* Creates the new ToDo from the command.
*
* @return The new ToDo.
*/
private ToDo createToDoFromCommand() {
if (command.split(" ", 2).length == 1) {
throw new DukeException(" OOPS!!! The description of a todo cannot be empty.");
}
ToDo newToDo = new ToDo(command.split(" ", 2)[1]);
return newToDo;
}

/**
* Returns if the parser is ended.
*/
Expand Down
80 changes: 67 additions & 13 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

Expand All @@ -22,23 +23,46 @@ public Storage(String filePath) {
}

/**
* Loads the task list with file. Initializes the file
* when necessary.
* Loads the task list with file.
*
* @return The task list.
* @throws IOException If unable to gain input from file.
*/
public ArrayList<Task> load() throws IOException {
File dir = new File("./data");
String dirPath = this.filePath.split("/")[0];
File f = getFile(dirPath);
ArrayList<Task> tasks = loadTasks(f);
return tasks;
}

/**
* Returns the file for storage. Initializes the file
* when necessary.
*
* @param path Path of the directory of the storage file.
* @throws IOException If an input or output exception occurs.
*/
private File getFile(String path) throws IOException {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
f.createNewFile();
return f;
}

/**
* Returns the task list after loading.
*
* @param f The file for storage.
* @throws FileNotFoundException If an attempt to open the file fails.
*/
private ArrayList<Task> loadTasks(File f) throws FileNotFoundException {
Scanner s = new Scanner(f);
ArrayList<Task> tasks = new ArrayList<>();
while (s.hasNext()) {
tasks.add(inputToTask(s.nextLine()));
tasks.add(storageToTask(s.nextLine()));
}
return tasks;
}
Expand All @@ -49,25 +73,53 @@ public ArrayList<Task> load() throws IOException {
* @param input The String input.
* @return The corresponding Task.
*/
private Task inputToTask(String input) {
private Task storageToTask(String input) throws DukeException {
String taskType = input.split(" \\| ")[0];
boolean isComplete = input.split(" \\| ")[1].equals("1");
String description = input.split(" \\| ")[2];
if (taskType.equals("T")) {
return new ToDo(description, isComplete);
} else if (taskType.equals("D")) {
LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Deadline(description, isComplete, d);
return this.createDeadlineFromStorage(input, description, isComplete);
} else if (taskType.equals("E")) {
return this.createEventFromStorage(input, description, isComplete);
} else {
LocalDate start = LocalDate.parse(input.split(" \\| ")[3],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate end = LocalDate.parse(input.split(" \\| ")[4],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Event(description, isComplete, start, end);
throw new DukeException("Unknown task type.");
}
}


/**
* Creates the Event from the storage input.
*
* @param input The input line.
* @param description The description of the Event.
* @param isComplete Whether the Event is completed.
* @return The Event.
*/
private Event createEventFromStorage(String input, String description, boolean isComplete) {
LocalDate start = LocalDate.parse(input.split(" \\| ")[3],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
LocalDate end = LocalDate.parse(input.split(" \\| ")[4],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Event(description, isComplete, start, end);
}

/**
* Creates the Deadline from the storage input.
*
* @param input The input line.
* @param description The description of the Deadline.
* @param isComplete Whether the Deadline is completed.
* @return The Event.
*/
private Deadline createDeadlineFromStorage(String input, String description, boolean isComplete) {
LocalDate d = LocalDate.parse(input.split(" \\| ")[3],
DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH));
return new Deadline(description, isComplete, d);
}


/**
* Adds the last task in the task list to file.
*
Expand Down Expand Up @@ -97,10 +149,12 @@ public void rewriteFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < list.size(); i++) {
fw.write(list.get(i).toTxt());
if (i != list.size() - 1) {
fw.write(list.get(i).toTxt());
fw.write("\n");
continue;
}
fw.write(list.get(i).toTxt());
}
fw.close();
} catch (IOException e) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public String markTask(int num) {
public String findTask(String keyword) {
ArrayList<Task> result = new ArrayList<>();
for (int i = 0; i < this.tasks.size(); i++) {
if (tasks.get(i).toString().contains(keyword)) {
result.add(tasks.get(i));
if (!tasks.get(i).toString().contains(keyword)) {
continue;
}
result.add(tasks.get(i));
}
return Ui.listMatchingTasks(result);
}
Expand Down

0 comments on commit e20422e

Please sign in to comment.