Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch level 6 #305

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -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
____ _
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

158 changes: 158 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -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<Task> 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();
}
}
5 changes: 5 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package duke;

public class DukeException extends Exception {

}
19 changes: 19 additions & 0 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
@@ -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 + ")";
}
}
18 changes: 18 additions & 0 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
@@ -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 + ")";
}
}
27 changes: 27 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
40 changes: 34 additions & 6 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -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
____________________________________________________________
7 changes: 7 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
todo borrow book
list
deadline return book /by Sunday
event project /at Mon 2-4pm
list
done 2
bye
4 changes: 2 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ 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
)
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
10 changes: 3 additions & 7 deletions text-ui-test/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down