-
Notifications
You must be signed in to change notification settings - Fork 267
[Edward Alvin] ip #294
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?
[Edward Alvin] ip #294
Changes from all commits
43b3d55
319056f
0688e25
53466c7
be0072b
e8e1092
a4f0552
b0bfa54
4dcb935
888a7b8
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,14 @@ | ||
| public class Deadline extends WordListItem{ | ||
| static private final String SYMBOL = "[D]"; | ||
| private String datetime; | ||
|
|
||
| public Deadline(String description, String datetime) { | ||
| super(description); | ||
| this.datetime = datetime; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return SYMBOL + super.toString() + " (by: " + this.datetime + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,76 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| static WordList wordList; | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner sc = new Scanner(System.in); | ||
| wordList = new WordList(); | ||
|
|
||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
|
|
||
| replyWelcomeMessage(); | ||
| String input; | ||
| while (true) { | ||
| input = sc.nextLine(); | ||
| if (input.isEmpty()) { | ||
| warnEmpty(); | ||
| continue; | ||
| } | ||
|
|
||
| Object[] parseResult = InputParser.parseInput(input); | ||
| InputType inputType = (InputType) parseResult[0]; | ||
| String[] value = (String[]) parseResult[1]; | ||
|
|
||
| processInput(inputType, value); | ||
| if (inputType == InputType.BYE) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void processInput(InputType inputType, String[] value) { | ||
| switch(inputType) { | ||
| case LIST: | ||
| wordList.printList(); | ||
| break; | ||
| case MARK: | ||
| wordList.markItem(Integer.parseInt(value[0])); | ||
| break; | ||
| case UNMARK: | ||
| wordList.unmarkItem(Integer.parseInt(value[0])); | ||
| break; | ||
| case TODO: | ||
| wordList.storeTodo(value[0]); | ||
| break; | ||
| case DEADLINE: | ||
| wordList.storeDeadline(value[0], value[1]); | ||
| break; | ||
| case EVENT: | ||
| wordList.storeEvent(value[0], value[1]); | ||
| break; | ||
| case BYE: | ||
| replyBye(); | ||
| break; | ||
| case NONE: | ||
| break; | ||
| } | ||
| } | ||
|
Comment on lines
+37
to
+63
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. The coding standard for this module requires no indentation for the |
||
|
|
||
| public static void replyWelcomeMessage() { | ||
| System.out.println("Hello! I'm Duke"); | ||
| System.out.println("What can I do for you?"); | ||
| } | ||
|
|
||
| public static void warnEmpty() { | ||
| System.out.println("input is Empty!"); | ||
| } | ||
| public static void replyBye() { | ||
| 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 WordListItem{ | ||
| static private final String SYMBOL = "[E]"; | ||
| private String datetime; | ||
|
|
||
| public Event(String description, String datetime) { | ||
| super(description); | ||
| this.datetime = datetime; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return SYMBOL + super.toString() + " (at: " + this.datetime + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| public class InputParser { | ||
| static Object[] parseInput(String input) { | ||
| InputType type = InputType.NONE; | ||
| String[] value = new String[]{ input }; | ||
| for(InputType inputType: InputType.values()) { | ||
| if (inputType == InputType.NONE) { | ||
| continue; | ||
| } | ||
|
|
||
| if (input.startsWith(inputType.label)) { | ||
| if (inputType == InputType.BYE || inputType == InputType.LIST) { | ||
| value = new String[]{}; | ||
| } else if (inputType == InputType.TODO || inputType == InputType.MARK | ||
| || inputType == InputType.UNMARK) { | ||
| String description = input.substring(inputType.label.length() + 1); | ||
| value = new String[]{description}; | ||
| } else if (inputType == InputType.DEADLINE) { | ||
| int datetimeIndex = input.indexOf("/by"); | ||
| String description = input.substring(inputType.label.length() + 1, datetimeIndex - 1); | ||
| String datetime = input.substring(datetimeIndex + 4); | ||
| value = new String[]{description, datetime}; | ||
| } else if (inputType == InputType.EVENT) { | ||
| int datetimeIndex = input.indexOf("/at"); | ||
| String description = input.substring(inputType.label.length() + 1, datetimeIndex - 1); | ||
| String datetime = input.substring(datetimeIndex + 4); | ||
| value = new String[]{description, datetime}; | ||
| } | ||
|
|
||
| type = inputType; | ||
| return new Object[]{type, value}; | ||
| } | ||
| } | ||
| return new Object[]{type, value}; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public enum InputType { | ||
|
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. Interesting way to use enums! |
||
| BYE("bye"), | ||
| LIST("list"), | ||
| DEADLINE("deadline"), | ||
| EVENT("event"), | ||
| TODO("todo"), | ||
| MARK("mark"), | ||
| UNMARK("unmark"), | ||
| NONE("none"); | ||
|
|
||
| public final String label; | ||
|
|
||
| private InputType(String label) { | ||
| this.label = label; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public class Todo extends WordListItem{ | ||
| static private final String SYMBOL = "[T]"; | ||
|
|
||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return SYMBOL + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class WordList { | ||
| private ArrayList<WordListItem> wordList; | ||
|
|
||
| public WordList() { | ||
| this.wordList = new ArrayList<>(); | ||
| } | ||
|
|
||
| private void echoAddedItem(WordListItem wordListItem) { | ||
| System.out.println(" ------------------------------------"); | ||
| System.out.println(" Got it. I've added this task: "); | ||
| System.out.println(" " + wordListItem); | ||
| System.out.println(" ------------------------------------"); | ||
| } | ||
|
|
||
| public void storeTodo(String word) { | ||
| WordListItem todo = new Todo(word); | ||
| this.wordList.add(todo); | ||
| echoAddedItem(todo); | ||
| } | ||
|
|
||
| public void storeDeadline(String word, String datetime) { | ||
| WordListItem deadline = new Deadline(word, datetime); | ||
| this.wordList.add(deadline); | ||
| echoAddedItem(deadline); | ||
| } | ||
|
|
||
| public void storeEvent(String word, String datetime) { | ||
| WordListItem event = new Event(word, datetime); | ||
| this.wordList.add(event); | ||
| echoAddedItem(event); | ||
| } | ||
|
|
||
| public void markItem(int itemNumber) { | ||
| this.wordList.get(itemNumber - 1).markItem(); | ||
| System.out.println("Nice! I've marked this task as done: "); | ||
| System.out.println(" " + this.wordList.get(itemNumber - 1)); | ||
| } | ||
|
|
||
| public void unmarkItem(int itemNumber) { | ||
| this.wordList.get(itemNumber - 1).unmarkItem(); | ||
| System.out.println("Nice! I've marked this task as not done: "); | ||
| System.out.println(" " + this.wordList.get(itemNumber - 1)); | ||
| } | ||
|
|
||
| public void printList() { | ||
| System.out.println(this); | ||
| } | ||
|
|
||
| public int length() { | ||
| return this.wordList.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| int i = 1; | ||
| String str = ""; | ||
| str += "------------------------------------\n"; | ||
| for(WordListItem wordListItem: this.wordList) { | ||
| str += i + ". " + wordListItem + "\n"; | ||
| i++; | ||
| } | ||
| str += "------------------------------------\n"; | ||
| return str; | ||
| } | ||
| } | ||
|
Comment on lines
+55
to
+67
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 think you can make use of the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| public class WordListItem { | ||
| private String description; | ||
| private boolean isDone; | ||
|
|
||
| public WordListItem(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public void markItem() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public void unmarkItem() { | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| String doneSymbol = isDone ? "[X]" : "[ ]"; | ||
| return doneSymbol + " " + this.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.
Be careful with the spacing requirements for Egyptian style brackets!