Skip to content

Commit

Permalink
Merge branch 'branch-Level-7'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chang-CH committed Aug 19, 2021
2 parents d3e6567 + 22137c1 commit 017eabf
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 16 deletions.
2 changes: 2 additions & 0 deletions data/save.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
E-0-asd 123-something 567
D-0-##24325-qwe 123
11 changes: 10 additions & 1 deletion src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
public class Deadline extends Task {

protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

public Deadline(boolean done, String description, String by) {
super(done, description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}

@Override
public String saveString() {
return "D-" + super.saveString() + "-" + by;
}
}
76 changes: 61 additions & 15 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;


public class Duke {
private static String PATH;

private static boolean run;
private static ArrayList<Task> tasks;
private static int index;

private enum Commands {
BYE (),
}


public static void main(String[] args) {
PATH = System.getProperty("user.dir") + "\\data\\save.txt";
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
Expand All @@ -27,7 +26,7 @@ public static void main(String[] args) {
System.out.println(greeting);

run = true;
tasks = new ArrayList<Task>();
fromFile(PATH);

Scanner inputReader = new Scanner(System.in);

Expand All @@ -46,13 +45,14 @@ public static boolean eval(String input) {

switch (inputArray[0]) {
case "bye":
saveFile(PATH);
System.out.println("____________________________________________________________\n"
+ "Bye. Don't come again!\n"
+ "____________________________________________________________\n");
return false;
case "list":
System.out.println("____________________________________________________________\n");
for(int i = 0; i < index; i++) {
for(int i = 0; i < tasks.size(); i++) {
System.out.printf("%d. %s\n", i + 1, tasks.get(i).toString());
}
System.out.println("____________________________________________________________\n");
Expand All @@ -70,9 +70,8 @@ public static boolean eval(String input) {
selectedTask = Integer.parseInt(inputArray[1]) - 1;
System.out.println("Noted. I've removed this task:\n" +
tasks.get(selectedTask).toString() + "\n" +
"Now you have " + (index - 1) + " tasks in the list.\n");
"Now you have " + tasks.size() + " tasks in the list.\n");
tasks.remove(selectedTask);
index--;
return true;
case "event":
params = input.split("/at");
Expand All @@ -82,9 +81,8 @@ public static boolean eval(String input) {

System.out.println("____________________________________________________________\n"
+ "Got it. I've added this task:\n"
+ tasks.get(index).toString() + "\n"
+ tasks.get(tasks.size() - 1).toString() + "\n"
+ "____________________________________________________________\n");
index++;
return true;
case "deadline":
params = input.split("/by");
Expand All @@ -94,9 +92,8 @@ public static boolean eval(String input) {

System.out.println("____________________________________________________________\n"
+ "Got it. I've added this task:\n"
+ tasks.get(index).toString() + "\n"
+ tasks.get(tasks.size() - 1).toString() + "\n"
+ "____________________________________________________________\n");
index++;
return true;
case "todo":
try {
Expand All @@ -107,9 +104,8 @@ public static boolean eval(String input) {
tasks.add(new ToDo(name));
System.out.println("____________________________________________________________\n"
+ "Got it. I've added this task:\n"
+ tasks.get(index).toString() + "\n"
+ tasks.get(tasks.size() - 1).toString() + "\n"
+ "____________________________________________________________\n");
index++;
}
catch (StringIndexOutOfBoundsException e) {
System.out.println("____________________________________________________________\n" +
Expand All @@ -125,4 +121,54 @@ public static boolean eval(String input) {
return true;
}
}

public static void fromFile(String path) {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
String line;
String[] arr;
boolean done;
tasks = new ArrayList<>();

while((line = bufferedReader.readLine()) != null) {
arr = line.split("-");
done = arr[1].equals("1");

switch (arr[0]) {
case "T":
tasks.add(new ToDo(done, arr[2]));
break;
case "E":
tasks.add(new Event(done, arr[2], arr[3]));
break;
case "D":
tasks.add(new Deadline(done, arr[2], arr[3]));
break;
default:
throw new IllegalArgumentException("Unrecognized task flag");
}
}
} catch(IOException e) {
System.out.println(e.toString());
}
}

public static void saveFile(String path) {
StringBuilder stringBuilder = new StringBuilder();
for (Task task : tasks) {
if (stringBuilder.length() != 0) {
stringBuilder.append("\n");
}
stringBuilder.append(task.saveString());
}
try {
FileWriter fileWriter = new FileWriter(path);
String output = stringBuilder.toString();
fileWriter.write(output);
fileWriter.close();
} catch (IOException e) {
System.out.println(e);
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ public Event(String description, String at) {
this.at = at;
}

public Event(boolean done, String description, String at) {
super(done, description);
this.at = at;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
}

@Override
public String saveString() {
return "E-" + super.saveString() + "-" + at;
}
}
13 changes: 13 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public Task(String description) {
this.description = description;
}

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

public void setDone() {
done = true;
}
Expand All @@ -18,4 +23,12 @@ public String toString() {
}
return "[" + indicator + "] " + this.description;
}

public String saveString() {
char indicator = '0';
if (done) {
indicator = '1';
}
return indicator + "-" + this.description;
}
}
9 changes: 9 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ public ToDo(String description) {
super(description);
}

public ToDo(boolean done, String description) {
super(done, description);
}

@Override
public String toString() {
return "[T]" + super.toString();
}

@Override
public String saveString() {
return "T-" + super.saveString();
}
}

0 comments on commit 017eabf

Please sign in to comment.