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/functions/Duke.java
  • Loading branch information
lixiyuan416 committed Sep 14, 2021
2 parents 6d29fe2 + b1aef3e commit ec14bc6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 16 deletions.
1 change: 1 addition & 0 deletions SAVEDLIST.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T--X--dance
115 changes: 105 additions & 10 deletions src/main/java/duke/functions/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
import java.util.ArrayList;
import java.util.Scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class Duke {

private static Boolean isFinished = false;
private static final String filepath = "SAVEDLIST.txt";

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Task> items = new ArrayList<>();

printIntro();

try {
readSavedList(items);
} catch (IOException e) {
System.out.println("Error creating/reading task list");
}

while (!isFinished) {
String userInput = sc.nextLine();
String command = userInput.split(" ")[0];
Expand All @@ -37,18 +49,29 @@ public static void main(String[] args) {
} catch (EmptyArgException e) {
System.out.println("\tWhich task is done?");
}
try {
writeToFile(items);
} catch (IOException e) {
System.out.println("\tError writing to file");
}
break;
}
case "delete":{
case "delete": {
try {
handleDelete(items, userInput);
printTaskList(items);
} catch (NumberFormatException e) {
System.out.println("\tInvalid argument, please enter a valid task number!");
} catch (NullPointerException | IndexOutOfBoundsException e) {
System.out.println("\tNo such task!");
} catch (EmptyArgException e) {
System.out.println("\tWhich task to delete?");
}
try {
writeToFile(items);
} catch (IOException e) {
System.out.println("\tError writing to file");
}
break;
}
case "todo": {
Expand All @@ -57,6 +80,11 @@ public static void main(String[] args) {
} catch (EmptyArgException e) {
System.out.println("\tDescription of todo cannot be empty!");
}
try {
writeToFile(items);
} catch (IOException e) {
System.out.println("\tError writing to file");
}
break;
}
case "deadline": {
Expand All @@ -67,6 +95,11 @@ public static void main(String[] args) {
} catch (WrongFormatException e) {
System.out.println("\tWrong format! Try \"Deadline [description] \\by [due date]\"");
}
try {
writeToFile(items);
} catch (IOException e) {
System.out.println("\tError writing to file");
}
break;
}
case "event": {
Expand All @@ -77,6 +110,11 @@ public static void main(String[] args) {
} catch (WrongFormatException e) {
System.out.println("\tWrong format! Try \"Event [description] \\by [due date]\"");
}
try {
writeToFile(items);
} catch (IOException e) {
System.out.println("\tError writing to file");
}
break;
}
default:
Expand All @@ -88,6 +126,60 @@ public static void main(String[] args) {
printBye();
}

private static void writeToFile(ArrayList<Task> items) throws IOException {
FileWriter fw = new FileWriter(filepath);
for (Task i : items) {
if (i instanceof Todo) {
fw.write("T" + "--" + i.getStatusIcon() + "--" + i.description);
fw.write(System.lineSeparator());
} else if (i instanceof Deadline) {
fw.write("D" + "--" + i.getStatusIcon() + "--" + i.description + "--" + ((Deadline) i).getBy());
fw.write(System.lineSeparator());

} else if (i instanceof Event) {
fw.write("E" + "--" + i.getStatusIcon() + "--" + i.description + "--" + ((Event) i).getAt());
fw.write(System.lineSeparator());
}
}
if (items.isEmpty()){
fw.write("");
}
fw.close();
}

private static void readSavedList(ArrayList<Task> items) throws IOException {
File f = new File(filepath);
if (f.createNewFile()) {
drawLine();
System.out.println("\tNo saved task list, new file created");
drawLine();
} else {
Scanner fs = new Scanner(f);
while (fs.hasNext()) {
String[] feed = fs.nextLine().split("--");

switch (feed[0]) {
case "T":
items.add(new Todo(feed[2]));
break;
case "D":
items.add(new Deadline(feed[2], feed[3]));
break;
case "E":
items.add(new Event(feed[2], feed[3]));
break;
default:
break;
}

if (feed[1].equals("X")) {
items.get(items.size() - 1).markAsDone();
}
}
printTaskList(items);
}
}

private static void handleDelete(ArrayList<Task> items, String userInput) throws EmptyArgException {
String[] arg = userInput.split(" ");
if (arg.length < 2) {
Expand Down Expand Up @@ -154,10 +246,14 @@ private static void handleTodo(ArrayList<Task> items, String userInput) throws E

private static void printTaskList(ArrayList<Task> items) {
drawLine();
System.out.println("\tHere is your task list:");
for (int i = 0; i < items.size(); i++) {
System.out.print("\t\t" + (i + 1) + ". ");
System.out.println(items.get(i));
if (!items.isEmpty()) {
System.out.println("\tHere is your task list:");
for (int i = 0; i < items.size(); i++) {
System.out.print("\t\t" + (i + 1) + ". ");
System.out.println(items.get(i));
}
} else {
System.out.println("\tYou have no tasks");
}
drawLine();
}
Expand All @@ -172,13 +268,12 @@ private static void printTaskAdded(ArrayList<Task> items) {
private static void printIntro() {
System.out.println(
" _ _\n" +
" ( \\---/ )\n" +
" ) . . (\n" +
"________________________,--._(___Y___)_,--._______________________ \n" +
" `--' `--'");
" ( \\---/ )\n" +
" ) . . (\n" +
"________________________,--._(___Y___)_,--._______________________ \n" +
" `--' `--'");
System.out.println("Hello I'm Duke");
System.out.println("What can I do for you?");
drawLine();
}

private static void printBye() {
Expand Down
2 changes: 1 addition & 1 deletion text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ What can I do for you?
____________________________________________________________

____________________________________________________________
Here is your task list:
You have no tasks
____________________________________________________________
Task Added:
[T][ ] fix the fan
Expand Down
6 changes: 1 addition & 5 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ list
todo fix the fan
deadline buy cabbage /by 5.30pm
event attend cca zoom meeting /at 11pm
done 1
done 2
done 3
list
bye
list

0 comments on commit ec14bc6

Please sign in to comment.