-
Notifications
You must be signed in to change notification settings - Fork 200
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
base: master
Are you sure you want to change the base?
Changes from 14 commits
6118ea3
9877b90
bc1e77d
012958e
ba5a640
09c76af
dc4925f
97f41d1
af773e9
452032b
d47c75d
724ee0b
4e75a59
3cf1411
c209898
894b7aa
dc3db0f
b6a0710
5fe873f
99531f8
21db370
08da801
1aaf326
c00debc
c9abb22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
|
||
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(" > ^ < "); | ||
} | ||
} |
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()); | ||
} | ||
} | ||
} |
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 + ")"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
} | ||
} | ||
} |
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()); | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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