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 13 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
Binary file added src/main/java/Duke.class
Binary file not shown.
83 changes: 82 additions & 1 deletion src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,91 @@
import java.util.ArrayList;
import java.util.Scanner;

import Exception.DukeException;
import Task.Deadline;
import Task.Event;
import Task.Task;
import Task.Todo;

public class Duke {
public static void main(String[] args) {
public static void main(String[] args) throws DukeException {
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?\n");
ArrayList<Task> tasks = new ArrayList<Task>();// using ArrayList to store tasks instead of array
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")) {
try {
if (input.equals("list")) {
output = "Here are the tasks in the list:" + System.lineSeparator()
+ Task.printTasksList(tasks);

} else if (input.startsWith("mark")) {
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();
Copy link

Choose a reason for hiding this comment

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

Try extracting such chunks into a seperate method to ease readability of the main function as it seems to be too big currently

} else if (input.startsWith("unmark")) {
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();
Copy link

Choose a reason for hiding this comment

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

Strings such as "/" and "deadline" seem to suggest usage of magic literals, do consider declaring them as named constants to enhance readability and understandability

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

Choose a reason for hiding this comment

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

Indentation seems off here, should be 4 instead of 2

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
+ " in the list.";
} else if (input.startsWith("delete")) {
int toDelete = Integer.parseInt(input.split(" ")[1]);
output = "Noted. I've removed this task:" + System.lineSeparator()
+ tasks.get(toDelete - 1).toString();
tasks.remove(toDelete - 1);
Task.decrementNumberOfTasks();
output += System.lineSeparator() + "Now you have " + Task.numberOfTasks + " tasks in the list.";
} else {
output = "OOPS!!! I'm sorry, but I don't know what that means :-(";
}
} catch (ArrayIndexOutOfBoundsException e) {
output = "Please provide the necessary information for the task.";
}

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!");

}

}
7 changes: 7 additions & 0 deletions src/main/java/Exception/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Exception;

public class DukeException extends Exception {
public DukeException (String message){
super(message);
}
}
16 changes: 16 additions & 0 deletions src/main/java/Task/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Task;

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+")";
}

}
17 changes: 17 additions & 0 deletions src/main/java/Task/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Task;

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

import java.util.ArrayList;

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

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

public static void decrementNumberOfTasks() {
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();
}

}
12 changes: 12 additions & 0 deletions src/main/java/Task/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Task;

public class Todo extends Task{
public Todo (String description){
super(description);
}

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