-
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
[ngyongjian] iP #208
base: master
Are you sure you want to change the base?
[ngyongjian] iP #208
Changes from 13 commits
fff13bd
d5c0add
900b481
6b160da
ef4b3fb
443d336
49f3394
03e770e
c010346
caa5969
06d3da3
161d9d3
1672649
8926248
2657473
a31c6c2
9f407e0
cd97e4c
a178113
2c054d4
713b40a
10af1c0
8aabdd8
f2ad465
ddc4626
30670ce
478a610
d35fc8f
f8ea312
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 |
---|---|---|
@@ -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)) { | ||
String input = scan.nextLine(); | ||
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. "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(); | ||
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. 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(); | ||
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. 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(); | ||
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. 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) { | ||
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. Good use of exceptions here |
||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
|
||
} | ||
|
||
} |
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); | ||
} | ||
} |
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+")"; | ||
} | ||
|
||
} |
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+")"; | ||
} | ||
} |
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(); | ||
} | ||
|
||
} |
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(); | ||
} | ||
} |
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.
Please follow the Java coding standards when using exceptions.