Skip to content

Commit

Permalink
Merge pull request #87 from cyanoei/master
Browse files Browse the repository at this point in the history
[CS2113T-F09-3]-cyanoei-B-DoAfterTasks
  • Loading branch information
cyanoei authored Sep 19, 2019
2 parents 06d923a + 1f36b61 commit ac6e146
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 41 deletions.
2 changes: 2 additions & 0 deletions data/saved_tasks.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
4/D/1/x /04/05/2000 2000
5/E/0/project meeting /19/9/2019 1000
6/T/0/sleep
7 changes: 6 additions & 1 deletion src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke;

import duke.commands.Command;
import duke.exceptions.BadInputException;

/**
* Duke is a task list that supports 3 types of classes - Todos, deadlines and events.
Expand Down Expand Up @@ -32,7 +33,11 @@ private void run() {
ui.printNewLine();
userInput = ui.read();
command = parser.parse(userInput);
command.execute(taskList, ui, storage);
try {
command.execute(taskList, ui, storage);
} catch (BadInputException e) {
System.out.println(e);
}
} while (command.getType() != Command.CommandType.BYE);

}
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ private String[] addEvent(String input) throws InsufficientInfoException {
}
}

private int processDoAfter(String input) throws BadInputException {
String shortStr;
String[] splitStr;
int taskIndex;

shortStr = input.substring(input.indexOf("/after"));

try {
splitStr = shortStr.split(" ", 3); //splits into "/after" "x" and other stuff, where "x" is an int
taskIndex = Integer.parseInt(splitStr[1]); //check if this is an int
} catch (ArrayIndexOutOfBoundsException | NumberFormatException e) {
throw new BadInputException("Please input the index number of the task that has to be done first.");
}
return taskIndex;
}

/**
* Checks if the command keyword (first word is valid).
Expand All @@ -64,6 +79,17 @@ private String[] addEvent(String input) throws InsufficientInfoException {
private Command handleListInput(String listInput) throws BadInputException,
InsufficientInfoException, NumberFormatException {

/*TODO: Update parser to handle Task requests separately to process optional commands better
eg. doAfter or repeating tasks
*/

int afterIndex;
afterIndex = -1;
if (listInput.contains("/after")) {
afterIndex = processDoAfter(listInput);
listInput = listInput.replace(" /after " + Integer.toString(afterIndex), "");
}

String[] keyword = listInput.split(" ", 2);
Command command;

Expand Down Expand Up @@ -93,16 +119,18 @@ private Command handleListInput(String listInput) throws BadInputException,
//Commands which require string input.
case "todo":
String[] todoTemp = addTodo(keyword[1]);
command = new AddCommand(Command.CommandType.TODO, todoTemp[0], (todoTemp.length > 1) ? todoTemp[1] : "");
command = new AddCommand(Command.CommandType.TODO, todoTemp[0],
(todoTemp.length > 1) ? todoTemp[1] : "", afterIndex);
break;

case "deadline": {
String[] temp = addDeadline(keyword[1]);
command = new AddCommand(Command.CommandType.DEADLINE, temp[0], temp[1]);
command = new AddCommand(Command.CommandType.DEADLINE, temp[0], temp[1], afterIndex);
break;
}
case "event": {
String[] temp = addEvent(keyword[1]);
command = new AddCommand(Command.CommandType.EVENT, temp[0], temp[1]);
command = new AddCommand(Command.CommandType.EVENT, temp[0], temp[1], afterIndex);
break;
}
case "find": {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ public TaskList load() {

switch (item[1]) {
case "T":
savedList.add(new Todo(Integer.parseInt(item[0]), item[3]));

savedList.add(new Todo(Integer.parseInt(item[0]), item[3], ""));
break;
case "D":
savedList.add(new Deadline(Integer.parseInt(item[0]), item[3], item[4]));
savedList.add(new Deadline(Integer.parseInt(item[0]), item[3], item[4], ""));

break;
case "E":
savedList.add(new Event(Integer.parseInt(item[0]), item[3], item[4]));
savedList.add(new Event(Integer.parseInt(item[0]), item[3], item[4], ""));

break;
default:
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public int getListIndex() {
*
* @param todoitem the description of the task.
*/
public void addTodoItem(int index, String todoitem) {
taskList.add(new Todo(index, todoitem)); //Use the constructor to create a new Task.
public void addTodoItem(int index, String todoitem, String doAfter) {
taskList.add(new Todo(index, todoitem, doAfter)); //Use the constructor to create a new Task.
System.out.println("Todo item added: " + todoitem);
setListIndex(index + 1); //Next open index.
}
Expand All @@ -63,8 +63,8 @@ public void addTodoItem(int index, String todoitem) {
* @param todoitem description of the task.
* @param hours duration of task.
*/
public void addTodoItem(int index, String todoitem, int hours) {
taskList.add(new Todo(index, todoitem, hours)); //Use the constructor to create a new Task.
public void addTodoItem(int index, String todoitem, String doAfter, int hours) {
taskList.add(new Todo(index, todoitem, doAfter, hours)); //Use the constructor to create a new Task.
System.out.println("Todo item added: " + todoitem);
System.out.println("Hours needed: " + hours);
setListIndex(index + 1); //Next open index.
Expand All @@ -75,9 +75,10 @@ public void addTodoItem(int index, String todoitem, int hours) {
*
* @param deadline the command with the description and deadline of the task.
*/
public void addDeadlineItem(int index, String description, String deadline) {
public void addDeadlineItem(int index, String description, String deadline, String doAfter) {
try {
taskList.add(new Deadline(index, description, deadline)); //Use the constructor to create a new Task.
//Use the constructor to create a new Task.
taskList.add(new Deadline(index, description, deadline, doAfter));
System.out.println("Deadline item added: " + description);
System.out.println("Deadline is: " + deadline);
setListIndex(index + 1); //Next open index.
Expand All @@ -92,9 +93,9 @@ public void addDeadlineItem(int index, String description, String deadline) {
* @param event the description of the task.
* @param at the time the event happens.
*/
public void addEventItem(int index, String event, String at) {
public void addEventItem(int index, String event, String at, String doAfter) {
try {
taskList.add(new Event(index, event, at)); //Use the constructor to create a new Task.
taskList.add(new Event(index, event, at, doAfter)); //Use the constructor to create a new Task.
System.out.println("Event item added: " + event);
System.out.println("Event happens at: " + at);
setListIndex(index + 1); //Next open index.
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/duke/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,45 @@
*/
public class AddCommand extends Command {

public String description;
public String details;
private String description;
private String details;
private int afterIndex;

/**
* Why does this require a javadoc comment.
*/
public AddCommand(CommandType type, String description, String details) {
public AddCommand(CommandType type, String description, String details, int afterIndex) {
super(type);
this.description = description;
this.details = details;
this.afterIndex = afterIndex;
}


@Override
public void execute(TaskList list, Ui ui, Storage storage) {
int index = list.getListIndex();
public void execute(TaskList list, Ui ui, Storage storage) throws BadInputException {

int index = list.getListIndex(); //To assign the next task's index.
String doAfterWhat = "";
//Section for handling doAfters.
//Using the indexes presented to the user.
if (afterIndex > 0 && afterIndex > list.getSize()) { //Referring to a nonexistent task
throw new BadInputException("This is not a valid task. (to do after)");
} else if (afterIndex > 0) { //afterIndex is -1 if there is nothing to do before.
//Get description of task to doAfter.
doAfterWhat = list.getTaskList().get(afterIndex - 1).getDescription(); //0-indexed list!
}

if (super.type == CommandType.TODO) {
if (!details.equals("")) {
list.addTodoItem(index, description, Integer.parseInt(details));
list.addTodoItem(index, description, doAfterWhat, Integer.parseInt(details));
} else {
list.addTodoItem(index, description);
list.addTodoItem(index, description, doAfterWhat);
}
} else if (super.type == CommandType.DEADLINE) {
list.addDeadlineItem(index, description, details);
list.addDeadlineItem(index, description, details, doAfterWhat);
} else { //Type is event
list.addEventItem(index, description, details);
list.addEventItem(index, description, details, doAfterWhat);
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/duke/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import duke.TaskList;
import duke.Ui;
import duke.Storage;
import duke.exceptions.BadInputException;

/**
* This is an abstract class.
Expand Down Expand Up @@ -36,7 +37,7 @@ public CommandType getType() {
* Prints the list or saves the list and sends exit message.
* Might need to separate into bye and list commands.
*/
public void execute(TaskList list, Ui ui, Storage storage) {
public void execute(TaskList list, Ui ui, Storage storage) throws BadInputException {
if (type == CommandType.LIST) {
list.printList();
} else if (type == CommandType.REMINDER) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/duke/items/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class Deadline extends Task implements Snooze {
/**
* Deadline object has a "by" string as well as a Date object.
*/
public Deadline(int index, String description, String by) throws BadInputException {
super(index, description, TaskType.DEADLINE); //Using the Task constructor. isDone is set to false.
public Deadline(int index, String description, String by, String doAfter) throws BadInputException {
super(index, description, TaskType.DEADLINE, doAfter); //Using the Task constructor. isDone is set to false.
this.doBy = by;
this.doByDate = new DateTime(doBy);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/duke/items/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public class Event extends Task implements Snooze {
/**
* Deadline object has a "at" string as well as a Date objects for start and end times.
*/
public Event(int index, String description, String time) throws BadInputException {
super(index, description, TaskType.EVENT); //Using the Task constructor. isDone is set to false.

public Event(int index, String description, String time, String doAfter) throws BadInputException {
super(index, description, TaskType.EVENT, doAfter); //Using the Task constructor. isDone is set to false.
try {
String[] startEndTime = time.split(" to ", 2);
this.start = startEndTime[0];
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/duke/items/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Task {
protected String description;
protected boolean isDone;
protected TaskType type;
protected String doAfter; //Can extend to a list of tasks that have to be done before.

protected enum TaskType {
TODO, DEADLINE, EVENT
Expand All @@ -29,11 +30,12 @@ public Task() {
/**
* All tasks contain a description and isDone status, and also belong to a type.
*/
public Task(int index, String description, TaskType type) {
public Task(int index, String description, TaskType type, String doAfter) {
this.taskIndex = index;
this.description = description;
this.isDone = false;
this.type = type;
this.doAfter = doAfter;
}

public int getTaskIndex() {
Expand All @@ -45,13 +47,17 @@ public String getDescription() {
}

public String getStatusIcon() {
return (isDone ? "[\u2713]" : "[\u2718]"); //return tick or X symbols
return (isDone ? "[\u2713] " : "[\u2718] "); //return tick or X symbols
}

public boolean getIsDone() {
return isDone;
}

public void setDoAfter(String doAfter) {
this.doAfter = doAfter;
}

public void printTaskDetails() {
System.out.println(toString());
}
Expand Down Expand Up @@ -101,12 +107,20 @@ public Date getDate() {
public String toString() {
String taskType;
if (this.type == TaskType.DEADLINE) {
taskType = "[D]";
taskType = "[D] ";
} else if (this.type == TaskType.TODO) {
taskType = "[T]";
taskType = "[T] ";
} else {
taskType = "[E] ";
}

String after;
if (!doAfter.equals("")) {
after = " (do after: " + doAfter + ")";
} else {
taskType = "[E]";
after = "";
}
return taskType + " " + getStatusIcon() + " " + description; //eg. [✓] read book

return taskType + getStatusIcon() + description + after; //eg. [✓] read book
}
}
10 changes: 5 additions & 5 deletions src/main/java/duke/items/Todo.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ public class Todo extends Task {

private int duration = 0;

public Todo(int index, String description) {
super(index, description, TaskType.TODO); //Using the Task constructor. isDone is set to false.
public Todo(int index, String description, String doAfter) {
super(index, description, TaskType.TODO, doAfter); //Using the Task constructor. isDone is set to false.
}

public Todo(int index, String description, int duration) {
super(index, description, TaskType.TODO);
public Todo(int index, String description, String doAfter, int duration) {
super(index, description, TaskType.TODO, doAfter);
this.duration = duration;
}

Expand All @@ -27,7 +27,7 @@ public String saveDetailsString() {
@Override
public String toString() {
if (duration > 0) {
return super.toString() + " (needs " + duration + "hours)";
return super.toString() + " (needs " + duration + " hours)";
}
return super.toString();
}
Expand Down

0 comments on commit ac6e146

Please sign in to comment.