Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
556af3f
Add Gradle support
May 24, 2020
98b0b8b
Level 1 complete
Jan 19, 2023
b286d41
Level 2 complete
Jan 19, 2023
29e0afb
Level 2 complete
Jan 19, 2023
1f85f96
Level 3 complete
Jan 19, 2023
0e983da
Level 4 complete
Jan 20, 2023
9039f8e
Level 5 complete
Jan 20, 2023
58c0f95
Level 5 complete
Jan 20, 2023
0c4a800
Level 6 complete
Jan 20, 2023
a3d3477
Complete Level 7
Feb 1, 2023
9b818a7
Complete Level 8
Feb 3, 2023
f6fda6f
Merge branch 'branch-level-7'
Feb 3, 2023
24786a7
Complete Level 8
Feb 3, 2023
4f67fd1
Resolve conflict between level 8 and master
Feb 3, 2023
cfb9c77
Complete A-MoreOOP
Feb 4, 2023
b678e36
Complete A-Packages
Feb 4, 2023
e7dbf2d
Merge branch 'add-gradle-support'
Feb 4, 2023
a5dd3ce
Complete A-Gradle
Feb 6, 2023
b16ac23
Complete A-JUnit
Feb 6, 2023
04c4fe7
Complete A-Jar
Feb 6, 2023
e18d496
Complete A-JavaDoc
Feb 6, 2023
217c71b
Complete A-CodingStandard
Feb 6, 2023
9846241
Complete Level-9
Feb 6, 2023
0f0592c
Complete level 10
Feb 19, 2023
7a5165e
Complete level 10
Feb 19, 2023
35f3de5
Complete A-Varargs
Feb 19, 2023
a3698bd
Add images
Feb 20, 2023
94aa96a
Complete A-Assertions
Feb 20, 2023
6fdfd7e
Complete A-CodeQuality
Feb 20, 2023
b0a88ed
Merge pull request #2 from Zhongli5712/branch-A-Assertions
Zhongli5712 Feb 20, 2023
10e8b1e
Merge branch 'master' into branch-A-CodeQuality
Feb 20, 2023
9ec8791
Merge pull request #3 from Zhongli5712/branch-A-CodeQuality
Zhongli5712 Feb 20, 2023
a352bac
Complete BCD-Exetension
Feb 20, 2023
c5c2081
Merge pull request #4 from Zhongli5712/branch-BCD-Extension
Zhongli5712 Feb 20, 2023
4ba6b4d
Fix some error
Feb 20, 2023
7db313a
Complete A-UserGuide
Feb 20, 2023
2e7d8bc
Trigger rebuild
Feb 20, 2023
68dc253
Fix Readme
Feb 20, 2023
f4f17f8
Make Duke compatible with Linux
Feb 28, 2023
4a64ff0
Update Readme.md
Mar 2, 2023
1a8a463
Update README.md
Zhongli5712 Mar 2, 2023
4692bc5
Update README.md
Zhongli5712 Mar 7, 2023
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
51 changes: 51 additions & 0 deletions src/main/java/ChatBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
public class ChatBot {
private TodoList todoList;

private enum Instructions {todo, deadline, event, mark, unmark, delete}

public ChatBot(TodoList todoList) {
this.todoList = todoList;
}

public void execute(String command) throws DukeExceptions {
String[] split_command = command.split(" ", 2);
String instruction = split_command[0];

if (instruction.equals("list")) {
if (split_command.length == 1) {
String shown_list = todoList.toString();
System.out.println(shown_list);
return;
} else {
throw new DukeExceptions("OOPS!!! The description of a list cannot have other parameters");
}
}

//check for valid instructions
for (Instructions validInstruction : Instructions.values()) {
if (validInstruction.name().equals(instruction)) {
if (split_command.length == 1) {
throw new DukeExceptions(String.format("OOPS!!! The description of a %s cannot be empty.", instruction));
} else if (instruction.equals("mark")) {
int digit = Integer.parseInt(split_command[1]);
todoList.mark(digit);
return;
} else if (instruction.equals("unmark")) {
int digit = Integer.parseInt(split_command[1]);
todoList.unmark(digit);
return;
} else if (instruction.equals("delete")) {
int digit = Integer.parseInt(split_command[1]);
todoList.delete(digit);
System.out.println(String.format("Now I have %d tasks in the list.", todoList.number_of_tasks()));
return;
} else {
todoList.add(instruction, split_command[1]);
System.out.println(String.format("Now I have %d tasks in the list.", todoList.number_of_tasks()));
return;
}
}
}
throw new DukeExceptions("OOPS!!! I'm sorry, but I don't know what that means.");
}
}
49 changes: 43 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
import java.util.Scanner;

public class Duke {


public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);


begin();

TodoList todoList = new TodoList();
ChatBot bot = new ChatBot(todoList);

Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();

while (!input.equals("bye")) {
try {
bot.execute(input);
} catch (DukeExceptions error) {
System.out.println(error.getErrorMessage());
}
endCommand();
input = scanner.nextLine(); //ready for next input
}
scanner.close();
bye();
}

private static void begin() {
endCommand();

System.out.println("Hello from Duke");
System.out.println("What can I do for you?");

endCommand();
}

private static void endCommand() {
System.out.println("____________________________________________________________");
}

private static void bye() {
System.out.println("Bye. Hope to see you again soon!");
endCommand();
}
}
10 changes: 10 additions & 0 deletions src/main/java/DukeExceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class DukeExceptions extends Exception{
private String error;
public DukeExceptions(String error) {
this.error = error;
}

public String getErrorMessage() {
return this.error;
}
}
93 changes: 93 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
public class Task {
private Boolean isDone;

public Task() {
this.isDone = false;
}

public Task createNewTask(String type, String task) throws DukeExceptions{
if (type.equals("todo")) {
return new Todo(task);
} else if (type.equals("deadline")) {
return new Deadline(task);
} else if (type.equals("event")) {
return new Event(task);
} else {
System.out.println("unknown command! Please try again.");
return null;
}
}

public void markTask() {
this.isDone = true;
}

public void unmarkTask() {
this.isDone = false;
}


class Todo extends Task {
private String task;

public Todo(String task) {
super();
this.task = task;
}

@Override
public String toString() {
String name = super.isDone ? "[T][X] " + task : "[T][ ] " + task;
return name;
}
}

class Deadline extends Task {
private String task;
private String deadline;

public Deadline(String task) throws DukeExceptions {
super();
String[] commands = task.split(" /by ");
if (commands.length == 1) {
throw new DukeExceptions("OOPS!!! Looks like someone forget his/her deadline :)\n Please use /by to indicate deadline");
}
this.task = commands[0];
this.deadline = commands[1];
}

@Override
public String toString() {
String name = super.isDone ? "[D][X] " + task + " (by: " + this.deadline +")" : "[D][ ] " + task + " (by: " + this.deadline +")";
return name;
}
}

class Event extends Task {
private String task;
private String from;
private String to;

public Event(String task) throws DukeExceptions {
super();
String[] commands = task.split(" /from ");
if (commands.length == 1) {
throw new DukeExceptions("OOPS!!! Looks like someone forget when the event begins :)\n Please use /from to indicate begin time");
}
this.task = commands[0];
String[] from_to_timeline = commands[1].split(" /to ");
if (from_to_timeline.length == 1) {
throw new DukeExceptions("OOPS!!! Looks like someone forget when the event ends :)\n Please use /to to indicate end time");
}
this.from = from_to_timeline[0];
this.to = from_to_timeline[1];
}

@Override
public String toString() {
String name = super.isDone ? "[E][X] " + task + " (from: " + this.from +" to: " + this.to + ")"
: "[E][ ] " + task + " (from: " + this.from +" to: " + this.to + ")";
return name;
}
}
}
64 changes: 64 additions & 0 deletions src/main/java/TodoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.ArrayList;

public class TodoList {
private ArrayList<Task> todo_list;

public TodoList() {
this.todo_list = new ArrayList<>(100);
}

public void add(String type, String task) throws DukeExceptions{
Task taskObject = new Task();
Task newTask = taskObject.createNewTask(type, task);
System.out.println("Got it. I've added this task:");
todo_list.add(newTask);
System.out.println(newTask);
}

public void mark(int index) throws DukeExceptions{
int todo_list_length = todo_list.size();
if (index < 0 || index > todo_list_length) {
throw new DukeExceptions("Please use list command to check the index!");
}
Task task = todo_list.get(index - 1);
task.markTask();
System.out.println("Nice! I've marked this task as done:");
System.out.println(todo_list.get(index-1));
}

public void unmark(int index) throws DukeExceptions{
int todo_list_length = todo_list.size();
if (index < 0 || index > todo_list_length) {
throw new DukeExceptions("Please use list command to check the index!");
}
Task task = todo_list.get(index - 1);
task.unmarkTask();
System.out.println("OK, I've marked this task as not done yet:");
System.out.println(todo_list.get(index-1));

}

public void delete(int index) throws DukeExceptions {
int todo_list_length = todo_list.size();
if (index < 0 || index > todo_list_length) {
throw new DukeExceptions("Please use list command to check the index!");
}
Task task = todo_list.remove(index - 1);
System.out.println("Noted. I've removed this task:");
System.out.println(task);
}

public int number_of_tasks() {
return todo_list.size();
}

@Override
public String toString() {
String shown_list = "";
int todo_list_length = todo_list.size();
for (int i = 1; i <= todo_list_length; i++) {
shown_list += String.format("%d. %s\n", i, todo_list.get(i-1));
}
return shown_list;
}
}
95 changes: 89 additions & 6 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -1,7 +1,90 @@
Hello from
____ _
| _ \ _ _| | _____
| | | | | | | |/ / _ \
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|
____________________________________________________________
Hello from Duke
What can I do for you?
____________________________________________________________
OOPS!!! I'm sorry, but I don't know what that means.
____________________________________________________________
OOPS!!! The description of a todo cannot be empty.
____________________________________________________________
Got it. I've added this task:
[T][ ] read book
Now I have 1 tasks in the list.
____________________________________________________________
OOPS!!! The description of a list cannot have other parameters
____________________________________________________________
1. [T][ ] read book

____________________________________________________________
OOPS!!! The description of a deadline cannot be empty.
____________________________________________________________
OOPS!!! Looks like someone forget his/her deadline :)
Please use /by to indicate deadline
____________________________________________________________
Got it. I've added this task:
[D][ ] return book (by: Sunday 2pm)
Now I have 2 tasks in the list.
____________________________________________________________
1. [T][ ] read book
2. [D][ ] return book (by: Sunday 2pm)

____________________________________________________________
OOPS!!! The description of a event cannot be empty.
____________________________________________________________
OOPS!!! Looks like someone forget when the event begins :)
Please use /from to indicate begin time
____________________________________________________________
OOPS!!! Looks like someone forget when the event ends :)
Please use /to to indicate end time
____________________________________________________________
Got it. I've added this task:
[E][ ] programming study (from: 5pm to: 9pm)
Now I have 3 tasks in the list.
____________________________________________________________
1. [T][ ] read book
2. [D][ ] return book (by: Sunday 2pm)
3. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
Nice! I've marked this task as done:
[T][X] read book
____________________________________________________________
1. [T][X] read book
2. [D][ ] return book (by: Sunday 2pm)
3. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
Nice! I've marked this task as done:
[D][X] return book (by: Sunday 2pm)
____________________________________________________________
1. [T][X] read book
2. [D][X] return book (by: Sunday 2pm)
3. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
OK, I've marked this task as not done yet:
[D][ ] return book (by: Sunday 2pm)
____________________________________________________________
1. [T][X] read book
2. [D][ ] return book (by: Sunday 2pm)
3. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
OOPS!!! The description of a delete cannot be empty.
____________________________________________________________
Please use list command to check the index!
____________________________________________________________
1. [T][X] read book
2. [D][ ] return book (by: Sunday 2pm)
3. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
Noted. I've removed this task:
[T][X] read book
Now I have 2 tasks in the list.
____________________________________________________________
1. [D][ ] return book (by: Sunday 2pm)
2. [E][ ] programming study (from: 5pm to: 9pm)

____________________________________________________________
Bye. Hope to see you again soon!
____________________________________________________________
Loading