diff --git a/README.md b/README.md index 9d95025bce..da61b7869f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Duke project template +# duke project template This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it. @@ -15,7 +15,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version. 1. Click `Open or Import`. 1. Select the project directory, and click `OK` 1. If there are any further prompts, accept the defaults. -1. After the importing is complete, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()`. If the setup is correct, you should see something like the below: +1. After the importing is complete, locate the `src/main/java/duke.java` file, right-click it, and choose `Run duke.main()`. If the setup is correct, you should see something like the below: ``` Hello from ____ _ diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java deleted file mode 100644 index 5d313334cc..0000000000 --- a/src/main/java/Duke.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Duke { - public static void main(String[] args) { - String logo = " ____ _ \n" - + "| _ \\ _ _| | _____ \n" - + "| | | | | | | |/ / _ \\\n" - + "| |_| | |_| | < __/\n" - + "|____/ \\__,_|_|\\_\\___|\n"; - System.out.println("Hello from\n" + logo); - } -} diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java new file mode 100644 index 0000000000..b6a9712220 --- /dev/null +++ b/src/main/java/duke/Duke.java @@ -0,0 +1,158 @@ +package duke; + +import duke.task.Deadline; +import duke.task.Event; +import duke.task.Task; +import duke.task.Todo; + +import java.util.Scanner; +import java.util.ArrayList; + +public class Duke { + static String horizontalLine = "--------------------------------------------------"; + static int thingsCounted = 0; + //static Task[] tasks = new Task[100]; + public static ArrayList taskArray = new ArrayList<>(); + + public static void storeTextAndList() throws DukeException { + String command; + Scanner in = new Scanner(System.in); + command = in.nextLine(); + while (!command.equals("bye")) { + System.out.println(horizontalLine); + try { + if (command.equals("list")) { + listTask(); + } else if (command.contains("done")) { + int index = Integer.parseInt(command.substring(5)); + if(index > thingsCounted){ + throw new DukeException(); + } + markTaskAsDone(index); + } else if (command.contains("todo")) { + addTodoTask(command); + } else if (command.contains("deadline")) { + addDeadlineTask(command); + } else if (command.contains("event")) { + addEventTask(command); + } else if (command.contains("delete")){ + int index = Integer.parseInt(command.substring(7)); + if(index > thingsCounted){ + throw new DukeException(); + } + deleteItem(index); + } else { + throw new DukeException(); + } + }catch (DukeException e) { + dealWithException(command); + } + System.out.println(horizontalLine); + command = in.nextLine(); + } + System.out.println(horizontalLine); + System.out.println("Bye. Hope to see you again soon"); + System.out.println(horizontalLine); + } + + public static void dealWithException(String text){ + switch (text) { + case "todo": + System.out.println("☹ OOPS!!! The description of a todo cannot be empty."); + break; + case "deadline": + System.out.println("☹ OOPS!!! The description of a deadline cannot be empty."); + break; + case "event": + System.out.println("☹ OOPS!!! The description of a event cannot be empty."); + break; + default: + System.out.println(("☹ OOPS!!! I'm sorry, but I don't know what that means :-(")); + break; + } + } + + public static void addTodoTask(String text) throws DukeException { + String todoDescription; + if(text.equals("todo")){ + throw new DukeException(); + } + todoDescription = text.substring(5); + Task task = new Todo(todoDescription); + //tasks[thingsCounted] = task; + thingsCounted++; + taskArray.add(task); + printTask(task); + } + + public static void addDeadlineTask (String text) throws DukeException { + String deadlineDescription; + String deadlineByDate; + int getIndex; + if(text.equals("deadline")){ + throw new DukeException(); + } + getIndex = text.indexOf("/"); + deadlineDescription = text.substring(9, getIndex - 1); + deadlineByDate = text.substring(getIndex + 4); + Task task = new Deadline(deadlineDescription, deadlineByDate); + //tasks[thingsCounted] = task; + thingsCounted++; + taskArray.add(task); + printTask(task); + } + + public static void addEventTask(String text) throws DukeException { + String eventDescription; + String eventAtDate; + int getIndex; + if(text.equals("event")){ + throw new DukeException(); + } + getIndex = text.indexOf("/"); + eventDescription = text.substring(6, getIndex - 1); + eventAtDate = text.substring(getIndex + 4); + Task task = new Event(eventDescription, eventAtDate); + //tasks[thingsCounted] = task; + thingsCounted++; + taskArray.add(task); + printTask(task); + } + + public static void printTask(Task task){ + System.out.println("Got it. I've added this task: "); + System.out.println(task); + System.out.println("Now you have " + thingsCounted + " tasks in the list."); + } + + + public static void markTaskAsDone(int index){ + taskArray.get(index-1).taskDone(); + System.out.println("Nice! I've marked this task as done:\n" +taskArray.get(index-1)); + } + + private static void deleteItem(int index) { + Task task = taskArray.remove(index - 1); + System.out.println("Noted. I've removed this task: "); + System.out.println(task); + thingsCounted--; + System.out.println("Now you have " + thingsCounted + " tasks in the list."); + } + + public static void listTask(){ + System.out.println(horizontalLine); + System.out.println("Here are the tasks in your list:"); + int count = 1; + for(Task task : taskArray){ + System.out.print(count + ". "); + System.out.println(task); + count++; + } + System.out.println(horizontalLine); + } + + public static void main(String[] args) throws DukeException { + System.out.println(horizontalLine + "\nHello! I'm Duke\n" + "What can I do for you?\n" + horizontalLine); + storeTextAndList(); + } +} \ No newline at end of file diff --git a/src/main/java/duke/DukeException.java b/src/main/java/duke/DukeException.java new file mode 100644 index 0000000000..e7ae283939 --- /dev/null +++ b/src/main/java/duke/DukeException.java @@ -0,0 +1,5 @@ +package duke; + +public class DukeException extends Exception { + +} diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java new file mode 100644 index 0000000000..d5a7c84ed4 --- /dev/null +++ b/src/main/java/duke/task/Deadline.java @@ -0,0 +1,19 @@ +package duke.task; + +public class Deadline extends Task { + + protected String byDate; + + public Deadline(String description, String byDate) { + super(description); + this.byDate=byDate; + } + + public String getTypeIcon(){ + return "[D]"; + } + + public String toString(){ + return this.getTypeIcon() + this.getStatusIcon() + description + " (by: " + byDate + ")"; + } +} diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java new file mode 100644 index 0000000000..882bffa9a5 --- /dev/null +++ b/src/main/java/duke/task/Event.java @@ -0,0 +1,18 @@ +package duke.task; + +public class Event extends Task{ + protected String atDate; + + public Event(String description, String atDate) { + super(description); + this.atDate=atDate; + } + + public String getTypeIcon(){ + return "[E]"; + } + + public String toString(){ + return this.getTypeIcon() + this.getStatusIcon() + description + " (at: " + atDate + ")"; + } +} diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java new file mode 100644 index 0000000000..96f37f5916 --- /dev/null +++ b/src/main/java/duke/task/Task.java @@ -0,0 +1,27 @@ +package duke.task; + +public class Task { + protected String description; + protected boolean isDone; + + public Task(String description) { + this.description = description; + this.isDone = false; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getStatusIcon() { + return (isDone ? "[\u2713]" : "[\u2718]"); //return tick or X symbols + } + + public void taskDone(){ + this.isDone=true; + } + + public String toString(){ + return this.getStatusIcon() + description; + } +} diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java new file mode 100644 index 0000000000..0176ab6def --- /dev/null +++ b/src/main/java/duke/task/Todo.java @@ -0,0 +1,16 @@ +package duke.task; + +public class Todo extends Task{ + + public Todo(String description) { + super(description); + } + + public String getTypeIcon(){ + return "[T]"; + } + + public String toString(){ + return this.getTypeIcon() + this.getStatusIcon() + description; + } +} diff --git a/text-ui-test/EXPECTED.TXT b/text-ui-test/EXPECTED.TXT index 657e74f6e7..eeef2799e6 100644 --- a/text-ui-test/EXPECTED.TXT +++ b/text-ui-test/EXPECTED.TXT @@ -1,7 +1,35 @@ -Hello from - ____ _ -| _ \ _ _| | _____ -| | | | | | | |/ / _ \ -| |_| | |_| | < __/ -|____/ \__,_|_|\_\___| +____________________________________________________________ + Hello! I'm duke + What can I do for you? +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[T][✘]borrow book +Now you have 1 tasks in the list. +____________________________________________________________ +____________________________________________________________ +1. [T][✘]borrow book +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[D][✘]return book (by: Sunday) +Now you have 2 tasks in the list. +____________________________________________________________ +____________________________________________________________ +Got it. I've added this task: +[D][✘]project (at: Mon 2-4pm) +Now you have 3 tasks in the list. +____________________________________________________________ +____________________________________________________________ +1. [T][✘]borrow book +2. [D][✘]return book (by: Sunday) +3. [D][✘]project (at: Mon 2-4pm) +____________________________________________________________ +____________________________________________________________ +Nice! I've marked this task as done: +[D][✓]return book (by: Sunday) +____________________________________________________________ +____________________________________________________________ +Bye. Hope to see you again soon +____________________________________________________________ \ No newline at end of file diff --git a/text-ui-test/input.txt b/text-ui-test/input.txt index e69de29bb2..55eb8a0066 100644 --- a/text-ui-test/input.txt +++ b/text-ui-test/input.txt @@ -0,0 +1,7 @@ +todo borrow book +list +deadline return book /by Sunday +event project /at Mon 2-4pm +list +done 2 +bye \ No newline at end of file diff --git a/text-ui-test/runtest.bat b/text-ui-test/runtest.bat index d0facc6310..1f3b5a5233 100644 --- a/text-ui-test/runtest.bat +++ b/text-ui-test/runtest.bat @@ -7,7 +7,7 @@ REM delete output from previous run del ACTUAL.TXT REM compile the code into the bin folder -javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\Duke.java +javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\*.java IF ERRORLEVEL 1 ( echo ********** BUILD FAILURE ********** exit /b 1 @@ -15,7 +15,7 @@ IF ERRORLEVEL 1 ( REM no error here, errorlevel == 0 REM run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ..\bin Duke < input.txt > ACTUAL.TXT +java -classpath ..\bin duke.Duke < input.txt > ACTUAL.TXT REM compare the output to the expected output FC ACTUAL.TXT EXPECTED.TXT diff --git a/text-ui-test/runtest.sh b/text-ui-test/runtest.sh index e169618a34..d3aaadd290 100644 --- a/text-ui-test/runtest.sh +++ b/text-ui-test/runtest.sh @@ -13,21 +13,17 @@ then fi # compile the code into the bin folder, terminates if error occurred -if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/Duke.java +if ! javac -cp ../src -Xlint:none -d ../bin ../src/main/java/duke/*.java then echo "********** BUILD FAILURE **********" exit 1 fi # run the program, feed commands from input.txt file and redirect the output to the ACTUAL.TXT -java -classpath ../bin Duke < input.txt > ACTUAL.TXT - -# convert to UNIX format -cp EXPECTED.TXT EXPECTED-UNIX.TXT -dos2unix ACTUAL.TXT EXPECTED-UNIX.TXT +java -classpath ../bin duke/Duke < input.txt > ACTUAL.TXT # compare the output to the expected output -diff ACTUAL.TXT EXPECTED-UNIX.TXT +diff ACTUAL.TXT EXPECTED.TXT if [ $? -eq 0 ] then echo "Test result: PASSED"