Skip to content

Commit

Permalink
Finish Level 7. Save
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChangru committed Sep 5, 2023
1 parent 611f1ef commit 4197612
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 8 deletions.
2 changes: 2 additions & 0 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
T | 1 | hihi
T | 0 | hihihi
10 changes: 10 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ public Deadline(String description, String by) {
this.by = by;
}

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

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

@Override
public String toTxt() {
return "D | " + super.toTxt() + " | " + this.by;
}
}
94 changes: 87 additions & 7 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import java.util.ArrayList;
import java.util.Scanner;
public class Duke {

public static void main(String[] args) {
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

Scanner userInput = new Scanner(System.in);
public class Duke {

ArrayList<Task> list = new ArrayList<>();
private Storage storage;
private TaskList tasks;
private Ui ui = new Ui();
private static final String filePath = "./data/duke.txt";


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

String command = userInput.nextLine();

ArrayList<Task> list;

try {
list = load();
} catch (IOException e) {
throw new DukeException("Unable to find file with data.");
}

Scanner userInput = new Scanner(System.in);
String command = userInput.nextLine();
while (!command.startsWith("bye")) {

try{
Expand All @@ -33,9 +49,10 @@ public static void main(String[] args) {
System.out.println(" Nice! I've marked this task as done:");
System.out.println(" " + list.get(Integer.valueOf(command.split(" ")[1]) - 1).toString());
System.out.println("____________________________________________________________");
rewriteFile(list);
} else if (command.startsWith("todo")) {
if (command.split(" ", 2).length == 1) {
throw new DukeException(" OOPS!!! The description of a todo cannot be empty.");
throw new DukeException(" OOPS!!! The description of a todo cannot be empty.");
}
ToDo newToDo = new ToDo(command.split(" ", 2)[1]);
list.add(newToDo);
Expand All @@ -44,6 +61,7 @@ public static void main(String[] args) {
System.out.println(" " + newToDo.toString());
System.out.println(" Now you have " + list.size() + " tasks in the list.");
System.out.println("____________________________________________________________");
addToFile(list);
} else if (command.startsWith("deadline")) {
String deadline = command.split(" /by ", 2)[1];
String name = command.split(" /by ", 2)[0].split(" ", 2)[1];
Expand All @@ -54,6 +72,7 @@ public static void main(String[] args) {
System.out.println(" " + newDeadline.toString());
System.out.println(" Now you have " + list.size() + " tasks in the list.");
System.out.println("____________________________________________________________");
addToFile(list);
} else if (command.startsWith("event")) {
String startTime = command.split(" /from ", 2)[1]
.split(" /to ", 2)[0];
Expand All @@ -66,15 +85,17 @@ public static void main(String[] args) {
System.out.println(" " + newEvent.toString());
System.out.println(" Now you have " + list.size() + " tasks in the list.");
System.out.println("____________________________________________________________");
addToFile(list);
} else if (command.startsWith("delete")) {
Task re = list.remove(Integer.valueOf(command.split(" ")[1]) - 1);
System.out.println("____________________________________________________________");
System.out.println(" Noted. I've removed this task:");
System.out.println(" " + re.toString());
System.out.println(" Now you have " + list.size() + " tasks in the list.");
System.out.println("____________________________________________________________");
rewriteFile(list);
} else {
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (DukeException e) {
System.out.println("____________________________________________________________");
Expand All @@ -89,4 +110,63 @@ public static void main(String[] args) {
System.out.println(" Bye. Hope to see you again soon!");
System.out.println("____________________________________________________________");
}

public static ArrayList<Task> load() throws IOException {
File dir = new File("./data");
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
f.createNewFile();
Scanner s = new Scanner(f);
ArrayList<Task> tasks = new ArrayList<>();
while (s.hasNext()) {
tasks.add(addTask(s.nextLine()));
}
return tasks;
}

private static Task addTask(String input) {
String taskType = input.split(" \\| ")[0];
boolean isComplete = input.split(" \\| ")[1].equals("1");
String description = input.split(" \\| ")[2];
if (taskType.equals("T")) {
return new ToDo(description, isComplete);
} else if (taskType.equals("D")) {
return new Deadline(description, isComplete, input.split(" \\| ")[3]);
} else {
return new Event(description, isComplete, input.split(" \\| ")[3], input.split(" \\| ")[4]);
}
}


public static void addToFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath, true);
if (list.size() == 1) {
fw.write(list.get(0).toTxt());
} else {
fw.write("\n" + list.get(list.size() - 1).toTxt());
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}

public static void rewriteFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
fw.write(list.get(i).toTxt());
} else {
fw.write(list.get(i).toTxt() + "\n");
}
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class DukeException extends Exception{
public class DukeException extends RuntimeException{
public DukeException(String message) {
super(message);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ public Event(String description, String startTime, String endTime) {
this.endTime = endTime;
}

public Event(String description, boolean isDone, String startTime, String endTime) {
super(description, isDone);
this.startTime = startTime;
this.endTime = endTime;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + startTime + " to: " + endTime + ")";
}

@Override
public String toTxt() {
return "E | " + super.toTxt() + " | " + this.startTime + " | " + this.endTime;
}
}
3 changes: 3 additions & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Parser {

}
74 changes: 74 additions & 0 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Storage {
private String filePath;

public Storage(String filePath) {
this.filePath = filePath;
}

public ArrayList<Task> load() throws IOException {
File dir = new File("./data");
if (!dir.exists()) {
dir.mkdir();
}
File f = new File(filePath);
f.createNewFile();
Scanner s = new Scanner(f);
ArrayList<Task> tasks = new ArrayList<>();
while (s.hasNext()) {
tasks.add(addTask(s.nextLine()));
}
return tasks;
}

private Task addTask(String input) {
String taskType = input.split(" \\| ")[0];
boolean isComplete = input.split(" \\| ")[1].equals("1");
String description = input.split(" \\| ")[2];
if (taskType.equals("T")) {
return new ToDo(description, isComplete);
} else if (taskType.equals("D")) {
return new Deadline(description, isComplete, input.split(" \\| ")[3]);
} else {
return new Event(description, isComplete, input.split(" \\| ")[3], input.split(" \\| ")[4]);
}
}


public void addToFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath, true);
if (list.size() == 1) {
fw.write(list.get(0).toTxt());
} else {
fw.write("\n" + list.get(list.size() - 1).toTxt());
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}

public void rewriteFile(ArrayList<Task> list) throws DukeException {
try {
FileWriter fw = new FileWriter(filePath);
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
fw.write(list.get(i).toTxt());
} else {
fw.write(list.get(i).toTxt() + "\n");
}
}
fw.close();
} catch (IOException e) {
throw new DukeException("Unable to write to file.");
}
}
}

9 changes: 9 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ public Task(String description) {
this.isDone = false;
}

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

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}
Expand All @@ -20,4 +25,8 @@ public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}

public String toTxt() {
return (this.isDone ? "1" : "0") + " | " + this.description;
}

}
17 changes: 17 additions & 0 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.ArrayList;

public class TaskList {
protected ArrayList<Task> tasks;

public TaskList() {
this.tasks = new ArrayList<>();
}

public TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}

public boolean isEmpty() {
return tasks.isEmpty();
}
}
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(String description, boolean isDone) {
super(description, isDone);
}

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

@Override
public String toTxt() {
return "T | " + super.toTxt();
}
}
2 changes: 2 additions & 0 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Ui {
}

0 comments on commit 4197612

Please sign in to comment.