-
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
[Lee Zhi Zhong Moses] iP #227
base: master
Are you sure you want to change the base?
Changes from 8 commits
9f810e6
dccfb91
a5f952e
a60298c
3262052
72c59ce
3242102
2fbc568
07bc4d5
f35b087
f4e64e5
5fbe514
11fed99
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 |
---|---|---|
@@ -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 + ")"; | ||
} | ||
} |
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]; | ||
|
||
public static void command(String line) { | ||
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. A better naming practices for methods is to start it with a verb. For example, you can call this |
||
String exitCommand = "bye"; | ||
String toDo = "list"; | ||
String checkAsDone = "mark"; | ||
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 might want to consider converting them into named constants instead of a variables since it is unlikely for them to be changed. |
||
int numberOfTasks = 0; | ||
|
||
String[] tokens = line.split(" ", 2); | ||
String command = tokens[0]; | ||
|
||
while (!command.equals(exitCommand)) { | ||
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. 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]); | ||
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 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); | ||
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. 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); | ||
} | ||
} |
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 + ")"; | ||
} | ||
} |
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 | ||
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 might want to capitalise the first character in your comment. |
||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public String toString() { | ||
return getStatus() + " " + description; | ||
} | ||
} |
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(); | ||
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 |
||
} | ||
} |
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.
For program scalability, you might want to consider using
ArrayList
instead of setting a maximum number of tasks user can have here.