Skip to content

Commit

Permalink
Refactor add new tasks methods
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleyW00 committed Sep 20, 2023
1 parent 22a5da6 commit 629359b
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,28 @@ public static boolean checkEmptyTodoInput(String input) {
return input.trim().isEmpty();
}

public static void addTodo(String input) throws DukeTaskException, IOException {
if (checkEmptyTodoInput(input)) {
throw new DukeTaskException();
}

tasks.add(new Todo(input.trim()));

private static void addNewData(String dataString) throws IOException {
FileWriter writer = new FileWriter(DATA_PATH, true);

if (tasksCount != 0) {
writer.write(System.lineSeparator());
}

writer.write("T | " + tasks.get(tasksCount).getStatus() + " | " + tasks.get(tasksCount).getDescription());
writer.write(dataString);
writer.close();
}

tasksCount++;
public static void addTodo(String input) throws DukeTaskException, IOException {
if (checkEmptyTodoInput(input)) {
throw new DukeTaskException();
}

tasks.add(new Todo(input.trim()));

String dataString = "T | false | " + input.trim();
addNewData(dataString);

tasksCount++;
}

public static void addDeadline(String input) throws DukeTaskException, IOException {
Expand All @@ -74,18 +78,10 @@ public static void addDeadline(String input) throws DukeTaskException, IOExcepti
throw new DukeTaskException();
}


tasks.add(new Deadline(parsedInput[0].trim(), parsedInput[1].trim()));

FileWriter writer = new FileWriter(DATA_PATH, true);

if (tasksCount != 0) {
writer.write(System.lineSeparator());
}

writer.write("D | " + tasks.get(tasksCount).getStatus() + " | " + parsedInput[0].trim() +
" | " + parsedInput[1].trim());
writer.close();
String dataString = "D | false | " + parsedInput[0].trim() + " | " + parsedInput[1].trim();
addNewData(dataString);

tasksCount++;
}
Expand All @@ -107,18 +103,11 @@ public static void addEvent(String input) throws DukeTaskException, IOException
}

String[] parsedInput = input.split(FROM_KEYWORD + "|" + TO_KEYWORD);

tasks.add(new Event(parsedInput[0].trim(), parsedInput[1].trim(), parsedInput[2].trim()));

FileWriter writer = new FileWriter(DATA_PATH, true);

if (tasksCount != 0) {
writer.write(System.lineSeparator());
}

writer.write("E | " + tasks.get(tasksCount).getStatus() + " | " + parsedInput[0].trim() +
" | " + parsedInput[1].trim() + " | " + parsedInput[2].trim());
writer.close();
String dataString = "E | false | " + parsedInput[0].trim() + " | " + parsedInput[1].trim()
+ " | " + parsedInput[2].trim();
addNewData(dataString);

tasksCount++;
}
Expand Down

0 comments on commit 629359b

Please sign in to comment.