Skip to content

Commit

Permalink
Add Incorrect statement handling (#9)
Browse files Browse the repository at this point in the history
* Add Exception comments

* Add case insensitivity feature

* Add IncorrectStatementException
  • Loading branch information
Ellieyee authored Aug 22, 2019
1 parent e2c49ea commit be6901a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/main/java/DeadlineTask.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
public class DeadlineTask extends Task {
private static final String DEFAULT_DEADLINE_ICON = "[D]";
private static final String DEADLINE_TASK_STATEMENT = "by";
private String deadLine;

public DeadlineTask(String taskName, String deadLine) {
super(taskName, DEFAULT_DEADLINE_ICON);
this.deadLine = deadLine;
}

public static void verifyTaskStatement(String statement) throws IncorrectStatementException {
if(!statement.equals(DEADLINE_TASK_STATEMENT)) {
throw new IncorrectStatementException(statement, DEADLINE_TASK_STATEMENT);
}
}

@Override
public String toString() {
return super.toString() + " (by: " + this.deadLine + ")";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public static void main(String[] args) {
/**
* Adds commands to lists and runs required commands
*/
static void runEvents() {
private static void runEvents() {
tasklist = new TaskList();
Scanner sc = new Scanner(System.in);
String command = sc.nextLine().trim();
while (!command.equals("bye")) {
while (!command.toLowerCase().equals("bye")) {
logicManager.executeCommand(tasklist, command);
command = sc.nextLine().trim();
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/EventTask.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
public class EventTask extends Task {
private static final String DEFAULT_EVENT_ICON = "[E]";
private static final String EVENT_TASK_STATEMENT = "at";
private String duration;

public EventTask(String taskName, String duration) {
super(taskName, DEFAULT_EVENT_ICON);
this.duration = duration;
}

public static void verifyTaskStatement(String statement) throws IncorrectStatementException {
if(!statement.equals(EVENT_TASK_STATEMENT)) {
throw new IncorrectStatementException(statement, EVENT_TASK_STATEMENT);
}
}

@Override
public String toString() {
return super.toString() + " (at: " + this.duration + ")";
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/IncorrectStatementException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class IncorrectStatementException extends InvalidCommandError {
private String actualStatement;
private String givenStatement;
public IncorrectStatementException(String givenStatement, String actualStatement) {
super(actualStatement);
this.actualStatement = actualStatement;
this.givenStatement = givenStatement;
}

@Override
public String getMessage() {
return "Sorry that statement is invalid. Try using \"/" + this.actualStatement + "\" instead of \"/"
+ this.givenStatement + "\".";
}
}
21 changes: 11 additions & 10 deletions src/main/java/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ public class LogicManager {
private TaskList taskList;
public void executeCommand(TaskList taskList, String command) {
this.taskList = taskList;
command = command.trim();
String[] commandDescription = command.split("\\s+", 2);
String[] commandDescription = command.trim().split("\\s+", 2);
commandDescription[0] = commandDescription[0].toLowerCase();
String taskName = commandDescription[0];
try {
switch (commandDescription[0]) {
switch (taskName) {
case "list":
this.printList(command);
break;
Expand Down Expand Up @@ -37,7 +38,7 @@ public void executeCommand(TaskList taskList, String command) {
/**
* Adds new task (without date) into list
* @param commandDescription - array of strings containing command description
* @throws IncompleteCommandError
* @throws IncompleteCommandError - throws error if the command is not in complete format
*/
private void addTaskWithoutDate(String[] commandDescription) throws IncompleteCommandError {
this.checkCommandEmpty(commandDescription);
Expand All @@ -47,15 +48,15 @@ private void addTaskWithoutDate(String[] commandDescription) throws IncompleteCo
/**
* Adds new task (with date) into list
* @param commandDescription - array of strings containing command description
* @throws UnknownCommandException
* @throws IncompleteCommandError - throws error if the command is not in correct format
*/
private void addTaskWithDate(String[] commandDescription) throws UnknownCommandException {
this.checkCommandEmpty(commandDescription);
try {
String[] taskArray = commandDescription[1].split("/", 2);
String taskName = taskArray[0].trim();
String date = taskArray[1].split("\\s+", 2)[1];
this.taskList.add(commandDescription[0], taskName, date);
String[] statementAndDate = taskArray[1].split("\\s+", 2);
this.taskList.add(commandDescription[0], taskName, statementAndDate[1], statementAndDate[0]);
} catch (IndexOutOfBoundsException e) {
throw new IncompleteCommandError("incomplete", commandDescription[0]);
}
Expand All @@ -65,7 +66,7 @@ private void addTaskWithDate(String[] commandDescription) throws UnknownCommandE
* Deletes existing task from list
* @param commandDescription - array of strings containing command description
* @throws RuntimeException - contains both NumberFormatException and IndexOutOfBoundsException
* @throws IncompleteCommandError
* @throws IncompleteCommandError - throws error if the command is not in complete format
*/
private void deleteTask(String[] commandDescription) throws RuntimeException, IncompleteCommandError {
this.checkCommandEmpty(commandDescription);
Expand All @@ -86,7 +87,7 @@ private void markTaskDone(String[] commandDescription) throws RuntimeException,

/**
* Prints out contents of list according to order of insertion
* @throws InvalidCommandError
* @throws InvalidCommandError - throws error if the command is in wrong format
*/
private void printList(String command) throws InvalidCommandError {
if (!command.equals("list")) { throw new InvalidCommandError(command); }
Expand All @@ -96,7 +97,7 @@ private void printList(String command) throws InvalidCommandError {
/**
* Throws error if the given command is empty
* @param commandDescription - array of strings containing command description
* @throws IncompleteCommandError
* @throws IncompleteCommandError - throws error if the command is not in complete format
*/
private void checkCommandEmpty(String[] commandDescription) throws IncompleteCommandError {
if(commandDescription.length == 1) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ public void add(String taskName) {
* @param taskType - Type description of task
* @param taskName - Name of task
* @param date - Date of which the task needs to be completed
* @throws UnknownCommandException
* @param taskStatement - statement required by the task. E.g deadline: by
*/
public void add(String taskType, String taskName, String date) throws UnknownCommandException {
public void add(String taskType, String taskName, String date, String taskStatement) throws IncorrectStatementException {
switch (taskType) {
case "deadline":
DeadlineTask.verifyTaskStatement(taskStatement);
this.list.add(new DeadlineTask(taskName, date));
break;
case "event":
EventTask.verifyTaskStatement(taskStatement);
this.list.add(new EventTask(taskName, date));
break;
default:
throw new UnknownCommandException(taskType);
}
Message.successfulAddMessage(this.list.get(list.size()-1), this.size());
}

/**
* Marks task at given index as done
* @param idx - Index of task in list
* @throws IndexOutOfBoundsException
* @throws IndexOutOfBoundsException - Throws error if given index is out of list bounds
*/
public void done(int idx) throws IndexOutOfBoundsException {
this.list.get(idx).markDone();
Expand All @@ -49,7 +49,7 @@ public void done(int idx) throws IndexOutOfBoundsException {
/**
* Marks task at given index as done
* @param idx - Index of task in list
* @throws IndexOutOfBoundsException
* @throws IndexOutOfBoundsException - Throws error if given index is out of list bounds
*/
public void delete(int idx) throws IndexOutOfBoundsException {
Task task = this.list.get(idx);
Expand Down

0 comments on commit be6901a

Please sign in to comment.