Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/duke/Duke.java
  • Loading branch information
SibingWu committed Feb 14, 2020
2 parents 3278712 + 5ee424a commit 3ec3c7e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 7 deletions.
Empty file added data/duke.txt
Empty file.
90 changes: 88 additions & 2 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import duke.tasks.Task;
import duke.tasks.Todo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
Expand All @@ -23,9 +27,20 @@
public class Duke {
// a array to store user input task
private static List<Task> tasks = new ArrayList<>();

private static String filePath = "data" + File.separator + "duke.txt";

public static void main(String[] args) {
displayWelcomeMessage();

// for debugging
// System.out.println(java.nio.file.Paths.get("").toAbsolutePath().toString());

try {
loadFileContents(filePath);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}

Scanner in = new Scanner(System.in);
String input = null;
Expand All @@ -42,10 +57,13 @@ public static void main(String[] args) {
case DONE_COMMAND:
try {
markAsDone(Integer.parseInt(split[1].trim()));
updateTasksToFile(filePath, tasks);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (NumberFormatException e) {
displayInvalidTaskNumberMessage();
} catch (IOException e) {
displayIOExceptionMessage(e);
} catch (ChatboxException e) {
displayInvalidTaskNumberMessage();
}
Expand All @@ -64,26 +82,35 @@ public static void main(String[] args) {
case TODO_COMMAND:
try {
addTodo(split[1]);
updateTasksToFile(filePath, tasks);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (IOException e) {
displayIOExceptionMessage(e);
}
break;
case DEADLINE_COMMAND:
try {
addDeadline(split[1]);
updateTasksToFile(filePath, tasks);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (ChatboxException e) {
displayTimeMissingMessage();
} catch (IOException e) {
displayIOExceptionMessage(e);
}
break;
case EVENT_COMMAND:
try {
addEvent(split[1]);
updateTasksToFile(filePath, tasks);
} catch (ArrayIndexOutOfBoundsException e) {
displayEmptyDescriptionMessage(command);
} catch (ChatboxException e) {
displayTimeMissingMessage();
} catch (IOException e) {
displayIOExceptionMessage(e);
}
break;
case BYE_COMMAND:
Expand All @@ -95,7 +122,7 @@ public static void main(String[] args) {
}
} while (!input.equals(BYE_COMMAND));
}

private static void displayWelcomeMessage() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
Expand Down Expand Up @@ -197,8 +224,67 @@ private static void displayCommandNotFoundMessage() {
private static void displayEmptyDescriptionMessage(String command) {
System.out.println(String.format("OOPS!!! The description of a %s cannot be empty.", command));
}

private static void displayTimeMissingMessage() {
System.out.println("Oops! Time is missing!");
}

private static void displayIOExceptionMessage(IOException e) {
System.out.println("Something went wrong: " + e.getMessage());
}

private static void loadFileContents(String filePath) throws FileNotFoundException {
File f = new File(filePath); // create a File for the given file path
Scanner s = new Scanner(f); // create a Scanner using the File as the source
System.out.println("Tasks on the list: ");
if (!s.hasNext()) {
System.out.println("Empty list");
}

while (s.hasNext()) {
String taskLine = s.nextLine();
System.out.println(taskLine);
saveStringtoTask(taskLine);
}
}

private static void updateTasksToFile(String filePath, List<Task> tasks) throws IOException {
FileWriter fw = new FileWriter(filePath);
for (Task task: tasks) {
fw.write(task.getFileString() + System.lineSeparator());
}
fw.close();
}

private static void saveStringtoTask(String fileLine) {
String[] arrays = fileLine.split("\\|");
String type = arrays[0].trim();
String isDone = arrays[1].trim();
String description = arrays[2].trim();
switch (type) {
case "T":
Task todo = new Todo(description);
if (isDone == "1") {
todo.markAsDone();
}
tasks.add(todo);
break;
case "D":
String by = arrays[3].trim();
Task deadline = new Deadline(description, by);
if (isDone == "1") {
deadline.markAsDone();
}
tasks.add(deadline);
break;
case "E":
String at = arrays[3].trim();
Task event = new Event(description, at);
if (isDone == "1") {
event.markAsDone();
}
tasks.add(event);
break;
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ public class Deadline extends Task {
public Deadline(String description, String by) {
super(description);
this.by = by;
this.type = "D";
}

@Override
public String toString() {
return String.format("[D]%s (by: %s)", super.toString(), by);
return String.format("[%s]%s (by: %s)", this.type, super.toString(), by);
}

@Override
public String getFileString() {
return String.format("%s | %d | %s | %s", type, isDone ? 1 : 0, description, by);
}
}
8 changes: 7 additions & 1 deletion src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ public class Event extends Task {
public Event(String description, String at) {
super(description);
this.at = at;
this.type = "E";
}

@Override
public String toString() {
return String.format("[E]%s (at: %s)", super.toString(), at);
return String.format("[%s]%s (at: %s)", this.type, super.toString(), at);
}

@Override
public String getFileString() {
return String.format("%s | %d | %s | %s", type, isDone ? 1 : 0, description, at);
}
}
15 changes: 13 additions & 2 deletions src/main/java/duke/tasks/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import static duke.utils.Constants.YES_ICON;
import static duke.utils.Constants.NO_ICON;

public class Task {
public abstract class Task {
protected String description;
protected boolean isDone;
protected String type; // to be assigned in subclasses

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

public String getStatusIcon() {
return (isDone ? YES_ICON : NO_ICON); // return tick or X symbols
}
Expand All @@ -20,7 +21,17 @@ public String toString() {
return String.format("[%s] %s", getStatusIcon(), description);
}

public boolean isDone() {
return this.isDone;
}

public void markAsDone() {
isDone = true;
}

public String getType() {
return type;
}

public abstract String getFileString();
}
8 changes: 7 additions & 1 deletion src/main/java/duke/tasks/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ public class Todo extends Task {

public Todo(String description) {
super(description);
this.type = "T";
}

@Override
public String toString() {
return String.format("[T]%s", super.toString());
return String.format("[%s]%s", this.type, super.toString());
}

@Override
public String getFileString() {
return String.format("%s | %d | %s", type, isDone ? 1 : 0, description);
}
}

0 comments on commit 3ec3c7e

Please sign in to comment.