-
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 8 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 |
---|---|---|
@@ -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+")"; | ||
} | ||
|
||
} |
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" | ||||||||
|
@@ -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>(); | ||||||||
try (Scanner scan = new Scanner(System.in)) { | ||||||||
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. Please follow the Java coding standards when using exceptions.
Suggested change
|
||||||||
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")) { | ||||||||
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 | ||||||||
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 lot of "magic numbers" involved. Maybe can consider using constants instead? 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 not to use magic numbers. Can consider using an aptly named variable. 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. 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); | ||||||||
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 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."; | ||||||||
} | ||||||||
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 code readability with the else-if statements! |
||||||||
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,15 @@ | ||
public class Event extends Task { | ||
protected String start; | ||
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. 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+")"; | ||
} | ||
} |
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(); | ||
} | ||
|
||
} |
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(); | ||
} | ||
} |
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.
I like how you used a resizable array to store the tasks instead of a fixed size of 100.