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

[Nguyen Quang Anh] iP #202

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6118ea3
Level-0
quanganh2810 Jan 25, 2023
9877b90
Add ability to echo input
quanganh2810 Jan 25, 2023
bc1e77d
Add the ability to read and store user commands
quanganh2810 Jan 26, 2023
012958e
Added Task and TaskManager as the class, add Duke's abiity to record,…
quanganh2810 Jan 27, 2023
ba5a640
fix some variable-naming issues and fix further formatting issues in …
quanganh2810 Jan 30, 2023
09c76af
Add Deadline and Event as subclasses
quanganh2810 Feb 4, 2023
dc4925f
fix commit
quanganh2810 Feb 4, 2023
97f41d1
Tidy up the Duke class a little bit by refactoring and shorter print …
quanganh2810 Feb 4, 2023
af773e9
Fix error of not tracking Deadline and Event java files/ added toStri…
quanganh2810 Feb 6, 2023
452032b
Added exceptions to catch error inputs
quanganh2810 Feb 6, 2023
d47c75d
Change the way Duke says goodbye
quanganh2810 Feb 6, 2023
724ee0b
Merge branch 'master' into branch-Level-5
quanganh2810 Feb 6, 2023
4e75a59
change the way Duke says goodbye
quanganh2810 Feb 6, 2023
3cf1411
Create package called "Duke" to store all of the classes
quanganh2810 Feb 6, 2023
c209898
Changing names to comply with the coding standards
quanganh2810 Feb 27, 2023
894b7aa
Add the ability to remove tasks and implement using ArrayList
quanganh2810 Feb 27, 2023
dc3db0f
Add ability to write and read to harddrive
quanganh2810 Feb 27, 2023
b6a0710
Merge branch 'branch-Level-7'
quanganh2810 Feb 27, 2023
5fe873f
Add the ability to read and save data in a file called load.txt properly
quanganh2810 Feb 28, 2023
99531f8
Create extra classes such that the project is more OOP - Parser, UI a…
quanganh2810 Feb 28, 2023
21db370
Add the ability to find task
quanganh2810 Feb 28, 2023
08da801
Add documentation
quanganh2810 Mar 2, 2023
1aaf326
no message
quanganh2810 Mar 2, 2023
c00debc
Merge branch 'branch-A-JavaDoc'
quanganh2810 Mar 2, 2023
c9abb22
Updated the readme file
quanganh2810 Mar 2, 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
src/main/java/Duke.class
src/main/java/Task.class
src/main/java/TaskManager.class
*.class
150 changes: 149 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,158 @@

import java.util.Scanner;

import duke.TaskManager;

public class Duke {
public static void main(String[] args) {

public static void printInstructions() {
System.out.println("LIST OF ALL COMMANDS:");
System.out.println("todo + \"task name\" to add a task");
System.out.println("deadline + \"task name\" + /by \"task deadline\" to add a task with a deadline");
System.out.println(
"event + \"event name\" + /from \"event start time\" + /to \"event finish time\" to add an event with a stant and finish time");
System.out.println("list to list all events and tasks available");
System.out.println("mark + \"task number\" to mark a task");
System.out.println("mark + \"task number\" to unmark a task");
}

public static void printHorizontalLine() {
for (int i = 0; i <= 30; i++) {
System.out.print("_");
}
System.out.println();
}

public static void falseInput() {
System.out.println("Sorry Duke could not understand your input :> please follow the instructions");
printInstructions();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method's name should start with a verb for example announceFalseInput. Same with firstWord method below


public static String firstWord(String s) {
String a[] = s.split(" ");
return a[0];
}

public static void executeAddTodo(String s, int taskId, TaskManager listofItems) {
try {
s = s.substring("todo ".length(), s.length());
listofItems.addTask(s, taskId);
System.out.println("Roger. The following todo has been added:");
System.out.println("[T][ ] " + s);
System.out.println("You now have " + (taskId + 1) + " item in the list");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Missing todo item. Please read instructions again");
printInstructions();
}
}

public static void executeAddDeadline(String s, int taskId, TaskManager listofItems) {
try {
s = s.substring("deadline ".length(), s.length());
String[] cmd = s.split(" /by ");
if (cmd.length > 2) {
System.out.println("Extra deadline found! Please try again!");
printInstructions();
return;
}
listofItems.addDeadline(cmd[0], cmd[1], taskId);
System.out.println("Roger. The following deadline has been added:");
System.out.println("[D][ ] " + cmd[0] + " (by: " + cmd[1] + ")");
System.out.println("You now have " + (taskId + 1) + " item in the list");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Your task name is missing please try again!");
printInstructions();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Your deadline is missing please try again!");
printInstructions();
}

}

public static void executeAddEvent(String s, int taskId, TaskManager listofItems) {
try {
s = s.substring("event ".length(), s.length());
String[] cmd = s.split(" /from ");
String startTime = cmd[1].split(" /to ")[0];
String endTime = cmd[1].split(" /to ")[1];
listofItems.addEvent(cmd[0], startTime, endTime, taskId);
System.out.println("Roger. The following event has been added:");
System.out.println("[E][ ] " + cmd[0] + " (from: " + startTime + " to: " + endTime + ")");
System.out.println("You now have " + (taskId + 1) + " item in the list");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("Your task name is missing please try again!");
printInstructions();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Something is wrong with your event's start time or finish time please try again!");
printInstructions();
}
}

private static void printGreeting() {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("Hello! I'm Duke");
System.out.println("What can I do for you ?");
printHorizontalLine();
printInstructions();
printHorizontalLine();
}

public static void main(String[] args) {
printGreeting();
Scanner scanObj = new Scanner(System.in);
TaskManager listofItems = new TaskManager();
String userCmd = scanObj.nextLine();
int taskId = 0;
while (!userCmd.equals("bye")) {
switch (firstWord(userCmd)) {
case "todo":
executeAddTodo(userCmd, taskId, listofItems);
printHorizontalLine();
taskId++;
break;
case "deadline":
executeAddDeadline(userCmd, taskId, listofItems);
printHorizontalLine();
taskId++;
break;
case "event":
executeAddEvent(userCmd, taskId, listofItems);
printHorizontalLine();
taskId++;
break;
case "list":
listofItems.listTask();
printHorizontalLine();
break;
case "mark":
String markId[] = userCmd.split(" ");
listofItems.markTask(Integer.parseInt(markId[1]) - 1);
printHorizontalLine();
break;
case "unmark":
String unmarkId[] = userCmd.split(" ");
listofItems.unmarkTask(Integer.parseInt(unmarkId[1]) - 1);
printHorizontalLine();
break;
default:
falseInput();
printHorizontalLine();
}
userCmd = scanObj.nextLine();
}
scanObj.close();
printGoodbye();
}

private static void printGoodbye() {
System.out.println("Thanks for using Duke! See ya!");
System.out.println(" /\\_/\\ ");
System.out.println("( o.o ) ");
System.out.println(" > ^ < ");
}
}
25 changes: 25 additions & 0 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package duke;
public class Deadline extends Task {
protected String by;

public Deadline(String name, boolean isDone, int taskId, String by) {
super(name, isDone, taskId);
this.by = by;
}

public String toString() {
if(this.getIsDone() == true) {
return " [D][X]" + this.getName() + " (by: " + this.by + ")";
} else {
return " [D][ ]" + this.getName() + " (by: " + this.by + ")";
}
}

public void print() {
if (this.isIsDone() == false) {
System.out.println((this.getTaskId() + 1) + "." + this.toString());
} else {
System.out.println((this.getTaskId() + 1) + "." + this.toString());
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package duke;
public class Event extends Task {
protected String startTime;
protected String finishTime;

public Event(String name, boolean isDone, int taskId, String startTime, String finishTime) {
super(name, isDone, taskId);
this.startTime = startTime;
this.finishTime = finishTime;
}

public String toString() {
if(this.getIsDone() == true) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add space between if and condition statement

return " [E][X]" + this.getName() + " (from: " + this.startTime
+ " to: " + this.finishTime + ")";
} else {
return " [E][ ]" + this.getName() + " (from: " + this.startTime
+ " to: " + this.finishTime + ")";
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else block are unnecessary. You can move the code inside else block out and put it after if block. Same with other if else block

}

public void print() {
if (this.isIsDone() == false) {
System.out.println((this.getTaskId() + 1) + "." + this.toString());
} else {
System.out.println((this.getTaskId() + 1) + "." + this.toString());
}
}
}
70 changes: 70 additions & 0 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package duke;

public class Task {
private String name;
private boolean isDone;
private int taskId;
private char taskType;

public boolean isIsDone() {
return this.isDone;
}

public char getTaskType() {
return this.taskType;
}

public void setTaskType(char taskType) {
this.taskType = taskType;
}

public Task(String name, boolean isDone, int taskId) {
this.name = name;
this.isDone = isDone;
this.taskId = taskId;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public boolean isDone() {
return this.isDone;
}

public boolean getIsDone() {
return this.isDone;
}

public void setIsDone(boolean isDone) {
this.isDone = isDone;
}

public int getTaskId() {
return this.taskId;
}

public void setTaskId(int taskId) {
this.taskId = taskId;
}

public String toString() {
if(this.isDone == true) {
return " [T][X]" + this.name;
} else {
return " [T][ ]" + this.name;
}
}

public void print() {
if (this.isDone == false) {
System.out.println((this.taskId + 1) + "." + this.toString());
} else {
System.out.println((this.taskId + 1) + "." + this.toString());
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/duke/TaskManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package duke;
public class TaskManager {
private Task[] tasks = new Task[100];
private int taskCount = 0;

public void addTask(String name, int id) {
tasks[taskCount] = new Task(name, false, id);
taskCount++;
}

public void addDeadline(String name, String deadline, int id) {
tasks[taskCount] = new Deadline(name, false, id, deadline);
taskCount++;
}

public void addEvent(String eventName, String startTime, String finishTime, int id) {
tasks[taskCount] = new Event(eventName, false, id, startTime, finishTime);
taskCount++;
}

public void markTask(int id) {
tasks[id].setIsDone(true);
System.out.println("The task has been marked as done!");
System.out.println("[X] " + tasks[id].getName());
}

public void unmarkTask(int id) {
tasks[id].setIsDone(false);
System.out.println("The task has been marked as NOT done!");
System.out.println("[ ] " + tasks[id].getName());
}

public void listTask() {
for (int i = 0; i < taskCount; i++) {
tasks[i].print();
}
}
}