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

[ngyongjian] iP #208

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
14 changes: 14 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Deadline extends Task {
protected String deadlineDay;

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

@Override
public String toString(){
return "[D]"+super.toString()+" ("+this.deadlineDay+")";
}

}
Binary file added src/main/java/Duke.class
Binary file not shown.
66 changes: 66 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -6,5 +9,68 @@ public static void main(String[] args) {
+ "| |_| | |_| | < __/\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?\n");
ArrayList<Task> tasks = new ArrayList<Task>();

Choose a reason for hiding this comment

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

I like how you used a resizable array to store the tasks instead of a fixed size of 100.

try (Scanner scan = new Scanner(System.in)) {

Choose a reason for hiding this comment

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

Please follow the Java coding standards when using exceptions.

Suggested change
try (Scanner scan = new Scanner(System.in)) {
try {
Scanner scan = new Scanner(System.in)

String input = scan.nextLine();

Choose a reason for hiding this comment

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

"Arrow Head" code, maybe you can consider avoiding deep nesting?

String output = new String();
while (!input.equals("bye")) {
if (input.equals("list")) {
output = "Here are the tasks in the list:" + System.lineSeparator() + Task.printTasksList(tasks);

} else if (input.matches("mark [0-9]{1,2}")) {
String[] marks = input.split(" ");
tasks.get(Integer.parseInt(marks[1]) - 1).markAsDone();
output = "Nice! I've marked this task as done:" + System.lineSeparator()
+ tasks.get(Integer.parseInt(marks[1]) - 1).toString();
} else if (input.matches("unmark [0-9]{1,2}")) {
String[] marks = input.split(" ");
tasks.get(Integer.parseInt(marks[1]) - 1).markAsNotDone();
output = "OK, I've marked this task as not done yet:" + System.lineSeparator()
+ tasks.get(Integer.parseInt(marks[1]) - 1).toString();
} else if (input.startsWith("todo", 0)) {
String toDoDesc = input.split("todo")[1].trim();
Task toDo = new Todo(toDoDesc);
tasks.add(toDo);
output = "Got it. I've added this task:" + System.lineSeparator()
+ toDo.toString() + System.lineSeparator() + "Now you have " + Task.numberOfTasks
+ " in the list.";
} else if (input.startsWith("deadline", 0)) {
String deadlineDesc = input.split("/")[0].split("deadline")[1].trim();
String deadlineDay = input.split("/")[1].trim();
Task deadline = new Deadline(deadlineDesc, deadlineDay);
tasks.add(deadline);
output = "Got it. I've added this task:" + System.lineSeparator()
+ deadline.toString() + System.lineSeparator() + "Now you have " + Task.numberOfTasks
+ " in the list.";
} else if (input.startsWith("event", 0)) {
String eventDesc = input.split("/")[0].split("event")[1].trim();
String start = input.split("/")[1].trim();
String end = input.split("/")[2].trim();
Task event = new Event(eventDesc, start, end);
tasks.add(event);
output = "Got it. I've added this task:" + System.lineSeparator()
+ event.toString() + System.lineSeparator() + "Now you have " + Task.numberOfTasks

Choose a reason for hiding this comment

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

A lot of "magic numbers" involved. Maybe can consider using constants instead?

Copy link

Choose a reason for hiding this comment

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

Try not to use magic numbers. Can consider using an aptly named variable.

Copy link

Choose a reason for hiding this comment

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

Can consider using switch case instead of if else to decide what to do based on the first word entered

+ " in the list.";
} else {
Task task = new Task(input);

Choose a reason for hiding this comment

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

A lot of complex strings/formulas, maybe consider splitting them up?

tasks.add(task);
output = "Got it. I've added this task:" + System.lineSeparator()
+ task.toString() + System.lineSeparator() + "Now you have " + Task.numberOfTasks
+ " in the list.";
}

Choose a reason for hiding this comment

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

Good code readability with the else-if statements!

System.out.println("___________________________________________________");
System.out.println(output);
System.out.println("___________________________________________________");
input = scan.nextLine();
}
} catch (NumberFormatException e) {

Choose a reason for hiding this comment

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

Good use of exceptions here

// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Bye. Hope to see you again soon!");

}

}
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;

Choose a reason for hiding this comment

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

variables such as start and end can be more descriptive.

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()+" ("+start+" "+end+")";
}
}
47 changes: 47 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.ArrayList;

public class Task {
protected String description;
protected boolean isDone;
static int numberOfTasks = 0;

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

public static String printTasksList(ArrayList<Task> tasks) {
String tasksList = new String();
int count = 1;
for (Task i : tasks) {
tasksList += count + ". " + i.toString();
if (count < numberOfTasks) {
tasksList += System.lineSeparator();
}
count++;
}
return tasksList;
}

public String getDescription() {
return description;
}

public String getStatusIcon() {
return (isDone ? "X" : " ");
}

public void markAsDone() {
isDone = true;
}

public void markAsNotDone() {
isDone = false;
}

public String toString() {
return "[" + this.getStatusIcon() + "]" + "\t" + this.getDescription();
}

}
10 changes: 10 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Todo extends Task{
public Todo (String description){
super(description);
}

@Override
public String toString(){
return "[T]"+super.toString();
}
}