Skip to content

Commit

Permalink
more oop done
Browse files Browse the repository at this point in the history
  • Loading branch information
TanZhanMing committed Aug 27, 2019
1 parent c450403 commit e1a34f1
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 109 deletions.
108 changes: 8 additions & 100 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;

public class Duke {

Expand All @@ -12,117 +10,27 @@ public enum Action {
DONE
}


public static void main(String[] args) throws IOException {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";

Scanner scan = new Scanner(System.in);
Ui ui = new Ui();
ArrayList<Task> taskList = new ArrayList<>(100);

System.out.println(logo);
System.out.println("Hello! I'm Duke \nWhat can I do for you?");

InputParser inputParser = new InputParser(taskList);
Scanner scan = new Scanner(System.in);
String input = "";
String firstWord;
int taskNumber;
String by;
String desc;
String at;


try {
FileReading.checkFileExists(taskList);
} catch (IOException e) {
System.out.println("Oops. something went wrong with the duke.txt file");
}

ui.greeting();

while (!input.equals("bye")) {
input = scan.nextLine();
firstWord = input.split(" ")[0];

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

case "todo":
try {
desc = input.split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! The description of a todo cannot be empty.\n");
break;
}
ToDo toDo = new ToDo(desc);
ModifyTaskList.modifyTaskList(taskList, toDo, Action.ADD);
break;


case "deadline":
try {
by = input.split(" /by ")[1];
desc = input.split(" /by ")[0].split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! Incorrect description for event; remember to use the /by keyword.\n");
break;
}
Deadline d = new Deadline(desc, by);
ModifyTaskList.modifyTaskList(taskList, d, Action.ADD);
break;

case "event":
try {
at = input.split(" /at ")[1];
desc = input.split(" /at ")[0].split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! Incorrect description for event; remember to use the /at keyword.\n");
break;
}
Event e = new Event(desc, at);
ModifyTaskList.modifyTaskList(taskList, e, Action.ADD);
break;

case "list":
System.out.println("Here are the tasks in your list:");
for (int a = 0; a < taskList.size(); a++) {
System.out.println((a + 1) + ". " + taskList.get(a).toString());
}
System.out.println();
break;

case "done":
try {
taskNumber = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException err) {
System.out.println("OOPS!!! Please enter a valid number\n");
break;
} catch (IndexOutOfBoundsException err) {
System.out.println("You only have " + taskList.size() + " tasks, please choose a number from that\n");
break;
}
ModifyTaskList.modifyTaskList(taskList, taskNumber - 1, Action.DONE);
break;

case "delete":
try {
taskNumber = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException err) {
System.out.println("OOPS!!! Please enter a valid number\n");
break;
} catch (IndexOutOfBoundsException err) {
System.out.println("You only have " + taskList.size() + " tasks, please choose a number from that\n");
break;
}
ModifyTaskList.modifyTaskList(taskList, taskNumber - 1, Action.REMOVE);
break;

default:
System.out.println("OOPS!!! I'm sorry, but I don't know what that means\n");
break;
}
inputParser.actionDeterminer(input);
}

ui.goodbye();
}
}
102 changes: 102 additions & 0 deletions src/main/java/InputParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import java.io.IOException;
import java.util.ArrayList;

public class InputParser {

private ArrayList<Task> taskList;
private ModifyTaskList modifyTaskList = new ModifyTaskList();

protected InputParser(ArrayList<Task> taskList) {
this.taskList = taskList;
}

protected void actionDeterminer (String input) throws IOException {

int taskNumber;
String by;
String desc;
String at;
String firstWord;

firstWord = input.split(" ")[0];
switch (firstWord) {

case "bye":
break;

case "todo":
try {
desc = input.split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! The description of a todo cannot be empty.\n");
break;
}
ToDo toDo = new ToDo(desc);
modifyTaskList.addToTaskList(taskList, toDo, Duke.Action.ADD);
break;


case "deadline":
try {
by = input.split(" /by ")[1];
desc = input.split(" /by ")[0].split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! Incorrect description for event; remember to use the /by keyword.\n");
break;
}
Deadline d = new Deadline(desc, by);
modifyTaskList.addToTaskList(taskList, d, Duke.Action.ADD);
break;

case "event":
try {
at = input.split(" /at ")[1];
desc = input.split(" /at ")[0].split(" ", 2)[1];
} catch (IndexOutOfBoundsException err) {
System.out.println("OOPS!!! Incorrect description for event; remember to use the /at keyword.\n");
break;
}
Event e = new Event(desc, at);
modifyTaskList.addToTaskList(taskList, e, Duke.Action.ADD);
break;

case "list":
System.out.println("Here are the tasks in your list:");
for (int a = 0; a < taskList.size(); a++) {
System.out.println((a + 1) + ". " + taskList.get(a).toString());
}
System.out.println();
break;

case "done":
try {
taskNumber = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException err) {
System.out.println("OOPS!!! Please enter a valid number\n");
break;
} catch (IndexOutOfBoundsException err) {
System.out.println("You only have " + taskList.size() + " tasks, please choose a number from that\n");
break;
}
modifyTaskList.changeTaskList(taskList, taskNumber - 1, Duke.Action.DONE);
break;

case "delete":
try {
taskNumber = Integer.parseInt(input.split(" ")[1]);
} catch (NumberFormatException err) {
System.out.println("OOPS!!! Please enter a valid number\n");
break;
} catch (IndexOutOfBoundsException err) {
System.out.println("You only have " + taskList.size() + " tasks, please choose a number from that\n");
break;
}
modifyTaskList.changeTaskList(taskList, taskNumber - 1, Duke.Action.REMOVE);
break;

default:
System.out.println("OOPS!!! I'm sorry, but I don't know what that means\n");
break;
}
}
}
18 changes: 9 additions & 9 deletions src/main/java/ModifyTaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@

public class ModifyTaskList {

public static void modifyTaskList (ArrayList<Task> taskList, Task t, Duke.Action action) throws IOException {
Ui ui = new Ui();

protected ModifyTaskList() {}

protected void addToTaskList(ArrayList<Task> taskList, Task t, Duke.Action action) throws IOException {
if (action == Duke.Action.ADD) {
taskList.add(t);
System.out.println("Got it. I've added this task:");
System.out.println(taskList.get(taskList.size() - 1).toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.\n");
ui.taskAdded(taskList);
FileWriting.writeToFile(taskList);
}
}
public static void modifyTaskList (ArrayList<Task> taskList, int taskNumber, Duke.Action action){
protected void changeTaskList (ArrayList<Task> taskList, int taskNumber, Duke.Action action){
if (action == Duke.Action.REMOVE) {
try {
System.out.println("Noted. I've removed this task:\n" + taskList.get(taskNumber).toString());
ui.taskRemoved(taskList, taskNumber);
taskList.remove(taskNumber);
System.out.println("Now you have " + taskList.size() + " tasks in the list.\n");
FileWriting.writeToFile(taskList);
}
catch (IndexOutOfBoundsException | IOException err){
Expand All @@ -26,9 +27,8 @@ public static void modifyTaskList (ArrayList<Task> taskList, int taskNumber, Duk
}
if (action == Duke.Action.DONE){
try {
ui.taskDone(taskList, taskNumber);
taskList.get(taskNumber).setDone();
System.out.println("Nice! I've marked this task as done:");
System.out.println(taskList.get(taskNumber).toString() + "\n");
FileWriting.writeToFile(taskList);
}
catch (IndexOutOfBoundsException | IOException err){
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/Ui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;

public class Ui {

public void ui() {

}

public void greeting(){
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println(logo);
System.out.println("Hello! I'm Duke \nWhat can I do for you?");
}

public void goodbye(){
System.out.println("Bye. Hope to see you again soon!");
}

public void taskAdded(ArrayList<Task> taskList){
System.out.println("Got it. I've added this task:");
System.out.println(taskList.get(taskList.size() - 1).toString());
System.out.println("Now you have " + taskList.size() + " tasks in the list.\n");
}

public void taskRemoved(ArrayList<Task> taskList, int taskNumber){
System.out.println("Noted. I've removed this task:\n" + taskList.get(taskNumber).toString());
System.out.println("Now you have " + (taskList.size()-1) + " tasks in the list.\n");
}

public void taskDone(ArrayList<Task> taskList, int taskNumber){
System.out.println("Nice! I've marked this task as done:");
System.out.println(taskList.get(taskNumber).toString() + "\n");
}


}

0 comments on commit e1a34f1

Please sign in to comment.