Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaojj2209 committed Aug 23, 2020
2 parents 62f18c4 + 0b824d2 commit 419e04b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 26 deletions.
5 changes: 3 additions & 2 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
public class Deadline extends Task {
String deadline;
public Deadline(String description) {
super(description.split(" /by ")[0]);
public Deadline(String description, boolean isDone) {
super(description.split(" /by ")[0], isDone);
System.out.println("new deadline");
this.deadline = description.split(" /by ")[1];
}

Expand Down
88 changes: 70 additions & 18 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;

enum actionType {
QUIT,
Expand Down Expand Up @@ -31,23 +34,67 @@ private static actionType getAction(String input) {
: actionType.WRONG_INPUT;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
private static ArrayList<Task> getTasks(File data) {
ArrayList<Task> list = new ArrayList<>();
System.out.println("Hello, I'm Duke");
System.out.println("I can help you keep track of all your tasks! ☆*:.。.o(≧▽≦)o.。.:*☆");
System.out.println("How to add tasks to the list:");
System.out.println("ToDo - type 'todo' followed by the description");
System.out.println("Deadline - type 'deadline' followed by the description," +
"then '/by', then due date");
System.out.println("Event - type 'event' followed by the description, " +
"then '/at', then timing");
System.out.println("Type 'done' followed by the task number " +
"and I'll mark it as done");
System.out.println("Type 'list' to see the list");
System.out.println("Type 'bye' to exit");
String next;
try {
Scanner dataScanner = new Scanner(data);
while (dataScanner.hasNext()) {
String next = dataScanner.nextLine();
char taskType = next.charAt(1);
boolean isDone = next.charAt(4) == '\u2713';
String description = next.substring(7);
if (taskType == 'T') {
list.add(new ToDo(description, isDone));
} else if (taskType == 'D') {
String[] split = description.split("[(]by:");
String deadlineDesc = split[0] + "/by" + split[1].substring(0, split[1].length() - 1);
list.add(new Deadline(deadlineDesc, isDone));
} else if (taskType == 'E') {
String[] split = description.split("[(]at:");
String eventDesc = split[0] + "/at" + split[1].substring(0, split[1].length() - 1);
list.add(new Event(eventDesc, isDone));
} else {
throw new DukeException("File reading error _(´ཀ`」 ∠)_");
}
}
} catch (Exception e) {
System.out.println(e);
}
return list;
}

private static void updateFile(ArrayList<Task> list) throws IOException {
FileWriter fw = new FileWriter("src/main/data.txt");
for (Task task: list) {
String taskString = task.toString();
fw.write(taskString + System.lineSeparator());
}
fw.close();
}

public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
File data = new File("src/main/data.txt");
ArrayList<Task> list;
if (data.createNewFile()) {
list = new ArrayList<>();
} else {
list = getTasks(data);
}
System.out.println("Hello, I'm Duke");
System.out.println("I can help you keep track of all your tasks! ☆*:.。.o(≧▽≦)o.。.:*☆");
System.out.println("How to add tasks to the list:");
System.out.println("ToDo - type 'todo' followed by the description");
System.out.println("Deadline - type 'deadline' followed by the description," +
"then '/by', then due date");
System.out.println("Event - type 'event' followed by the description, " +
"then '/at', then timing");
System.out.println("Type 'done' followed by the task number " +
"and I'll mark it as done");
System.out.println("Type 'list' to see the list");
System.out.println("Type 'bye' to exit");
String next;
while (true) {
next = sc.nextLine();
actionType action = getAction(next);
Expand All @@ -74,6 +121,7 @@ public static void main(String[] args) {
} else {
Task completedTask = list.get(taskNo - 1);
completedTask.markAsDone();
updateFile(list);
System.out.println("Task marked complete:");
System.out.println(completedTask.toString());
}
Expand All @@ -84,6 +132,7 @@ public static void main(String[] args) {
throw new DukeException("Task does not exist _(´ཀ`」 ∠)_");
} else {
Task deletedTask = list.remove(deleteNo - 1);
updateFile(list);
System.out.println("Task deleted:");
System.out.println(deletedTask.toString());
}
Expand All @@ -92,8 +141,9 @@ public static void main(String[] args) {
if (next.length() < 6) {
throw new DukeException("Task cannot be empty _(´ཀ`」 ∠)_");
} else {
Task newTodo = new ToDo(next.substring(5));
Task newTodo = new ToDo(next.substring(5), false);
list.add(newTodo);
updateFile(list);
System.out.println("Added: " + newTodo.toString());
System.out.println("Total tasks: " + list.size());
}
Expand All @@ -102,8 +152,9 @@ public static void main(String[] args) {
if (next.length() < 7) {
throw new DukeException("Event cannot be empty _(´ཀ`」 ∠)_");
} else {
Task newEvent = new Event(next.substring(6));
Task newEvent = new Event(next.substring(6), false);
list.add(newEvent);
updateFile(list);
System.out.println("Added: " + newEvent.toString());
System.out.println("Total tasks: " + list.size());
}
Expand All @@ -112,8 +163,9 @@ public static void main(String[] args) {
if (next.length() < 10) {
throw new DukeException("Deadline cannot be empty _(´ཀ`」 ∠)_");
} else {
Task newDeadline = new Deadline(next.substring(9));
Task newDeadline = new Deadline(next.substring(9), false);
list.add(newDeadline);
updateFile(list);
System.out.println("Added: " + newDeadline.toString());
System.out.println("Total tasks: " + list.size());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
public class Event extends Task {
String eventTime;
public Event(String description) {
super(description.split(" /at ")[0]);
public Event(String description, boolean isDone) {
super(description.split(" /at ")[0], isDone);
this.eventTime = description.split(" /at ")[1];
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ public class Task {
protected String description;
protected boolean isDone;

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

public String getStatusIcon() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public class ToDo extends Task {
public ToDo(String description) {
super(description);
public ToDo(String description, boolean isDone) {
super(description, isDone);
}

@Override
Expand Down

0 comments on commit 419e04b

Please sign in to comment.