-
Notifications
You must be signed in to change notification settings - Fork 361
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
[Qiu Qianhui] iP #376
base: master
Are you sure you want to change the base?
[Qiu Qianhui] iP #376
Changes from 7 commits
556af3f
2abf484
6e6c66f
8e390d2
63deb9f
638fe5e
fea77b0
a6d87e4
a57249e
1bc3b13
3d7c8f3
f308ef4
eaeedb9
db837df
1e9262f
e1c0c5e
858927b
c88a915
817bcee
6a8cd60
1a75274
f896222
5e84399
06a30bd
bc80cb8
2afc403
b05471e
0f3fe17
a7b2d76
895bae8
d3b4044
74bdb53
ff5718d
e9c03a4
6c72fb3
bc2e9a6
3e7ebc9
2c7366f
11b85c6
da2cede
9e36d1c
4ef0415
4b65663
f7f30c0
3a8547a
786fd75
9e53f70
85a4776
6882f68
ac99812
3b97191
9ff9abb
26f91eb
832d46a
f7536e5
abd962b
769a11d
b5d8dc4
8cd75f8
a19d9e3
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 by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,186 @@ | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
|
||
private Scanner input = new Scanner(System.in); | ||
private ArrayList<Task> tasks = new ArrayList<>(); | ||
|
||
public void start() { | ||
String logo = "\t ____ _ \n" | ||
+ "\t | _ \\ _ _| | _____ \n" | ||
+ "\t | | | | | | | |/ / _ \\\n" | ||
+ "\t | |_| | |_| | < __/\n" | ||
+ "\t |____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println(logo); | ||
System.out.println("\t Hello! I'm Duke"); | ||
System.out.println("\t What can I do for you?\n"); | ||
} | ||
|
||
public void display() { | ||
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); | ||
} | ||
|
||
public void mark(String command) throws DukeException{ | ||
if (command.trim().equals("mark")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a mark cannot be empty.\n"); | ||
} | ||
try { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
if (index + 1 > tasks.size() || index < 0) { | ||
throw new DukeException("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
tasks.get(index).mark(); | ||
} catch (NumberFormatException ex) { | ||
System.out.println("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
} | ||
|
||
public void unmark(String command) throws DukeException{ | ||
if (command.trim().equals("mark")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a unmark cannot be empty.\n"); | ||
} | ||
try { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
if (index + 1 > tasks.size() || index < 0) { | ||
throw new DukeException("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} else if (tasks.get(index).getStatusIcon().equals(" ")) { | ||
throw new DukeException("\t ☹ OOPS!!! This task has not been marked yet.\n"); | ||
} | ||
tasks.get(index).unmark(); | ||
} catch (NumberFormatException ex) { | ||
System.out.println("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
} | ||
|
||
public void list() { | ||
for (int i = 0; i < tasks.size(); i++) { | ||
System.out.print("\t " + (i + 1) + "." + tasks.get(i).toString() + "\n"); | ||
} | ||
System.out.print("\n"); | ||
} | ||
|
||
public void addTask(Task task) { | ||
tasks.add(task); | ||
System.out.println("\t Got it. I've added this task:\n" | ||
+ "\t\t "+ tasks.get(tasks.size() - 1).toString() | ||
+ "\n\t Now you have " + tasks.size() + " tasks in the list.\n"); | ||
} | ||
public void todo(String command) throws DukeException { | ||
if (command.trim().equals("todo")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a todo cannot be empty.\n"); | ||
} | ||
String description = command.split(" ", 2)[1]; | ||
Todo todo = new Todo(description); | ||
addTask(todo); | ||
} | ||
|
||
public void deadline(String command) throws DukeException { | ||
if (command.trim().equals("deadline")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a deadline cannot be empty.\n"); | ||
} else if (command.trim().contains(" /by ") == false) { | ||
throw new DukeException("\t ☹ OOPS!!! There is no deadline date or the wrong format\n"); | ||
} | ||
|
||
command = command.split(" ", 2)[1]; | ||
if (command.startsWith("/by")) { | ||
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.\n"); | ||
} else if (command.endsWith("/by")) { | ||
throw new DukeException(("☹ OOPS!!! The due date of a deadline cannot be empty.\n")); | ||
} | ||
|
||
String info[] = command.split(" /by "); | ||
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. Perhaps we should put the [] right after the String instead even though this is valid too? 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. Thanks for pointing them out! |
||
String description = info[0]; | ||
String by = info[1]; | ||
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. Maybe instead of just "by", we can use a variable name that indicates that this is a date? |
||
Deadline deadline = new Deadline(description, by); | ||
addTask(deadline); | ||
} | ||
|
||
public void event(String command) throws DukeException{ | ||
command = command.trim(); | ||
if (command.equals("event")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a event cannot be empty.\n"); | ||
} else if (command.contains(" /from ") == false || command.contains(" /to ") == false) { | ||
throw new DukeException("\t ☹ OOPS!!! The from date or due date of a deadline cannot be empty.\n" | ||
+ "\t Or the format is wrong.\n"); | ||
} | ||
|
||
command = command.split(" ", 2)[1]; | ||
if (command.startsWith("/from") || command.endsWith("/to") || command.contains("/from /to")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description, from date or due date of a event cannot be empty.\n"); | ||
} | ||
|
||
String info[] = command.split(" /from "); | ||
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. Perhaps the naming of this variable can be a standard plural noun instead? |
||
String description = info[0]; | ||
String dates[] = info[1].split(" /to "); | ||
String from = dates[0]; | ||
String to = dates[1]; | ||
|
||
Event event = new Event(description, from, to); | ||
addTask(event); | ||
} | ||
|
||
public void delete(String command) throws DukeException { | ||
command = command.trim(); | ||
if (command.equals("delete")) { | ||
throw new DukeException("\t ☹ OOPS!!! The description of a delete cannot be empty.\n"); | ||
} | ||
int index; | ||
try { | ||
index = Integer.parseInt(command.split(" ")[1]); | ||
} catch (NumberFormatException ex) { | ||
throw new DukeException("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
if (index < 1 || index > tasks.size()) { | ||
throw new DukeException("\t ☹ OOPS!!! The input is out of range.\n"); | ||
} | ||
System.out.println("\t Noted. I've removed this task:\n" | ||
+ "\t\t " + tasks.get(index - 1).toString()); | ||
tasks.remove(index - 1); | ||
System.out.println("\t Now you have " + tasks.size() + " tasks in the list.\n"); | ||
} | ||
|
||
public void run() { | ||
start(); | ||
while (true) { | ||
String command = input.nextLine(); | ||
try { | ||
if (command.equals("bye")) { | ||
display(); | ||
System.out.println("\t Bye. Hope to see you again soon!"); | ||
break; | ||
} else if (command.equals("list")) { | ||
display(); | ||
list(); | ||
} else if (command.startsWith("mark")) { | ||
display(); | ||
mark(command); | ||
} else if (command.startsWith("unmark")) { | ||
display(); | ||
unmark(command); | ||
} else if (command.startsWith("todo")) { | ||
display(); | ||
todo(command); | ||
} else if (command.startsWith("deadline")) { | ||
display(); | ||
deadline(command); | ||
} else if (command.startsWith("event")) { | ||
display(); | ||
event(command); | ||
} else if (command.startsWith("delete")) { | ||
display(); | ||
delete(command); | ||
} else { | ||
display(); | ||
System.out.println("\t ☹ OOPS!!! I'm sorry, but I don't know what that means.\n"); | ||
} | ||
} catch (DukeException e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} | ||
} | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
Duke duke = new Duke(); | ||
duke.run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class DukeException extends Exception{ | ||
|
||
public DukeException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Event extends Task{ | ||
protected String from; | ||
protected String to; | ||
|
||
public Event(String description, String from, String to) { | ||
super(description); | ||
this.from = from; | ||
this.to = to; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
public void mark() { | ||
this.isDone = true; | ||
System.out.println("\t Nice! I've marked this task as done:\n" | ||
+ "\t\t [X] " + this.description + "\n"); | ||
} | ||
|
||
public void unmark() { | ||
this.isDone = false; | ||
System.out.println("\t OK, I've marked this task as not done yet:\n" | ||
+ "\t\t [ ] " + this.description + "\n"); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return " [" + getStatusIcon() + "] " + this.description; | ||
} | ||
} |
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(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,56 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello! I'm Duke | ||
What can I do for you? | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Got it. I've added this task: | ||
[T] [ ] borrow book | ||
Now you have 1 tasks in the list. | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Got it. I've added this task: | ||
[D] [ ] return book (by: Sunday) | ||
Now you have 2 tasks in the list. | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
1.[T] [ ] borrow book | ||
2.[D] [ ] return book (by: Sunday) | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Nice! I've marked this task as done: | ||
[X] return book | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Got it. I've added this task: | ||
[E] [ ] project meeting (from: Mon 2pm to: 4pm) | ||
Now you have 3 tasks in the list. | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Nice! I've marked this task as done: | ||
[X] project meeting | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
OK, I've marked this task as not done yet: | ||
[ ] return book | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
1.[T] [ ] borrow book | ||
2.[D] [ ] return book (by: Sunday) | ||
3.[E] [X] project meeting (from: Mon 2pm to: 4pm) | ||
|
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Bye. Hope to see you again soon! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
todo borrow book | ||
deadline return book /by Sunday | ||
list | ||
mark 2 | ||
event project meeting /from Mon 2pm /to 4pm | ||
mark 3 | ||
unmark 2 | ||
list | ||
bye |
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 what you did here - indenting and spacing a sentence into parts that are more readable!