-
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 1 commit
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 |
---|---|---|
|
@@ -21,14 +21,36 @@ public void display() { | |
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); | ||
} | ||
|
||
public void mark(String command) { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
tasks[index].mark(); | ||
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 > taskIndex || index < 0) { | ||
throw new DukeException("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
tasks[index].mark(); | ||
} catch (NumberFormatException ex) { | ||
System.out.println("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
} | ||
|
||
public void unmark(String command) { | ||
int index = Integer.parseInt(command.split(" ")[1]) - 1; | ||
tasks[index].unmark(); | ||
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 > taskIndex || index < 0) { | ||
throw new DukeException("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} else if (tasks[index].getStatusIcon().equals(" ")) { | ||
throw new DukeException("\t ☹ OOPS!!! This task has not been marked yet.\n"); | ||
} | ||
tasks[index].unmark(); | ||
} catch (NumberFormatException ex) { | ||
System.out.println("\t ☹ OOPS!!! Please input a valid number.\n"); | ||
} | ||
} | ||
|
||
public void list() { | ||
|
@@ -45,22 +67,51 @@ public void addTask(Task task) { | |
+ "\t\t "+ tasks[taskIndex - 1].toString() | ||
+ "\n\t Now you have " + taskIndex + " tasks in the list.\n"); | ||
} | ||
public void todo(String command) { | ||
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) { | ||
String info[] = command.split(" ", 2)[1].split(" /by "); | ||
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 "); | ||
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) { | ||
String info[] = command.split(" ", 2)[1].split(" /from "); | ||
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]; | ||
|
@@ -74,32 +125,40 @@ public void run() { | |
start(); | ||
while (true) { | ||
String command = input.nextLine(); | ||
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); | ||
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 { | ||
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) { | ||
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); | ||
} | ||
} |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing them out!