Skip to content

Commit

Permalink
Implemented saving to saving to/reading from disk + Level-7
Browse files Browse the repository at this point in the history
  • Loading branch information
PhireHandy committed Sep 2, 2019
1 parent 6c96a18 commit cb85e8d
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 95 deletions.
12 changes: 10 additions & 2 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ public Deadline(String name, String time) {
this.isDone = false;
}

public String toFile() {
if(isDone) {
return "D-1-" + name + "-" + time;
} else {
return "D-0-" + name + "-" + time;
}
}

public String toString() {
if (isDone) {
return "[D}[✓] " + name + " (by: " + time + ")";
return "[D][✓] " + name + " (by: " + time + ")";
} else {
return "[D}[✗] " + name + " (by: " + time + ")";
return "[D][✗] " + name + " (by: " + time + ")";
}
}
}
19 changes: 13 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import java.io.FileNotFoundException;

public class Duke {
public static void main(String[] args) {
System.out.println(" ____________________________________________________________\n" +
" Hello! I'm Duke\n" +
" What can I do for you?\n" +
" ____________________________________________________________\n");
System.out.println(" ____________________________________________________________\n" +
" Hello! I'm Duke\n" +
" What can I do for you?\n" +
" ____________________________________________________________\n");

MainManager mm = new MainManager();
mm.run();
MainManager mm = new MainManager();
try {
mm.readFromFile();
} catch (FileNotFoundException fE) {
System.out.println(fE);
}
mm.run();
}
}
14 changes: 11 additions & 3 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ public Event(String name, String time) {
this.isDone = false;
}

public String toString() {
public String toFile() {
if(isDone) {
return "[E}[✓] " + name + " (at: " + time + ")";
return "E-1-" + name + "-" + time;
} else {
return "E-0-" + name + "-" + time;
}
}

public String toString() {
if (isDone) {
return "[E][✓] " + name + " (at: " + time + ")";
} else {
return "[E}[✗] " + name + " (at: " + time + ")";
return "[E][✗] " + name + " (at: " + time + ")";
}
}
}
261 changes: 180 additions & 81 deletions src/main/java/MainManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import java.lang.reflect.Array;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Path;
import java.io.IOException;
import java.io.FileNotFoundException;

public class MainManager {
private ArrayList<Task> list;
Expand All @@ -25,114 +30,208 @@ private String[] getDeadlineDetails(String command) throws TaskException{
return woCommand.split(" /by ");
}

private void writeToFile() throws FileNotFoundException, IOException {
try {
Path p = Paths.get(System.getProperty("user.dir"));
File data = new File(p + "/data/duke.txt");
FileWriter fw = new FileWriter(data);

public void run(){
Scanner sc = new Scanner(System.in);
String toWrite = "";
for(Task task : list) {
toWrite += task.toFile() + " \n";
}

while(sc.hasNext()) {
String toPrint = sc.nextLine();
String[] command = toPrint.split(" ");
switch(command[0]) {
case "bye":
System.out.println("Bye. Hope to see you again soon!");
break;
fw.write(toWrite);
fw.close();
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
}

case "list":
for(int i = 0; i < list.size(); i++) {
if(list.get(i) != null) {
System.out.println(i + 1 + "." + list.get(i));
}
public void readFromFile() throws FileNotFoundException{
Path p = Paths.get(System.getProperty("user.dir"));
File data = new File(p + "/data/duke.txt");
try {
Scanner sc = new Scanner(data);

while(sc.hasNextLine()) {
String[] next = sc.nextLine().split("-");

switch (next[0]) {
case ("T"):
Task todo = new Todo(next[2]);

if (Integer.valueOf(next[1]) == 1) {
todo.setDone();
}
list.add(todo);
break;

case "done" :
try {
if(toPrint.length() <= 4) {
throw new DoneException();
}
System.out.println("Nice! I've marked this task as done:");
Task currTask = list.get(Integer.parseInt(command[1]) - 1);
currTask.setDone();
System.out.println(" " + currTask);
} catch (DoneException doEx) {
System.out.println(doEx);

case ("E"):
Task event = new Event(next[2], next[3]);

if (Integer.valueOf(next[1]) == 1) {
event.setDone();
}
list.add(event);
break;

case "todo":
try {
if(toPrint.length() <= 4) {
throw new TaskException();
}
String todoName = toPrint.substring(5);
Task todo = new Todo(todoName);
list.add(todo);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + todo);
System.out.println("Now you have " + list.size() + " tasks in the list.");
case ("D"):
Task deadline = new Deadline(next[2], next[3]);

} catch(TaskException toEx) {
System.out.println(toEx);
if (Integer.valueOf(next[1]) == 1) {
deadline.setDone();
}
list.add(deadline);
break;
}
}
sc.close();
} catch(FileNotFoundException fE) {
System.out.println(fE);
}

case "deadline":
try {
String[] deadlineDetails = getDeadlineDetails(toPrint);
Task deadline = new Deadline(deadlineDetails[0], deadlineDetails[1]);
list.add(deadline);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + deadline);
System.out.println("Now you have " + list.size() + " tasks in the list.");
} catch(TaskException dlEx) {
System.out.println(dlEx);

}

public void run(){
Scanner sc = new Scanner(System.in);

while(sc.hasNext()) {
String toPrint = sc.nextLine();
String[] command = toPrint.split(" ");

switch(command[0]) {
case "bye":
System.out.println("Bye. Hope to see you again soon!");
break;

case "list":
for(int i = 0; i < list.size(); i++) {
if(list.get(i) != null) {
System.out.println(i + 1 + "." + list.get(i));
}
break;
}
break;

case "event":
try {
String[] eventDetails = getEventDetails(toPrint);
Task event = new Event(eventDetails[0], eventDetails[1]);
list.add(event);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + event);
System.out.println("Now you have " + list.size() + " tasks in the list.");
} catch (TaskException evEx) {
System.out.println(evEx);
case "done" :
try {
if(toPrint.length() <= 4) {
throw new DoneException();
}
break;
System.out.println("Nice! I've marked this task as done:");
Task currTask = list.get(Integer.parseInt(command[1]) - 1);
currTask.setDone();
System.out.println(" " + currTask);

writeToFile();
} catch (DoneException doEx) {
System.out.println(doEx);
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
break;

case "delete":
try {
if (command.length <= 1){
throw new DeleteException();
} else {
int index = Integer.parseInt(command[1]);
case "todo":
try {
if(toPrint.length() <= 4) {
throw new TaskException();
}
String todoName = toPrint.substring(5);
Task todo = new Todo(todoName);
list.add(todo);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + todo);
System.out.println("Now you have " + list.size() + " tasks in the list.");

writeToFile();
} catch(TaskException toEx) {
System.out.println(toEx);
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
break;

if (command.length <= 1 || index > list.size() || index <= 0) {
throw new DeleteException();
}
case "deadline":
try {
String[] deadlineDetails = getDeadlineDetails(toPrint);
Task deadline = new Deadline(deadlineDetails[0], deadlineDetails[1]);
list.add(deadline);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + deadline);
System.out.println("Now you have " + list.size() + " tasks in the list.");

writeToFile();
} catch(TaskException dlEx) {
System.out.println(dlEx);
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
break;

case "event":
try {
String[] eventDetails = getEventDetails(toPrint);
Task event = new Event(eventDetails[0], eventDetails[1]);
list.add(event);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + event);
System.out.println("Now you have " + list.size() + " tasks in the list.");

writeToFile();
} catch (TaskException evEx) {
System.out.println(evEx);
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
break;

case "delete":
try {
if (command.length <= 1){
throw new DeleteException();
} else {
int index = Integer.parseInt(command[1]);

System.out.println(" Noted. I've removed this task: ");
System.out.println(" " + list.remove(index - 1));
System.out.println("Now you have " + list.size() + " tasks in the list.");
if (command.length <= 1 || index > list.size() || index <= 0) {
throw new DeleteException();
}
} catch(DeleteException deEx) {
System.out.println(deEx);

System.out.println(" Noted. I've removed this task: ");
System.out.println(" " + list.remove(index - 1));
System.out.println("Now you have " + list.size() + " tasks in the list.");

writeToFile();
}
break;
} catch(DeleteException deEx) {
System.out.println(deEx);
} catch (FileNotFoundException Fe) {
System.out.println(Fe);
} catch (IOException IOe) {
System.out.println(IOe);
}
break;


default:
System.out.println(new UnknownCommandException());
break;
default:
System.out.println(new UnknownCommandException());
break;
}

if(toPrint.equals("bye")) {
break;
}
}
}


}
2 changes: 2 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public void setDone() {
isDone = true;
}

public abstract String toFile();

@Override
public abstract String toString();
}
Loading

0 comments on commit cb85e8d

Please sign in to comment.