-
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
[javienneyeo] ip #215
base: master
Are you sure you want to change the base?
[javienneyeo] ip #215
Changes from 9 commits
2133998
aebbd76
c9dce36
7c733ca
1aa5dc0
cd54a1e
33428da
983890b
fe58087
d919bc1
94c8e08
f0554aa
1792a0b
e067a36
dde6faf
a3e5838
8bb6a12
48805a0
a27c740
b4f2ebf
4e684b7
e6b9e70
54766f3
1474f6e
a1ba4ea
ac1e054
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,138 @@ | ||
import duke.tasks.Task; | ||
import duke.tasks.Deadline; | ||
import duke.tasks.Event; | ||
import duke.tasks.Todo; | ||
import duke.exceptions.EmptyDescriptionException; | ||
import duke.exceptions.TaskToMarkDoesNotExistException; | ||
import duke.exceptions.UnknownCommandException; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
private static void displayList(Task[] tasks) { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < Task.numberOfTasks; i += 1) { | ||
System.out.print(i + 1 + ". "); | ||
tasks[i].printTask(); | ||
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. This is a good usage of polymorphism. When you are implementing the features in future levels, you might want to think about using similar techniques for other appropriate features too. |
||
} | ||
} | ||
|
||
private static void markTask(Task[] tasks, String task) { | ||
tasks[Integer.parseInt(task) - 1].setDone(); | ||
System.out.println("Nice! I've marked this task as done:"); | ||
displayList(tasks); | ||
} | ||
|
||
private static void unmarkTask(Task[] tasks, String task) { | ||
tasks[Integer.parseInt(task) - 1].setUndone(); | ||
System.out.println("OK, I've marked this task as not done yet:"); | ||
displayList(tasks); | ||
} | ||
|
||
private static void createTodo(Task[] tasks, String task) { | ||
tasks[Task.numberOfTasks] = new Todo(task); | ||
System.out.println("Got it. I've added this task:"); | ||
tasks[Task.numberOfTasks - 1].printTask(); | ||
System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); | ||
} | ||
|
||
private static void createEvent(Task[] tasks, String task) { | ||
String[] words = task.split("/from"); | ||
String description = words[0]; | ||
String[] words2 = words[1].split("/to"); | ||
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. Instead of calling it words and words2, you might want to think of other more related or intuitive namings for them |
||
String start = words2[0]; | ||
String end = words2[1]; | ||
tasks[Task.numberOfTasks] = new Event(description, start, end); | ||
System.out.println("Got it. I've added this task:"); | ||
tasks[Task.numberOfTasks - 1].printTask(); | ||
System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); | ||
} | ||
|
||
private static void createDeadline(Task[] tasks, String task) { | ||
String[] words = task.split("/by"); | ||
String description = words[0]; | ||
System.out.println(words[0]); | ||
System.out.println(words[1]); | ||
String end = words[1]; | ||
tasks[Task.numberOfTasks] = new Deadline(description, end); | ||
System.out.println("Got it. I've added this task:"); | ||
tasks[Task.numberOfTasks - 1].printTask(); | ||
System.out.println("Now you have " + Task.numberOfTasks + " tasks in your list."); | ||
} | ||
|
||
private static String[] getInput() { | ||
Scanner input = new Scanner(System.in); | ||
String text = input.nextLine(); // input the whole sentence into text | ||
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. Generally, we would want to capitalise the first letter in our comment. |
||
return text.split(" ", 2); | ||
} | ||
|
||
private static void editList() throws UnknownCommandException, EmptyDescriptionException, TaskToMarkDoesNotExistException { | ||
String[] splitText = getInput(); | ||
Task[] tasks = new Task[100]; | ||
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. Your current code structure only accepts a maximum of 100 tasks for now. Moving on, you might want to make this more flexible by using ArrayList. |
||
Task.numberOfTasks = 0; | ||
while (!splitText[0].equals("bye")) { | ||
switch (splitText[0]) { | ||
case "mark": | ||
try { | ||
markTask(tasks, splitText[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new EmptyDescriptionException("marked"); | ||
} catch (NullPointerException e) { | ||
throw new TaskToMarkDoesNotExistException("mark"); | ||
} | ||
break; | ||
case "unmark": | ||
try { | ||
unmarkTask(tasks, splitText[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new EmptyDescriptionException("unmarked"); | ||
} catch (NullPointerException e) { | ||
throw new TaskToMarkDoesNotExistException("unmark"); | ||
} | ||
break; | ||
case "list": | ||
displayList(tasks); | ||
break; | ||
case "todo": | ||
try { | ||
createTodo(tasks, splitText[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new EmptyDescriptionException("todo"); | ||
} | ||
break; | ||
case "deadline": | ||
try { | ||
createDeadline(tasks, splitText[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new EmptyDescriptionException("deadline"); | ||
} | ||
break; | ||
case "event": | ||
try { | ||
createEvent(tasks, splitText[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new EmptyDescriptionException("deadline"); | ||
} | ||
break; | ||
default: | ||
throw new UnknownCommandException(); | ||
} | ||
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. Good use of default branch. |
||
splitText = getInput(); | ||
} | ||
System.out.println("Bye! Hope to see you again soon!"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
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?"); | ||
try { | ||
editList(); | ||
} catch (UnknownCommandException e) { | ||
System.out.println("OOPS!! I'm sorry but I don't know what that means :-("); | ||
} catch (EmptyDescriptionException e) { | ||
e.printErrorMessage(); | ||
} catch (TaskToMarkDoesNotExistException e) { | ||
e.printErrorMessage(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package duke.exceptions; | ||
|
||
public class EmptyDescriptionException extends Exception { | ||
protected String typeOfTask; | ||
|
||
public EmptyDescriptionException (String typeOfTask) { | ||
this.typeOfTask = typeOfTask; | ||
} | ||
public void printErrorMessage() { | ||
if (typeOfTask.equals("marked") || typeOfTask.equals("unmarked")) { | ||
System.out.println("OOPS!! Task to be " + typeOfTask + " was not specified!"); | ||
} else { | ||
System.out.println("OOPS!! The description of " + typeOfTask + " cannot be empty!"); | ||
} | ||
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. Instead of checking whether it is marked or unmarked and proceed to print a different error message for it, you might want to just create another exception separately for the mark and unmark feature. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package duke.exceptions; | ||
|
||
import duke.tasks.Task; | ||
|
||
public class TaskToMarkDoesNotExistException extends Exception { | ||
protected String command; | ||
|
||
public TaskToMarkDoesNotExistException(String command) { | ||
this.command = command; | ||
} | ||
|
||
public void printErrorMessage() { | ||
System.out.println("You can only " + command + " tasks that are currently in the list!"); | ||
System.out.println("You only have " + Task.numberOfTasks + " tasks in your list."); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package duke.exceptions; | ||
|
||
public class UnknownCommandException extends Exception { | ||
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. Instead of having the content of the exception empty, you can consider doing some interesting and useful stuff within the exception, such as declaring a function to print the error message. |
||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package duke.tasks; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected String end; | ||
public Deadline(String description, String end) | ||
{ | ||
super(description); | ||
this.end = end; | ||
} | ||
|
||
@Override | ||
public void printTask() { | ||
System.out.println("[D][" + getStatusIcon() + "] " + description + "(by:" + end + ")"); | ||
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. Arguably, strings like |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package duke.tasks; | ||
|
||
public class Event extends Task { | ||
protected String start; | ||
protected String end; | ||
public Event(String description, String start, String end) { | ||
super(description); | ||
this.start = start; | ||
this.end = end; | ||
} | ||
|
||
@Override | ||
public void printTask() { | ||
System.out.println("[E][" + getStatusIcon() + "] " + description + "(from:" + start + "to:" + end + ")"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package duke.tasks; | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
public static int numberOfTasks; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
numberOfTasks += 1; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public void setDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void setUndone() { | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); // mark done task with X | ||
} | ||
|
||
public void printTask() { | ||
System.out.println("task"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package duke.tasks; | ||
|
||
public class Todo extends Task { | ||
|
||
public Todo (String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public void printTask() { | ||
System.out.println("[T][" + getStatusIcon() + "] " + description); | ||
} | ||
} |
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.
I noticed that your Duke.java file is outside the
duke
folder. You might want to move it into that folder such that all your code is under the folder. You could use themove class
feature under refactor to achieve it.