-
Notifications
You must be signed in to change notification settings - Fork 267
[Edward Alvin] ip #305
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 #305
Changes from 10 commits
d839859
43b3d55
319056f
0688e25
53466c7
be0072b
e8e1092
a4f0552
b0bfa54
4dcb935
888a7b8
94995b9
28a9832
5aa3725
14e2a6a
b307f61
082bd2e
f451d9f
1e84453
d93678d
0ae6bae
f5ac54f
9017932
0da00b1
49667e6
0df53e3
06db74c
61632f0
2c1ab2b
de030de
9701571
2fb2a5d
9cbecdf
939b9ab
f2136c3
e089191
6d1fe81
19b492b
559e67e
3080ed8
941e353
020089e
705a92d
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) { | ||
|
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. Small thing because its just a parameter but maybe change value to values so its plural? |
||
| switch(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. Minor style-related thing, but there should be a space before the bracket i.e |
||
| case LIST: | ||
|
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. According to the codestyle, there should be no intentation for 'case' clauses in the switch block. |
||
| 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; | ||
| } | ||
| } | ||
|
|
||
| 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) { | ||
|
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 it would be neater to separate out into boolean variables for example |
||
| 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 { | ||
| 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{ | ||
|
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 there should be a space between WordListItem and {. |
||
| 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; | ||
|
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 this could be renamed to words or something similar to be plural? |
||
|
|
||
| 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() { | ||
|
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 standards suggest that method names be verbs. Perhaps this could be changed to getLength or something like that. |
||
| 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; | ||
| } | ||
| } | ||
| 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.
Why the cast to String[] array here, instead of making
InputParser::parseInput(input)return a String[] instead of Object[]?