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

[javienneyeo] ip #215

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2133998
Greet and exit
javienneyeo Jan 31, 2023
aebbd76
Greet, echo and exit
javienneyeo Jan 31, 2023
c9dce36
Add elements into a list
javienneyeo Feb 1, 2023
7c733ca
added list to store items
javienneyeo Feb 3, 2023
1aa5dc0
added mark as done functionality
javienneyeo Feb 3, 2023
cd54a1e
added todo, event and deadline
javienneyeo Feb 8, 2023
33428da
handle exceptions in commands
javienneyeo Feb 15, 2023
983890b
shift classes into packages
javienneyeo Feb 15, 2023
fe58087
merge branch-A-Packages
javienneyeo Feb 15, 2023
d919bc1
added delete task functionality
javienneyeo Feb 26, 2023
94c8e08
Merge branch 'branch-Level-6'
javienneyeo Feb 26, 2023
f0554aa
store the data in txt file
javienneyeo Feb 27, 2023
1792a0b
cleaned up code for storage
javienneyeo Feb 27, 2023
e067a36
resolve merge conflict
javienneyeo Feb 27, 2023
dde6faf
merge 'branch-Level-7'
javienneyeo Feb 27, 2023
a3e5838
added Ui class
javienneyeo Feb 28, 2023
8bb6a12
added Parser and TaskList classes
javienneyeo Feb 28, 2023
48805a0
added find task functionality
javienneyeo Feb 28, 2023
a27c740
Merge pull request #1 from javienneyeo/branch-Level-9
javienneyeo Feb 28, 2023
b4f2ebf
added documentation to the code
javienneyeo Mar 3, 2023
4e684b7
added user guide
javienneyeo Mar 3, 2023
e6b9e70
edited user guide
javienneyeo Mar 3, 2023
54766f3
changed the alignment of user guide
javienneyeo Mar 3, 2023
1474f6e
changed duke to expected output
javienneyeo Mar 3, 2023
a1ba4ea
dealt with a few more exceptions
javienneyeo Mar 3, 2023
ac1e054
changed code for format task
javienneyeo Mar 3, 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
144 changes: 136 additions & 8 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,138 @@
import duke.tasks.Task;
Copy link

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 the move class feature under refactor to achieve it.

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();
Copy link

Choose a reason for hiding this comment

The 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");
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The 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];
Copy link

Choose a reason for hiding this comment

The 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();
}
Copy link

Choose a reason for hiding this comment

The 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();
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/exceptions/EmptyDescriptionException.java
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!");
}
Copy link

Choose a reason for hiding this comment

The 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.

}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/exceptions/TaskToMarkDoesNotExistException.java
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.");
}
}
6 changes: 6 additions & 0 deletions src/main/java/duke/exceptions/UnknownCommandException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package duke.exceptions;

public class UnknownCommandException extends Exception {
Copy link

Choose a reason for hiding this comment

The 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.



}
16 changes: 16 additions & 0 deletions src/main/java/duke/tasks/Deadline.java
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 + ")");
Copy link

Choose a reason for hiding this comment

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

Arguably, strings like [D] can be seen as a magic strings. You might want to consider extracting strings like this into a named constants. Sometimes, you could also use enums to achieve similar effect. There could be other magic strings/numbers/literals in your other code too that you might want to look out for them.

}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/tasks/Event.java
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 + ")");
}
}
33 changes: 33 additions & 0 deletions src/main/java/duke/tasks/Task.java
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");
}
}
13 changes: 13 additions & 0 deletions src/main/java/duke/tasks/Todo.java
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);
}
}