-
Notifications
You must be signed in to change notification settings - Fork 76
[rohitcube] iP #86
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
base: master
Are you sure you want to change the base?
[rohitcube] iP #86
Changes from 8 commits
1d65904
102f25f
31650d6
3b2adcc
76a133a
fb4c7e8
046aa40
6d3621e
7d71e38
47c89fa
2ca1a03
2840b04
d9e74d0
22e6d32
a185c4a
d46612a
a4fd5f6
afbf41f
841f65c
3731fe1
1552e4d
f26d5a3
4129c36
7a801e3
c36c6f3
afbb48e
e9f3fd3
cdec293
d71f127
7a861a7
a049895
35b3563
9cbbef5
372929a
455325c
6924d30
d615630
2254701
63815fe
a25d92c
b41095c
25bd15c
98e22e3
6484d32
d81536c
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,2 @@ | ||
| public class Main { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
|
|
||
|
|
||
| import Tasks.*; | ||
| import dukey.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Dukey { | ||
| private static void addTodo(String line, ArrayList<Task> tasks) { | ||
| String[] words = line.split(" "); | ||
| String description = null; | ||
| try { | ||
| description = words[1]; | ||
| tasks.add(new Todo(description)); | ||
| tasks.get(tasks.size() - 1).printNewTask(); | ||
| } catch(IndexOutOfBoundsException 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. Consider abstracting this out into a constant variable called LINE_DIVIDER. |
||
| System.out.println(DukeyException.todoDescriptionError()); | ||
| System.out.println("_____________________________________________________"); | ||
| } | ||
| } | ||
|
|
||
| private static void addEvent(String line, ArrayList<Task> tasks) { | ||
| int startIndexOfFrom = line.indexOf("/from"); | ||
| int startIndexOfTo = line.indexOf("/to"); | ||
| final int beginIndex = 6; | ||
| try { | ||
| String from = line.substring(startIndexOfFrom + 6, startIndexOfTo); | ||
| String to = line.substring(startIndexOfTo + 4); | ||
| String description = line.substring(beginIndex, startIndexOfFrom); | ||
| tasks.add(new Event(from, to, description)); | ||
| tasks.get(tasks.size() - 1).printNewTask(); | ||
| } catch(StringIndexOutOfBoundsException e) { | ||
| System.out.println("_____________________________________________________"); | ||
| System.out.println(DukeyException.EventFormatError()); | ||
| System.out.println("_____________________________________________________"); | ||
| } | ||
| } | ||
|
|
||
| private static void addDeadline(String line, ArrayList<Task> tasks) { | ||
| String[] words = line.split("/by"); | ||
| String[] words2 = line.split(" "); | ||
| try { | ||
| String description = words2[1]; | ||
| String by = words[1]; | ||
| tasks.add(new Deadline(description, by)); | ||
| tasks.get(tasks.size() - 1).printNewTask(); | ||
| } catch(ArrayIndexOutOfBoundsException e) { | ||
| System.out.println("_____________________________________________________"); | ||
| System.out.println(DukeyException.deadlineDescriptionError()); | ||
| System.out.println("_____________________________________________________"); | ||
| } | ||
| } | ||
|
|
||
| private static void unmarkTask(String line, ArrayList<Task> tasks) { | ||
| String[] words = line.split(" "); | ||
| int taskNum = Integer.parseInt(words[1]) - 1; | ||
| tasks.get(taskNum).unmarkTask(); | ||
| } | ||
|
|
||
| private static void markTask(String line, ArrayList<Task> tasks) { | ||
| String[] words = line.split(" "); | ||
| int taskNum = Integer.parseInt(words[1]) - 1; | ||
| tasks.get(taskNum).setDone(); | ||
| } | ||
|
|
||
|
|
||
| public static void printTaskList(ArrayList<Task> tasks) { | ||
| System.out.println("Here are the tasks in your list:"); | ||
| int index = 1; | ||
| for (Task task : tasks) { | ||
| System.out.println((index++) + "." + task); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Hey! I'm Dukey, your virtual assistant!\nWhat can I do for you?\n"); | ||
| Scanner in = new Scanner(System.in); | ||
| String line; | ||
|
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. Consider changing the variable name to something more appropriate such as userInput. |
||
| ArrayList<Task> tasks = new ArrayList<>(); | ||
|
|
||
| while (true) { | ||
| line = in.nextLine(); | ||
| String[] words = line.split(" "); | ||
| String firstWord = words[0]; | ||
| switch (firstWord) { | ||
| case "bye": | ||
|
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. Take note of the coding standard violation here, the case clause should not have an indentation. You can change your IDE to follow the coding standard. |
||
| System.out.println("Bye. Hope to see you again soon!\n"); | ||
| return; | ||
| case "list": | ||
| printTaskList(tasks); | ||
| break; | ||
| case "mark": | ||
| markTask(line, tasks); | ||
| break; | ||
| case "unmark": | ||
| unmarkTask(line, tasks); | ||
| break; | ||
| case "deadline": | ||
| addDeadline(line, tasks); | ||
| break; | ||
| case "event": | ||
| addEvent(line, tasks); | ||
| break; | ||
| case "todo": | ||
| addTodo(line, tasks); | ||
| break; | ||
| default: | ||
| if (line.trim().isEmpty()) { | ||
| System.out.println("________________________________"); | ||
| System.out.println(DukeyException.EmptyInputError()); | ||
| System.out.println("________________________________"); | ||
| } else { | ||
| System.out.println("____________________________________________________________"); | ||
| System.out.println(DukeyException.InvalidInputError()); | ||
| System.out.println("____________________________________________________________"); | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package Tasks; | ||
|
|
||
| import Tasks.Task; | ||
|
|
||
| public class Deadline extends Task { | ||
| private String due; | ||
|
|
||
| public Deadline(String description, String due) { | ||
| super(description); | ||
| this.due = due; | ||
| this.type = 'D'; | ||
| } | ||
| // appends "[D]" to the beginning of the string. | ||
| // Then, it calls super.toString(), ie. it calls the toString() method of the superclass | ||
| // then adds the due date | ||
| @Override | ||
| public String toString() { | ||
| return "[" + this.type + "]" + super.toString() + " (by:"+ this.due + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package Tasks; | ||
|
|
||
| import Tasks.Task; | ||
|
|
||
| public class Event extends Task { | ||
| private String from; | ||
| private String by; | ||
|
|
||
| public Event(String from, String by, String description) { | ||
| // call by the superclass (task) constructor | ||
| super(description); | ||
| this.from = from; | ||
| this.by = by; | ||
| this.type = 'E'; | ||
| } | ||
| // appends "[R]" to the beginning of the string. | ||
| // Then, it calls super.toString(), ie. it calls the toString() method of the superclass | ||
| // then adds the from and to timings | ||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + "(from: " + this.from + "to: " + this.by + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package Tasks; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| protected char type; | ||
| protected static int numTasks = 0; | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "X" : " "); | ||
| } | ||
| public static int getNumTasks() { | ||
| return numTasks; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return this.description; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return this.isDone; | ||
| } | ||
|
|
||
| public void setDone() { | ||
| this.isDone = true; | ||
| System.out.println("Nice! I've marked this task as done:\n\t " + this); | ||
| } | ||
| // defines constructor for the task class | ||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| this.type = 'T'; | ||
| numTasks++; | ||
| } | ||
|
|
||
| // made changes as an earlier comment mentioned it was better to have | ||
| // mark and unmark as two different functions | ||
| public void unmarkTask() { | ||
| this.isDone = false; | ||
| System.out.println("Nice! I've marked this task as done:\n\t " + this); | ||
| } | ||
|
|
||
| // the superclass toString outputs the mark box and the description | ||
| @Override | ||
| public String toString() { | ||
| return "[" + this.getStatusIcon() + "] " + this.getDescription(); | ||
| } | ||
|
|
||
| // | ||
| public void printNewTask() { | ||
| System.out.print("Got it. I've added this task:\n " + this + "\nNow you have " + getNumTasks()); | ||
| if (getNumTasks() > 1) { | ||
| System.out.println(" tasks in the list."); | ||
| } else { | ||
| System.out.println(" task in the list."); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package Tasks; | ||
|
|
||
| import Tasks.Task; | ||
|
|
||
| public class Todo extends Task { | ||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
| // It appends "[T]" to the beginning of the string. | ||
| // Then, it calls super.toString(), which means it calls the toString() method of the superclass | ||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package dukey; | ||
|
|
||
| public class DukeyException extends Exception { | ||
| public DukeyException(String message){ | ||
| super(message); | ||
| } | ||
|
|
||
| public static String InvalidInputError(){ | ||
| return "☹ OOPS!!! I'm sorry, but I don't know what that means :-("; | ||
| } | ||
|
|
||
| public static String EmptyInputError(){ | ||
| return "☹ OOPS!!! I'm sorry, but you have to input something :-|"; | ||
| } | ||
|
|
||
| public static String todoDescriptionError(){ | ||
| return "☹ OOPS!!! The description of a todo cannot be empty."; | ||
| } | ||
|
|
||
| public static String deadlineDescriptionError(){ | ||
| return "☹ OOPS!!! Please enter the description then '/by', followed by the time."; | ||
| } | ||
|
|
||
| public static String EventFormatError(){ | ||
| return "☹ OOPS!!! Please enter '/from' and '/to' into your instructions."; | ||
| } | ||
|
|
||
| } |
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.
Imported classes should always be listed explicitly.