Skip to content

Commit

Permalink
Improve Code quality
Browse files Browse the repository at this point in the history
Divide readTask method into multiple method to increase
readability.
  • Loading branch information
iyioon committed Sep 13, 2023
1 parent 33e6ffb commit 034cb6e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 72 deletions.
1 change: 1 addition & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
T|true|1
T|false|2
22 changes: 0 additions & 22 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,28 +176,6 @@ private String getResponse(String input) {
return ui.getLastMsg();
}

/*
public void run() {
ui.showWelcome();
boolean isExit = false;
Storage.readTask(tasks, ui);
while (!isExit) {
try {
String userInput = ui.readInput();
ui.showLine();
Command command = Parser.parse(userInput);
isExit = command.execute(this.tasks, ui);
Storage.writeTask(tasks, ui); // Store to file
} catch (DukeException e) {
ui.showError(e.getMessage());
} finally {
ui.showLine();
}
}
}
*/

/**
* DialogBox.
*/
Expand Down
113 changes: 68 additions & 45 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,83 @@ public class Storage {
*/
public static void readTask(TaskList tasks, Ui ui) {
try {
File directory = new File("./data");
File file = new File(filePath);
File file = checkFileExist(ui);
readFile(tasks, file);
} catch (FileNotFoundException e) {
ui.showError(e.getMessage());
} catch (DukeException e) {
ui.showError(e.getMessage());
}
}

if (!directory.exists()) {
directory.mkdir();
}
/**
* Check if file exist.
* Create new if it does not exist.
*
* @param ui To show error message.
* @return file.
*/
private static File checkFileExist(Ui ui) {
File directory = new File("./data");
File file = new File(filePath);

if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
ui.showError("Error creating new file: " + e.getMessage());
}
}
if (!directory.exists()) {
directory.mkdir();
}

Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
Task task;
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
ui.showError("Error creating new file: " + e.getMessage());
}
}
return file;
}

String line = scanner.nextLine();
String[] parts = line.split("\\|");
String taskType = parts[0];
boolean isDone = parts[1].equals("true");
String description = parts[2];
/**
* Read the file and store it in tasklist.
*
* @param tasks List of tasks.
* @param file To read.
* @throws FileNotFoundException When file not found.
* @throws DukeException When failes to save on list.
*/
private static void readFile(TaskList tasks, File file) throws FileNotFoundException, DukeException {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
Task task;

switch (taskType) {
case "T":
task = new Todo(description);
break;
case "D":
String date = parts[3];
task = new Deadline(description, date);
break;
case "E":
String from = parts[3];
String to = parts[4];
task = new Event(description, from, to);
break;
default:
continue;
}
String line = scanner.nextLine();
String[] parts = line.split("\\|");
String taskType = parts[0];
boolean isDone = parts[1].equals("true");
String description = parts[2];

if (isDone) {
task.mark();
}
switch (taskType) {
case "T":
task = new Todo(description);
break;
case "D":
String date = parts[3];
task = new Deadline(description, date);
break;
case "E":
String from = parts[3];
String to = parts[4];
task = new Event(description, from, to);
break;
default:
continue;
}

tasks.add(task);
if (isDone) {
task.mark();
}
scanner.close();
} catch (FileNotFoundException e) {
ui.showError(e.getMessage());
} catch (DukeException e) {
ui.showError(e.getMessage());

tasks.add(task);
}
scanner.close();
}


Expand Down
8 changes: 4 additions & 4 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void add(Task task) {
* @param index The index of the task to be removed.
*/
public void remove(int index) {
assert index >= 0 : "index should be >= 0";
assert index >= 0 : "index should be >= 0";
tasks.remove(index);
}

Expand All @@ -47,7 +47,7 @@ public void remove(int index) {
* @param index The index of the task to be marked as done.
*/
public void mark(int index) {
assert index >= 0 : "index should be >= 0";
assert index >= 0 : "index should be >= 0";
tasks.get(index).mark();
}

Expand All @@ -57,7 +57,7 @@ public void mark(int index) {
* @param index The index of the task to be unmarked.
*/
public void unmark(int index) {
assert index >= 0 : "index should be >= 0";
assert index >= 0 : "index should be >= 0";
tasks.get(index).unmark();
}

Expand Down Expand Up @@ -107,7 +107,7 @@ public String toString() {
* @return all task on the date.
* @throws DukeException
*/
public String getbyDate(String date) throws DukeException {
public String getByDate(String date) throws DukeException {
LocalDate targetDate;
try {
targetDate = LocalDate.parse(date);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/command/ListCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public boolean execute(TaskList tasks, Ui ui) throws DukeException {
String msg = ui.getLastMsg();
String[] parts = msg.toLowerCase().split("\\s+");
if (parts.length >= 2) { // If the date is provided
ui.respond(tasks.getbyDate(parts[1]));
ui.respond(tasks.getByDate(parts[1]));
} else {
ui.respond(tasks.toString());
}
Expand Down

0 comments on commit 034cb6e

Please sign in to comment.