-
Notifications
You must be signed in to change notification settings - Fork 437
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
[Cao] iP #466
base: master
Are you sure you want to change the base?
[Cao] iP #466
Changes from 16 commits
a0828cb
2a955ed
2960cdb
e6f5a1b
d619ffa
7a01956
23ff51e
aaf0db0
b989315
46c1cf5
bf02861
9ed888d
4630050
eccd4bd
518a798
3d92ca2
488eaec
74ae682
fc5812c
fd6a9b3
9528c06
b6efb63
ecd9609
7dbaa04
ad53f61
2a51b71
18690fd
48642d3
500c754
4e5da11
04d380f
da7df49
e9dc761
d743259
e9542b0
88955d9
64d7ccd
a2fe141
03db3d5
6177d62
6ed168a
6703d18
4867fea
600c0da
31edbef
c24a998
7dd194a
a99084c
3f9875b
2b5a07c
4e15055
ce96300
19baa19
25b7998
1e4cdf2
1046b5e
19b36fb
cf73057
4037756
1655726
4b7aeb1
74aa3da
cbb962b
3457ff4
8e00007
9a57b7a
5513e6d
bd54f7a
777b9e0
1615411
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,213 @@ | ||
package main.java; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class CommandHandler { | ||
Scanner sc; | ||
ArrayList<Task> taskList; | ||
|
||
public CommandHandler() { | ||
this.sc = new Scanner(System.in); | ||
this.taskList = new ArrayList<>(); | ||
} | ||
|
||
public void handleCommand() { | ||
while (true) { | ||
String command = sc.next(); | ||
switch (command) { | ||
case "bye": | ||
handleBye(); | ||
return; | ||
case "list": | ||
handleList(); | ||
break; | ||
case "done": | ||
try { | ||
handleDone(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
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 you could abstract out this border line as a variable, then call it whenever you need it. Makes the code tidy. |
||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
break; | ||
case "delete": | ||
try { | ||
handleDelete(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
break; | ||
case "todo": | ||
try { | ||
handleTodo(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
break; | ||
case "deadline": | ||
try { | ||
handleDeadline(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
break; | ||
case "event": | ||
try { | ||
handleEvent(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
break; | ||
default: | ||
try { | ||
handleDefault(); | ||
} catch (DukeException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" " + e.getMessage()); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void handleBye() { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Bye. Hope to see you again soon!"); | ||
System.out.println(" ____________________________________________________________\n"); | ||
return; | ||
} | ||
|
||
public void handleList() { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Here are the tasks in your list:"); | ||
for (int i = 0; i < taskList.size(); i++) { | ||
System.out.print(" " + (i + 1) + "."); | ||
taskList.get(i).printDescription(); | ||
} | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
|
||
public void handleDone() throws DukeException { | ||
String doneCommand = sc.nextLine(); | ||
int index = 0; | ||
if (doneCommand.isEmpty()) { | ||
throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be done!"); | ||
} | ||
try { | ||
index = Integer.parseInt(doneCommand.stripLeading()); | ||
} catch (NumberFormatException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" \u2639 Please enter a valid integer!!"); | ||
System.out.println(" ____________________________________________________________\n"); | ||
return; | ||
} | ||
if (index > taskList.size()) { | ||
throw new DukeException("\u2639 Your number is too large!!"); | ||
} | ||
Task currentTask = taskList.get(index - 1); | ||
currentTask.markAsDone(); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Nice! I've marked this task as done:"); | ||
System.out.print(" "); | ||
currentTask.printDescription(); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
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 you could add appropriate line breaks to enhance readability |
||
|
||
public void handleDelete() throws DukeException { | ||
String deleteCommand = sc.nextLine(); | ||
int index = 0; | ||
if (deleteCommand.isEmpty()) { | ||
throw new DukeException("\u2639 OOPS!!! I need to know the index of the task to be deleted!"); | ||
} | ||
try { | ||
index = Integer.parseInt(deleteCommand.stripLeading()); | ||
} catch (NumberFormatException e) { | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" \u2639 Please enter a valid integer!!"); | ||
System.out.println(" ____________________________________________________________\n"); | ||
return; | ||
} | ||
if (index > taskList.size()) { | ||
throw new DukeException("\u2639 Your number is too large!!"); | ||
} | ||
Task currentTask = taskList.get(index - 1); | ||
taskList.remove(index - 1); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Noted! I've removed this task:"); | ||
System.out.print(" "); | ||
currentTask.printDescription(); | ||
System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||
System.out.println(" ____________________________________________________________\n"); | ||
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. Same comment as above. Line breaks for readability. |
||
} | ||
|
||
public void handleTodo() throws DukeException { | ||
String todoDescription = sc.nextLine(); | ||
if (todoDescription.isEmpty()) { | ||
throw new DukeException("\u2639 OOPS!!! The description of a todo cannot be empty."); | ||
} | ||
Todo todo = new Todo(todoDescription); | ||
taskList.add(todo); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.print(" "); | ||
todo.printDescription(); | ||
System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
|
||
public void handleDeadline() throws DukeException { | ||
|
||
String deadlineCommand = sc.nextLine(); | ||
if (deadlineCommand.isEmpty()) { | ||
throw new DukeException("\u2639 OOPS!!! The description of a deadline cannot be empty."); | ||
} | ||
if (!deadlineCommand.contains("/by")) { | ||
throw new DukeException("\u2639 OOPS!!! The timing of a deadline cannot be empty."); | ||
} | ||
String[] strings = deadlineCommand.split("/by"); | ||
String deadlineDescription = strings[0]; | ||
String time = strings[1]; | ||
Deadline deadline = new Deadline(deadlineDescription, time); | ||
taskList.add(deadline); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.print(" "); | ||
deadline.printDescription(); | ||
System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
|
||
public void handleEvent() throws DukeException{ | ||
String eventCommand = sc.nextLine(); | ||
if (eventCommand.isEmpty()) { | ||
throw new DukeException("\u2639 OOPS!!! The description of an event cannot be empty."); | ||
} | ||
if (!eventCommand.contains("/at")) { | ||
throw new DukeException("\u2639 OOPS!!! The timing of an event cannot be empty."); | ||
} | ||
String[] strings = eventCommand.split("/at"); | ||
String eventDescription = strings[0]; | ||
String date = strings[1]; | ||
Event event = new Event(eventDescription, date); | ||
taskList.add(event); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Got it. I've added this task:"); | ||
System.out.print(" "); | ||
event.printDescription(); | ||
System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||
System.out.println(" ____________________________________________________________\n"); | ||
} | ||
|
||
public void handleDefault() throws DukeException { | ||
throw new DukeException("\u2639 OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main.java; | ||
|
||
public class Deadline extends Task { | ||
protected String deadline; | ||
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. Suggest using private keyword instead of protected. |
||
|
||
public Deadline(String description, String deadline) { | ||
super(description); | ||
this.deadline = deadline; | ||
} | ||
|
||
@Override | ||
public void printDescription() { | ||
System.out.println("[D][" + getStatusIcon() | ||
+ "]" + description + "(by:" + deadline + ")"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
package main.java; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
public static void main(String[] args){ | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| _ \\ _ _| | _____\n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println(" ____________________________________________________________"); | ||
System.out.println(" Hello! I'm Duke\n What can I do for you?"); | ||
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. different parts of the code can be grouped together as a method, make it easier for abstraction. |
||
System.out.println(" ____________________________________________________________\n"); | ||
|
||
CommandHandler commandHandler = new CommandHandler(); | ||
commandHandler.handleCommand(); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main.java; | ||
|
||
public class DukeException extends Exception { | ||
public DukeException(String ErrorMessage) { | ||
super(ErrorMessage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main.java; | ||
|
||
public class Event extends Task { | ||
protected String time; | ||
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. same suggestion as deadline |
||
|
||
public Event(String description, String time) { | ||
super(description); | ||
this.time = time; | ||
} | ||
|
||
@Override | ||
public void printDescription() { | ||
System.out.println("[E][" + getStatusIcon() | ||
+ "]" + description + "(at:" + time + ")"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main.java; | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "\u2713" : "\u2718"); | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
public void printDescription() { | ||
System.out.println("[" + getStatusIcon() + "] " + description); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main.java; | ||
|
||
public class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public void printDescription() { | ||
System.out.println("[T][" + getStatusIcon() + "]" + description); | ||
} | ||
} |
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.
There's no indention for the case statements. It should be 'aligned' with the switch statement.