Skip to content

Commit

Permalink
added Parser, Ui, Storage, TaskList classes
Browse files Browse the repository at this point in the history
  • Loading branch information
cheeyou committed Sep 6, 2019
1 parent fd94b63 commit 33338cd
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 136 deletions.
140 changes: 71 additions & 69 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import java.io.FileNotFoundException;
import java.lang.reflect.Array;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;

public class Duke {

private Storage storage;
private TaskList tasks;
private Ui ui;
Expand All @@ -15,89 +11,95 @@ public Duke(String filePath) {
storage = new Storage(filePath);
try {
tasks = new TaskList(storage.load());
} catch (DukeException e) {
} catch (FileNotFoundException e) {
ui.showLoadingError();
tasks = new TaskList(); //if the file is corrupted, the tasks list will start fresh
tasks = new TaskList();
}
}

public void run() {
ui.introduction();
ui.showWelcome();
while(true) {
try {
String commandLine = ui.getNextLine();
Parser p = new Parser(commandLine);
}
}
}

public static void main(String[] args) {
new Duke("data/tasks.txt").run();


while(reader.hasNextLine()) {
String input = reader.next();
if(input.equals("bye")) {
System.out.println(horLine);
System.out.println(" Bye. Hope to see you again soon!");
System.out.println(horLine);
Parser p = new Parser(ui.readNextLine());
p.parse();
String command = p.getCommand();
String taskDetails = p.getTaskDetails();
if(command.equals("bye")) {
ui.showBye();
break;
} else if(input.equals("list")){
System.out.println(horLine);
if(taskList.isEmpty()) {
} else if(command.equals("list")) {
ui.showLine();
if(tasks.getListOfTasks().isEmpty()) {
System.out.println("Sorry, there are no tasks in the list");
} else {
System.out.println(" Here are the tasks in your list:");
int counter = 1;
for (Task task : taskList) {
System.out.println(" " + counter + "." + task);
counter++;
}
tasks.printList();
ui.showLine();
ui.separationline();
}
System.out.println(horLine + "\n");

} else if(input.equals("done")) {
int number = reader.nextInt();
Task temp = taskList.get(number - 1);
temp.setDone();
System.out.println(horLine);
System.out.println(" Nice! I've marked this task as done:");
System.out.println(" " + temp);
System.out.println(horLine + "\n");
storage.arrayDataToFile(taskList);
} else if(input.equals("delete")) {
int number = reader.nextInt();
tasks.deleteTask(number);
System.out.println(horLine);
System.out.println(" Nice! I've removed this task:");
System.out.println(" " + temp);
System.out.println(" Now you have " + taskList.size() + " tasks in the list.");
System.out.println(horLine + "\n");
storage.arrayDataToFile(taskList);
} else{ //all other commands
} else if(command.equals("done")) {
int number = Integer.parseInt(taskDetails);
Task temp = tasks.getTask(number - 1);
tasks.setTaskAsDone(number - 1);
ui.showDone(temp);
storage.write(tasks.getListOfTasks());
} else if(command.equals("delete")) {
int number = Integer.parseInt(taskDetails);
Task temp = tasks.getTask(number - 1);
tasks.deleteTask(number - 1);
int size = tasks.getListOfTasks().size();
ui.showDelete(temp, size);
storage.write(tasks.getListOfTasks());
} else { //all other commands
try {
String taskDetails = reader.nextLine().trim();
tasks.addTask(input, taskDetails);
if (command.equals("todo")) {
if (taskDetails.equals("")) { //will it be such that String[].get(1) will be zero i.e. error?
throw new DukeException(" ☹ OOPS!!! The description of a todo cannot be empty.");
}
tasks.addTask(new Todo(taskDetails));
} else if (command.equals("deadline") || command.equals("event")) {
if(taskDetails.equals("")) {
throw new DukeException(" ☹ OOPS!!! The description of a " +
command + " cannot be empty.");
}
//replace the first / so that the dates will not be split up
taskDetails = taskDetails.replaceFirst("/", ":"); //need to assign this to tempString so it is re-recorded
String[] tempStringArr = taskDetails.split(":");
String description = ((String) Array.get(tempStringArr, 0)).trim(); //to remove ending whitespace
String secondString = ((String) Array.get(tempStringArr, 1)).substring(3);
if (command.equals("deadline")) {
tasks.addTask(new Deadline(description, secondString));
} else {
tasks.addTask(new Event(description, secondString));
}
} else {//all other keywords not part of Duke's task handling schedule
throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-(");
}
} catch (DukeException de) {
System.out.println(horLine);
ui.showLine();
System.err.println(de.getMessage());
System.out.println(horLine + "\n");
ui.showLine();
ui.separationline();
continue; //to prevent printing of below mentioned lines
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println(horLine);
System.err.println(" OOPS!!! You need to specify the " + input +
ui.showLine();
System.err.println(" ☹ OOPS!!! You need to specify the " + command +
" time through a /by (deadline) and /at (event)");
System.out.println(horLine + "\n");
ui.showLine();
ui.separationline();
continue;
}
System.out.println(horLine);
System.out.println(" Got it. I've added this task:");
System.out.println(" " + taskList.get(taskList.size() - 1));
System.out.println(" Now you have " + taskList.size() + " tasks in the list.");
System.out.println(horLine);
System.out.println();
storage.arrayDataToFile(taskList);
Task temp = tasks.getTask(tasks.getListOfTasks().size() - 1);
int size = tasks.getListOfTasks().size();
ui.showAdd(temp, size);
storage.write(tasks.getListOfTasks());
}
}
}
}
public static void main(String[] args) {
new Duke("data/tasks.txt").run();
}
}



23 changes: 19 additions & 4 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import java.lang.reflect.Array;

public class Parser {
private String line;
private String inputString;
private String command;
private String taskDetails;

public Parser(String line) {
this.line = line;
public Parser(String inputString) {
this.inputString = inputString;
}
public String parse() {

public void parse() { //cannot use "|" as a replacement
String temp = this.inputString.replaceFirst(" ", ":");
System.out.println(temp);
String[] tempArr = temp.split(":");
command = (String)Array.get(tempArr, 0);
if(tempArr.length > 1) { //account for the fact that commands like "list" do not have task details
taskDetails = ((String) Array.get(tempArr, 1)).trim();
}
}
public String getCommand() {
return this.command;
}
public String getTaskDetails() {
return this.taskDetails;
}
}
31 changes: 13 additions & 18 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,30 @@

public class Storage {
private String filePath;
private ArrayList<Task> tasks;

public Storage(String filePath) {
this.filePath = filePath;
this.tasks = new ArrayList<>();
}

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

public void updateFile(String textToAdd) throws IOException {
FileWriter fw = new FileWriter(this.filePath);
//updateTasksToFile and appendToFile are methods used by write() to write data from programme into the file
private void updateTasksToFile(String filePath, String textToAdd) throws IOException {
FileWriter fw = new FileWriter(filePath);
fw.write(textToAdd);
fw.close();
}
public void appendToFile(String textToAppend) throws IOException {
FileWriter fw = new FileWriter(this.filePath, true); // create a FileWriter in append mode
private void appendToFile(String filePath, String textToAppend) throws IOException {
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode
fw.write(textToAppend);
fw.close();
}

public void arrayDataToFile() {
public void write(ArrayList<Task> list) {
//Store all the latest data from the ArrayList
try {
this.updateFile("");
updateTasksToFile(this.filePath, "");
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
for(Task task : this.tasks) {
for(Task task : list) {
StringBuilder temp = new StringBuilder();
temp.append(task.getTaskType());
temp.append(" | ");
Expand All @@ -57,14 +51,16 @@ public void arrayDataToFile() {
}
}
try {
this.appendToFile(temp.toString() + System.lineSeparator());
appendToFile(this.filePath, temp.toString() + System.lineSeparator());
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}

//Loading data from file into the Array
public ArrayList<Task> load() throws FileNotFoundException {
ArrayList<Task> list = new ArrayList<>();
File f = new File(this.filePath);
Scanner s = new Scanner(f);
while(s.hasNext()) {
Expand All @@ -88,9 +84,8 @@ public ArrayList<Task> load() throws FileNotFoundException {
if(((String)Array.get(temp, 2)).equals("1")) {
newTask.setDone();
}
this.tasks.add(newTask);
list.add(newTask);
}
return this.tasks;
return list;
}

}
70 changes: 40 additions & 30 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
import java.lang.reflect.Array;
import java.util.ArrayList;

/**
* Represents a taskList with the ability to perform features on the tasks within.
*/
public class TaskList {
private ArrayList<Task> tasks;
private ArrayList<Task> listOfTasks;

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

public ArrayList<Task> getTasks() {
return this.tasks;
public ArrayList<Task> getListOfTasks() {
return listOfTasks;
}
public void addTask(Task newTask) {
listOfTasks.add(newTask);
}
public void deleteTask(int number) {
Task temp = tasks.get(number - 1);
tasks.remove(number - 1);
Task temp = listOfTasks.get(number - 1);
listOfTasks.remove(number - 1);
}
public void addTask (String taskType, String taskDetails) throws DukeException {
if (taskDetails.equals("")) {
throw new DukeException(" ☹ OOPS!!! The description of a " + taskType + " cannot be empty.");
}
if (taskType.equals("todo")) {
this.tasks.add(new Todo(taskDetails));
} else if(taskType.equals("deadline") || taskType.equals("event")) {
if(taskType.equals("deadline") || taskType.equals("event")) {
//replace the first / so that the dates will not be split up
taskDetails = taskDetails.replaceFirst("/", ":"); //need to assign this to taskDetails so it is re-recorded
String[] tempStringArr = taskDetails.split(":");
String description = ((String) Array.get(tempStringArr, 0)).trim(); //to remove ending whitespace
String secondString = ((String) Array.get(tempStringArr, 1)).substring(3);
if (taskType.equals("deadline")) {
tasks.add(new Deadline(description, secondString));
} else {
tasks.add(new Event(description, secondString));
}
}
} else {//all other keywords not part of Duke's task handling schedule
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");

public void printList() {
int counter = 1;
for (Task task : this.listOfTasks) {
System.out.println(" " + counter + "." + task);
counter++;
}
}

/**
* Sets a task in a specified position in listOfTasks as done.
*
* @param number Specified index of task.Task object in listOfTasks.
*/
public void setTaskAsDone(int number) {
Task temp = this.listOfTasks.get(number - 1);
temp.setDone();
}

/**
* Returns a task.Task object in a specified position in listOfTasks.
*
* @param number Specified index of task.Task object in listOfTasks.
* @return task.Task
*/
public Task getTask(int number) { //what about case where it is empty?
return this.listOfTasks.get(number);
}
}
Loading

0 comments on commit 33338cd

Please sign in to comment.