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.java
#	src/main/java/DukeExceptions.java
#	src/main/java/Parser.java
  • Loading branch information
joelczk committed Feb 12, 2020
2 parents f74aaf1 + 3f7f39a commit efc452d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ public Deadline(String description, String by) {
this.by = by;
}

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

public String getBy() {
return by;
}

@Override
public String storeText() {
return "[D]," + super.getStatus() + "," + super.getDescription() + "," + by;
}

@Override
public String toString() {
return "[D][ " + super.getStatusIcon() + " ] " + super.getDescription() + "(By: " + getBy() + ")";
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Duke {
private UI userInterface;
private Parser parser;
private DukeExceptions dukeExceptions;
private Storage storage;
private final int startIndex = 0;
private final int lengthOfDeadline = 9;
private final int lengthOfEvent = 5;
Expand All @@ -21,6 +22,7 @@ public Duke() {
userInterface = new UI();
parser = new Parser();
dukeExceptions = new DukeExceptions();
storage = new Storage();
}

private void printList() {
Expand All @@ -32,6 +34,7 @@ private void printList() {
}

private void list() {
tasks = storage.loadTasks();
if (tasks.size() == 0) {
dukeExceptions.printListExceptions();
} else {
Expand All @@ -57,12 +60,13 @@ private void markAsDone(int index) {
tasks.set(index, completedTask);
System.out.println(" Duke has marked the following tasks as done:");
System.out.println(" " + completedTask);
storage.save(tasks);
}
}

private void getDoneExceptions(String input) {
try {
int index = getIndex(input,lengthOfDone);
int index = getIndex(input, lengthOfDone);
markAsDone(index);
} catch(IndexOutOfBoundsException exception) {
dukeExceptions.printDoneIOBExceptions(input);
Expand All @@ -79,6 +83,7 @@ private void addToDo(String input) {
System.out.println(" Got it! I have added the following tasks: ");
System.out.println( newToDo);
System.out.println("There are currently " + tasks.size() + " task(s) queued");
storage.save(tasks);
}

private void getToDoExceptions(String input) {
Expand All @@ -105,6 +110,7 @@ private void addEvent(String input) {
System.out.println(" New task has been added:");
System.out.println(" " + newEvent);
System.out.println(" There are currently " + tasks.size() + " task(s) being queued");
storage.save(tasks);
}
}

Expand Down Expand Up @@ -133,6 +139,7 @@ private void addDeadline(String input) {
System.out.println(" Got it!I've added this task:");
System.out.println(" " + newDeadline);
System.out.println(" There are currently " + tasks.size() + " being queued");
storage.save(tasks);
}
}

Expand All @@ -152,7 +159,7 @@ private void deleteItems(int index) {
System.out.println(" Got it! I've removed this task:");
System.out.println(" " + deletedTask);
System.out.println(" There are currently " + length + " task(s) being queued");
// storage.save(tasks);
storage.save(tasks);
}

private void getDeleteExceptions(String input) {
Expand All @@ -169,6 +176,7 @@ private void getDeleteExceptions(String input) {
private void run() {
Scanner sc = new Scanner(System.in);
userInterface.printGreetingMessage();
storage.restoreArray(sc, tasks);
while (true) {
String input = sc.nextLine().trim();
parser.updateInput(input);
Expand All @@ -187,7 +195,7 @@ private void run() {
getEventExceptions(input);
} else if (parser.isDeadline()) {
getDeadlineExceptions(input);
} else if (parser.isDelete()){
} else if (parser.isDelete()) {
getDeleteExceptions(input);
} else {
System.out.println(" Duke does not understand your command! Can you repeat again?");
Expand Down
1 change: 1 addition & 0 deletions src/main/java/DukeExceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ public void printNFEDeleteExceptions(String input) {
System.out.println(" Parameters: delete [TASK NUMBER]");
System.out.println(" Example: delete 1");
}

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

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

public String getEventDetails() {
return eventDetails;
}
Expand All @@ -16,4 +21,9 @@ public String getEventDetails() {
public String toString() {
return "[E][ " + super.getStatusIcon() + " ] " + super.getDescription() + "(At: " + getEventDetails() + ")";
}

@Override
public String storeText() {
return "[E]," + super.getStatus() + "," + super.getDescription() + "," + getEventDetails();
}
}
1 change: 1 addition & 0 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ public boolean isDelete() {
String firstWord = getFirstWord();
return firstWord.equals("delete");
}

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

public class Storage{
protected File file;
protected String filePath;
protected ArrayList <Task> tasks;

public Storage() {
file = new File("store.txt");
filePath = file.getAbsolutePath();
}

public void save(ArrayList <Task> tasks) {
this.tasks = tasks;
try {
FileWriter fileWriter = new FileWriter(filePath);
for (Task task : tasks) {
fileWriter.write(task.storeText());
fileWriter.write(System.lineSeparator());
}
fileWriter.close();
} catch (IOException exception) {
System.out.println(" [Error " + exception.getMessage() + "] ");
System.out.println(" Please contact the systems adminstrator for assistance");
}
}

public void restoreArray(Scanner fileReader, ArrayList <Task> tasksList) {
while (fileReader.hasNextLine()) {
String input = fileReader.nextLine();
String [] parameters = input.split(",");
String type = parameters[0];
boolean isDone = Boolean.parseBoolean(parameters[1]);
String description = parameters[2];
if (type.equals("[T]")) {
ToDo existingToDo = new ToDo(description, isDone);
tasksList.add(existingToDo);
} else if (type.equals("[E]")) {
String eventDetails = parameters[3];
Event existingEvent = new Event(description, isDone, eventDetails);
tasksList.add(existingEvent);

} else {
String by = parameters[3];
Deadline existingDeadline = new Deadline(description, isDone, by);
tasksList.add(existingDeadline);
}
}
}

public ArrayList <Task> loadTasks() {
ArrayList <Task> tasksList = new ArrayList<Task>();
try {
Scanner fileReader = new Scanner(file);
restoreArray(fileReader, tasksList);
fileReader.close();
} catch (FileNotFoundException exception) {
System.out.println("file not found");
}
return tasksList;
}
}
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 boolean getStatus() {
return isDone;
}
Expand All @@ -26,4 +31,8 @@ public void markAsDone() {
public String toString() {
return "[ " + getStatusIcon() + " ] " + getDescription();
}

public String storeText() {
return isDone + "|" + getDescription();
}
}
8 changes: 8 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ public ToDo(String description) {
super(description);
}

public ToDo(String description, boolean isDone) {
super(description , isDone);
}
@Override
public String toString() {
return "[T][ " + super.getStatusIcon() + " ] " + super.getDescription();
}

@Override
public String storeText() {
return "[T]," + super.getStatus() + "," + super.getDescription() + ",";
}
}

0 comments on commit efc452d

Please sign in to comment.