-
Notifications
You must be signed in to change notification settings - Fork 462
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
[Tan Hong Liang] iP #501
base: master
Are you sure you want to change the base?
[Tan Hong Liang] iP #501
Changes from 1 commit
0d13617
e176f21
ff600bd
04c50e0
cf80052
14fbfe9
5442e79
de18707
bd191ca
32a4edb
812a5f8
d42fd1f
cc19d6b
a3a39b8
7408a38
c1cb522
7718dc5
ba58ab6
c85360a
9e9dcd7
b248eee
1d8b29d
bde3039
174ec22
92b2745
76b923c
632ab49
fc92a48
fd15136
c49356d
53c523e
e2959fd
13377a1
f48f75f
1ff9775
41ea42d
0957668
7c80298
0d7c9c1
21f3c2f
52c8f42
4e05947
7963072
636d061
909bc1c
c59219f
7d75648
0356d41
3a7811c
fe7c2af
ebf81e5
b696e2a
02e1d24
63a9b27
a01d243
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,9 @@ | ||
enum CommandType { | ||
LIST, | ||
MARK, | ||
UNMARK, | ||
TODO, | ||
DEADLINE, | ||
EVENT, | ||
BYE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
private static ArrayList<Task> tasks = new ArrayList<Task>(); | ||
private static boolean isEnd = false; | ||
|
@@ -19,70 +18,114 @@ public static void main(String[] args) { | |
String greeting = "Hello! I'm Duke \n" + "What can I do for you?\n"; | ||
Scanner userInput = new Scanner(System.in); | ||
chat(greeting); | ||
while (!isEnd) { | ||
String userStatement = userInput.nextLine(); | ||
String splitUserStatement[] = userStatement.split(" ", 2); | ||
String command = splitUserStatement[0]; | ||
String userArgs = ""; | ||
if (splitUserStatement.length > 1) { | ||
userArgs = splitUserStatement[1]; | ||
while (!isEnd) { | ||
try { | ||
String[] parsedUserInput = parseCommand(userInput.nextLine()); | ||
String command = parsedUserInput[0]; | ||
String arg1 = parsedUserInput[1]; | ||
String arg2 = parsedUserInput[2]; | ||
switch(command) { | ||
case COMMAND_LIST: | ||
chat("Here are the tasks in your list: \n" + list()); | ||
break; | ||
case COMMAND_BYE: | ||
chat("Bye! Hope to see you again!\n"); | ||
isEnd = true; | ||
userInput.close(); | ||
break; | ||
case COMMAND_TODO: | ||
if (arg1.equals("")) { | ||
error("ToDo task requires a description"); | ||
break; | ||
} | ||
ToDo toDoToAdd = new ToDo(arg1); | ||
tasks.add(toDoToAdd); | ||
chat("Got it, I've added this task:\n " + toDoToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
case COMMAND_DEADLINE: | ||
Deadline deadlineToAdd = new Deadline(arg1, arg2); | ||
tasks.add(deadlineToAdd); | ||
chat("Got it, I've added this task:\n " + deadlineToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
case COMMAND_EVENT: | ||
Event eventToAdd = new Event(arg1, arg2); | ||
tasks.add(eventToAdd); | ||
chat("Got it, I've added this task:\n " + eventToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
case COMMAND_MARK: | ||
mark(Integer.parseInt(arg1)); | ||
break; | ||
case COMMAND_UNMARK: | ||
unmark(Integer.parseInt(arg1)); | ||
break; | ||
default: | ||
error("No or invalid command given, are you just looking to chat? :D"); | ||
break; | ||
} | ||
} catch (MissingArgumentException e) { | ||
chat(e.getLocalizedMessage() + "\n"); | ||
} catch (InvalidCommandException e) { | ||
chat(e.getLocalizedMessage() + "\n"); | ||
} | ||
} | ||
/*String[] splitUserArgs = userArgs.split("/by", 2); | ||
String dateTime = ""; | ||
String description = splitUserArgs[0]; | ||
if (splitUserArgs.length > 1) { | ||
dateTime = splitUserArgs[1]; | ||
} */ | ||
switch(command) { | ||
} | ||
|
||
public static String[] parseCommand(String userCommand) throws InvalidCommandException { | ||
String[] parsedCommand = {"","",""}; | ||
String splitUserStatement[] = userCommand.split(" ", 2); | ||
if (!isValidCommand(splitUserStatement[0])) { | ||
throw new InvalidCommandException("Thats not an available command."); | ||
} | ||
String command = splitUserStatement[0]; | ||
parsedCommand[0] = command; | ||
String userArgs = ""; | ||
if (splitUserStatement.length > 1) { | ||
userArgs = splitUserStatement[1]; | ||
} | ||
String[] splitUserArgs = {"",""}; | ||
if (command.equals(COMMAND_DEADLINE)) { | ||
splitUserArgs = userArgs.split("/by", 2); | ||
} | ||
else if (command.equals(COMMAND_EVENT)) { | ||
splitUserArgs = userArgs.split("/at", 2); | ||
} else { | ||
splitUserArgs = userArgs.split("/", 2); | ||
} | ||
parsedCommand[1] = splitUserArgs[0]; | ||
if (splitUserArgs.length > 1) { | ||
parsedCommand[2] = splitUserArgs[1]; | ||
} | ||
return parsedCommand; | ||
|
||
} | ||
|
||
public static boolean isValidCommand(String command) { | ||
switch(command) { //no breaks as all cases lead to return | ||
case COMMAND_LIST: | ||
chat("Here are the tasks in your list: \n" + list()); | ||
break; | ||
return true; | ||
case COMMAND_BYE: | ||
chat("Bye! Hope to see you again!\n"); | ||
isEnd = true; | ||
break; | ||
return true; | ||
case COMMAND_TODO: | ||
ToDo toDoToAdd = new ToDo(userArgs); | ||
tasks.add(toDoToAdd); | ||
chat("Got it, I've added this task:\n " + toDoToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
case COMMAND_DEADLINE: | ||
String[] splitUserArgs = userArgs.split("/by", 2); | ||
String dateTime = ""; | ||
String description = splitUserArgs[0]; | ||
if (splitUserArgs.length > 1) { | ||
dateTime = splitUserArgs[1]; | ||
} | ||
Deadline deadlineToAdd = new Deadline(description, dateTime); | ||
tasks.add(deadlineToAdd); | ||
chat("Got it, I've added this task:\n " + deadlineToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
case COMMAND_EVENT: | ||
String[] splitUserArgs2 = userArgs.split("/at", 2); | ||
String dateTime2 = ""; | ||
String description2 = splitUserArgs2[0]; | ||
if (splitUserArgs2.length > 1) { | ||
dateTime2 = splitUserArgs2[1]; | ||
} | ||
Event eventToAdd = new Event(description2, dateTime2); | ||
tasks.add(eventToAdd); | ||
chat("Got it, I've added this task:\n " + eventToAdd + "\n" + outputNumOfTasks()); | ||
break; | ||
return true; | ||
case COMMAND_DEADLINE: | ||
return true; | ||
case COMMAND_EVENT: | ||
return true; | ||
case COMMAND_MARK: | ||
mark(Integer.parseInt(userArgs)); | ||
break; | ||
return true; | ||
case COMMAND_UNMARK: | ||
unmark(Integer.parseInt(userArgs)); | ||
break; | ||
return true; | ||
default: | ||
chat("Not a command, just wanna chat?"); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
public static String list() { | ||
String output = ""; | ||
int count = 1; | ||
if (tasks.size() == 0) { | ||
return "No tasks! Yay!"; | ||
} | ||
for (Task task : tasks) { | ||
output += String.valueOf(count) + ". " + task + "\n"; | ||
count += 1; | ||
|
@@ -111,6 +154,10 @@ public static void chat(String message) { | |
System.out.println(UI_LINE_SPACING + message + UI_LINE_SPACING); | ||
} | ||
|
||
public static void error(String message) { | ||
System.out.println(UI_LINE_SPACING + ":( OOPS: " + message + "\n" + UI_LINE_SPACING); | ||
} | ||
|
||
public static void end() { | ||
System.out.println(UI_LINE_SPACING + "Bye! Hope to see you again!" + UI_LINE_SPACING); | ||
isEnd = true; | ||
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. Overall code is clear and the logic flows and should have no problem completing the IP |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class InvalidCommandException extends Exception{ | ||
|
||
public InvalidCommandException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class MissingArgumentException extends Exception { | ||
|
||
public MissingArgumentException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
public class Parser { | ||
|
||
private static final String COMMAND_LIST = "list"; | ||
private static final String COMMAND_BYE = "bye"; | ||
private static final String COMMAND_TODO = "todo"; | ||
private static final String COMMAND_DEADLINE = "deadline"; | ||
private static final String COMMAND_EVENT = "event"; | ||
private static final String COMMAND_MARK = "mark"; | ||
private static final String COMMAND_UNMARK = "unmark"; | ||
|
||
String type; | ||
String description; | ||
String dateTime; | ||
|
||
|
||
public Parser(String userInput) throws InvalidCommandException { | ||
String splitUserStatement[] = userInput.split(" ", 2); | ||
String command = splitUserStatement[0]; | ||
switch (command) { | ||
|
||
} | ||
String userArgs = ""; | ||
if (splitUserStatement.length > 1) { | ||
userArgs = splitUserStatement[1]; | ||
} | ||
String[] splitUserArgs = userArgs.split("/by", 2); | ||
String dateTime = ""; | ||
String description = splitUserArgs[0]; | ||
if (splitUserArgs.length > 1) { | ||
dateTime = splitUserArgs[1]; | ||
} | ||
} | ||
|
||
public static void parseUserInput(String userInput) { | ||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
public class Task { | ||
protected String type; | ||
protected String description; | ||
protected String dateTime; | ||
protected boolean isDone; | ||
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. Seems that there is an extra white space followed by "protected" according to coding standard. 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. Yes this violates the coding standard |
||
|
||
Task (String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
Task (String description, boolean isDone) { | ||
Task (String type, String description, String dateTime) { | ||
this.description = description; | ||
this.isDone = isDone; | ||
this.dateTime = dateTime; | ||
this.isDone = false; | ||
} | ||
|
||
public String getDescription() { | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.InputMismatchException; | ||
import java.util.Scanner; | ||
|
||
public class Test { | ||
public static void main(String[] args) { | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
---------------------------------------- | ||
Hello! I'm Duke | ||
What can I do for you? | ||
---------------------------------------- | ||
|
||
---------------------------------------- | ||
Got it, I've added this task: | ||
[T][ ] work | ||
Now you have 1 tasks in the list | ||
---------------------------------------- | ||
|
||
---------------------------------------- | ||
Got it, I've added this task: | ||
[D][ ] quiz (by: sunday) | ||
Now you have 2 tasks in the list | ||
---------------------------------------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
todo work | ||
deadline quiz /by sunday |
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.
can consider making this a private static final variable