-
Notifications
You must be signed in to change notification settings - Fork 315
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
[Natalie Tan] Duke Increments #344
base: master
Are you sure you want to change the base?
Changes from 7 commits
65f72a8
0112efe
cfd6da7
6e6ace1
a3ca5a4
7b60e81
2327b2a
791010a
070f7e5
d497040
17493be
f84bf53
6bb6adf
6a07612
b1e9465
7e6ed3a
070a1e2
148f51b
54e6458
8eafe91
06492d2
d89a23a
94dcf1c
13c1f9b
72d92b2
de6b351
0d7e28e
82c5a39
a0ae06d
7043555
ce6a15d
901c014
51e41e5
404874b
0dca0c2
7cffc25
3413d6b
3988b1d
66a1ab5
f3fbba2
003af61
dd6a241
b308c14
98d1395
3034767
94dd6d4
b744f1d
8b3d7c6
a89e491
8d0aa67
c6e12d8
3ecafbf
e4fd3fc
6bfa580
6c84fec
d9b9f4d
eaaf797
97eaa87
be0d50f
d5daa2d
64b49d2
2b12109
4ed6113
6608006
95db334
da470b7
427b3d6
ed3a65a
8505aa5
68257c5
aff5481
95fc0ad
506bd56
ae70f07
a988fae
a9e589d
5720d80
52bf5a7
9725820
cd2608c
434af83
77f0894
e6bd017
39f4004
6a0dbab
5d1b9ae
f44e942
cad088e
e960263
a1c9980
d57ad03
9f14515
711cb9b
7ea9ef2
538e85a
8f03955
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,15 @@ | ||
public class Deadline extends Task { | ||
private String by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = by; | ||
this.type = "D"; | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString() + "(by: " + by + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import java.util.Scanner; | ||
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. Not needed |
||
|
||
public class Duke { | ||
private static DukeBot dukeBot; | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
dukeBot = new DukeBot(); | ||
dukeBot.initialise(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import java.util.InputMismatchException; | ||
import java.util.NoSuchElementException; | ||
import java.util.Scanner; | ||
|
||
public class DukeBot { | ||
private Scanner in; | ||
private TaskList taskList = new TaskList(); | ||
private static final String welcomeMessage = "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. From the coding standard:
|
||
|
||
/** | ||
* Starts DukeBot and prints welcome message. | ||
*/ | ||
|
||
public void initialise() { | ||
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. These code should be in the constructor and |
||
String logo = " ____ _ \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. Maybe this should be a constant too. |
||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println(welcomeMessage); | ||
in = new Scanner(System.in); | ||
run(); | ||
} | ||
|
||
/** | ||
* Reads input from the user and passes it to process(). | ||
*/ | ||
private void run() { | ||
String input; // stores input from user | ||
|
||
this.taskList = new TaskList(); | ||
|
||
// continuously take in user input until terminated | ||
while (in.hasNextLine()) { | ||
input = in.nextLine(); | ||
if (input.equals("bye")) { | ||
// if "bye", terminate | ||
// todo: is there a way to combine this into processInput()? | ||
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 can make |
||
break; | ||
} | ||
|
||
try { | ||
this.taskList.processInput(input); | ||
} catch (InputMismatchException e) { | ||
System.out.println("Sorry! I don't know what that means :("); | ||
} | ||
} | ||
|
||
// say goodbye upon exit | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public class Event extends Task { | ||
private String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
this.type = "E"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString() + "(at: " + at + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Task { | ||
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 this class should be static :) |
||
private String description; | ||
private Boolean isDone; | ||
protected String type; | ||
|
||
public Task(String description) { | ||
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. IMO this constructor should take in |
||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String type = "[" + this.type + "] "; | ||
String status = "[" + this.getStatusIcon() + "] "; | ||
return type + status + this.description; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "\u2713" : "\u2718"); // return tick or X symbols | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
System.out.println("Nice! I've marked this task as done:"); | ||
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. User interaction should be handled at the call site. Otherwise, it would be difficulty to integrate with GUI. |
||
System.out.println(this.toString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import java.util.ArrayList; | ||
import java.util.InputMismatchException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.Scanner; | ||
|
||
public class TaskList { | ||
|
||
private ArrayList<Task> tasks = new ArrayList<>(); | ||
|
||
public enum TaskType { | ||
DEADLINE, | ||
EVENT, | ||
TODO | ||
} | ||
|
||
public void addToList(TaskType taskType, String description, 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. Maybe make this method take in a |
||
throws IllegalArgumentException { | ||
Task newTask = null; // task to be added | ||
|
||
// based on task type, create corresponding subclass object | ||
switch (taskType) { | ||
case DEADLINE: | ||
newTask = new Deadline(description, deadline); | ||
break; | ||
case EVENT: | ||
newTask = new Event(description, deadline); | ||
break; | ||
case TODO: | ||
newTask = new Todo(description); | ||
break; | ||
default: | ||
// this should not happen; invalid task type should be caught in caller method already | ||
System.out.println("Something went wrong!"); | ||
break; | ||
} | ||
|
||
// add newTask to taskList | ||
tasks.add(newTask); | ||
System.out.println("Okay! I've added: " + description | ||
+ ". Use list to see all your tasks!"); | ||
// todo: description has trailing space due to using /at or /by as delimiter. how? | ||
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. Use |
||
} | ||
|
||
public void printList() { | ||
int i = 1; | ||
for (Iterator<Task> iterator = this.tasks.iterator(); iterator.hasNext(); i++) { | ||
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. I don't think the iterator here is necessary :) |
||
String s = iterator.next().toString(); | ||
System.out.println(i + ". " + s); | ||
} | ||
} | ||
|
||
public Task getTask(int taskIndex) throws IndexOutOfBoundsException { | ||
return tasks.get(taskIndex - 1); | ||
} | ||
|
||
/** | ||
* Processes the given input string. | ||
* | ||
* @param input input string given by user. | ||
*/ | ||
public void processInput(String input) throws InputMismatchException { | ||
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. Instead of having almost all the app logic here, maybe it would help to define a |
||
Scanner inputReader = new Scanner(input); | ||
String command = inputReader.next(); | ||
String description; | ||
String deadline; | ||
|
||
switch (command) { | ||
case "list": | ||
// if "list", print list | ||
this.printList(); | ||
break; | ||
case "done": | ||
try { | ||
int taskId = inputReader.nextInt(); // extract the task ID entered by user | ||
this.getTask(taskId).markAsDone(); // mark task as done | ||
} catch (InputMismatchException e) { | ||
// user input after "done" is not an int | ||
System.out.println("Oops! You entered an invalid task ID!"); | ||
} catch (NoSuchElementException e) { | ||
// user input after "done" is blank | ||
System.out.println("Oops! You did not enter a task ID!"); | ||
} catch (IndexOutOfBoundsException e) { | ||
// user input after "done" is an invalid task ID | ||
System.out.println("Oops! You entered an invalid task ID!"); | ||
} | ||
break; | ||
case "todo": | ||
try { | ||
description = inputReader.nextLine(); | ||
deadline = "no deadline"; | ||
this.addToList(TaskList.TaskType.TODO, description, deadline); | ||
} catch (NoSuchElementException e) { | ||
// user imput after task type is blank | ||
System.out.println("Oops! You did not enter a description!"); | ||
} | ||
break; | ||
case "event": | ||
inputReader.useDelimiter("/at"); | ||
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. This is pretty neat :) |
||
try { | ||
description = inputReader.next(); | ||
deadline = inputReader.next(); | ||
this.addToList(TaskList.TaskType.EVENT, description, deadline); | ||
} catch (NoSuchElementException e) { | ||
// user imput after task type is blank | ||
System.out.println("Oops! You did not enter a description or deadline!"); | ||
} | ||
break; | ||
case "deadline": | ||
inputReader.useDelimiter("/by"); | ||
try { | ||
description = inputReader.next(); | ||
deadline = inputReader.next(); | ||
this.addToList(TaskList.TaskType.DEADLINE, description, deadline); | ||
} catch (NoSuchElementException e) { | ||
// user imput after task type is blank | ||
System.out.println("Oops! You did not enter a description or deadline!"); | ||
} | ||
break; | ||
default: | ||
throw new InputMismatchException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
this.type = "T"; | ||
} | ||
} |
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.
Unused import :)