Skip to content
Open
14 changes: 14 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Deadline extends WordListItem{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class Deadline extends WordListItem{
public class Deadline extends WordListItem {

Be careful with the spacing requirements for Egyptian style brackets!

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 + ")";
}
}
66 changes: 66 additions & 0 deletions src/main/java/Duke.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The coding standard for this module requires no indentation for the case clauses under switch! Perhaps you can configure your IDE to follow this new standard by default.


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!");
}
}
14 changes: 14 additions & 0 deletions src/main/java/Event.java
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 + ")";
}
}
35 changes: 35 additions & 0 deletions src/main/java/InputParser.java
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};
}
}
17 changes: 17 additions & 0 deletions src/main/java/InputType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public enum InputType {

Choose a reason for hiding this comment

The 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;
}

}
12 changes: 12 additions & 0 deletions src/main/java/Todo.java
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();
}
}
67 changes: 67 additions & 0 deletions src/main/java/WordList.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can make use of the toString() method of WordListItem directly in printList(), instead of returning a complicated String expression here.

23 changes: 23 additions & 0 deletions src/main/java/WordListItem.java
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;
}
}