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

[Lee Zhi Zhong Moses] iP #227

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Deadline extends Task{
protected String dueDate;

public Deadline(String description, String dueBy) {
super(description);
this.dueDate = dueBy;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (due by: " + dueDate + ")";
}
}
86 changes: 86 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,96 @@
import java.util.Scanner;
import java.util.Arrays;
public class Duke {

private static Scanner in = new Scanner(System.in);
private static final 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.

For program scalability, you might want to consider using ArrayList instead of setting a maximum number of tasks user can have here.


public static void command(String line) {
Copy link

Choose a reason for hiding this comment

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

A better naming practices for methods is to start it with a verb. For example, you can call this executeCommand or something better here. (While the word command can be a verb too, it doesn't suit your usage very well)

String exitCommand = "bye";
String toDo = "list";
String checkAsDone = "mark";
Copy link

Choose a reason for hiding this comment

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

You might want to consider converting them into named constants instead of a variables since it is unlikely for them to be changed.
E.g. public static final String EXIT_COMMAND = "bye";

int numberOfTasks = 0;

String[] tokens = line.split(" ", 2);
String command = tokens[0];

while (!command.equals(exitCommand)) {
Copy link

Choose a reason for hiding this comment

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

Currently, this entire method is too long. You can try to keep a method short by extracting its content out to other methods whenever appropriate.

if (tokens[0].equals(toDo)) {
System.out.println("Here are your list of tasks to complete!");
for (int i = 0; i < numberOfTasks; ++i) {
System.out.format("%d. ", (i + 1));
System.out.println(tasks[i]);
}
line = in.nextLine();
tokens = line.split(" ", 2);
command = tokens[0];
} else if (command.equals(checkAsDone)){
int finishedTask = Integer.parseInt(tokens[1]) - 1;
tasks[finishedTask].markAsDone();
System.out.println("Alright! I have marked the task as complete!");

for (int i = 0; i < numberOfTasks; ++i) {
System.out.format("%d. ", (i + 1));
System.out.println(tasks[i]);
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 use of polymorphism here. You can apply a similar concept when dealing with similar scenarios in further levels.

}

line = in.nextLine();
tokens = line.split(" ", 0);
command = tokens[0];
} else {
String task = tokens[1];
switch(command) {
case "todo":
Task t = new ToDo(task);
Copy link

Choose a reason for hiding this comment

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

We want to avoid using a single character as the name of a variable (with some exceptions such as i, j, k in a for loop). Try to think if another name for this

tasks[numberOfTasks] = t;
tasks[numberOfTasks].isDone = false;
++numberOfTasks;
break;
case "deadline":
String[] taskDeadline = task.split("/", 2);
String taskDescription = taskDeadline[0];
String dueBy = taskDeadline[1];

Task d = new Deadline(taskDescription, dueBy);
tasks[numberOfTasks] = d;
tasks[numberOfTasks].isDone = false;
++numberOfTasks;
break;
case "event":
String[] taskEvent = task.split("/", 3);
String eventDescription = taskEvent[0];
String start = taskEvent[1];
String end = taskEvent[2];

Task e = new Event(eventDescription, start, end);
tasks[numberOfTasks] = e;
tasks[numberOfTasks].isDone = false;
++numberOfTasks;
break;
default:
System.out.println("Sorry! There seems to be an error:( Please try again.");
break;
}
System.out.println("added: " + line);
line = in.nextLine();
tokens = line.split(" ", 0);
command = tokens[0];
}
}
System.out.println("Bye! Hope to see you again");
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("Hi! My name is Duke. \n Nice to meet you!");

String line;
line = in.nextLine();
command(line);
}
}
15 changes: 15 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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 String toString() {
return "[E]" + super.toString() + " (from: " + start + " to " + end + ")";
}
}
21 changes: 21 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Task {
protected String description;
protected boolean isDone;

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

public String getStatus() {
return (isDone ? "[X]" : "[ ]"); // mark done task with X
Copy link

Choose a reason for hiding this comment

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

You might want to capitalise the first character in your comment.

}

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

public String toString() {
return getStatus() + " " + description;
}
}
11 changes: 11 additions & 0 deletions src/main/java/ToDo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class ToDo extends Task {

public ToDo(String description) {
super(description);
}

@Override
public String toString() {
return "[T]" + super.toString();
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 [T] 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.

}
}