Skip to content

Commit

Permalink
Merge pull request #8 from leowyh/master
Browse files Browse the repository at this point in the history
Added B-DoWithinPeriodTasks with unit testing
  • Loading branch information
leowyh authored Sep 18, 2019
2 parents 2760b36 + f275db5 commit 089b73f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
45 changes: 37 additions & 8 deletions src/main/java/command/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import common.DukeException;
import common.TaskList;
import task.Deadline;
import task.Event;
import task.Task;
import task.Todo;
import task.*;
import ui.Ui;

import java.text.ParseException;
Expand Down Expand Up @@ -62,19 +59,20 @@ public static boolean parse(String input, TaskList tasklist, Ui ui, Storage stor
} else if (isFind(input)) {
processFind(input, tasklist, ui);

} else if (isWithinPeriodTask(input)) {
processWithin(input, tasklist, ui);
storage.save(tasklist.returnArrayList());
} else {
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(");

}

} catch (DukeException e) {
ui.exceptionMessage(e.getMessage());
return true;
}

return false;
}



/**
* Processes the find command and outputs a list of tasks containing the word.
* @param input Input from the user.
Expand Down Expand Up @@ -198,6 +196,33 @@ private static void processEvent(String input, TaskList tasklist, Ui ui) {
}
}

/**
* Processes the within command and adds a withinPeriodTask to the user's Tasklist.
* @param input Input from the user.
* @param tasklist Tasklist of the user.
* @param ui Ui that interacts with the user.
*/
private static void processWithin(String input, TaskList tasklist, Ui ui) {
try {
String[] splitspace = input.split(" ", 2);
String[] splitslash = splitspace[1].split("/", 2);
String taskDescription = splitslash[0];
String[] splittime = splitslash[1].split(" ", 2);
String[] splitand = splittime[1].split("and ", 2);
String taskstart = splitand[0];
String taskend = splitand[1];
Date formattedtimestart = dataformat.parse(taskstart);
Date formattedtimeend = dataformat.parse(taskend);
WithinPeriodTask withinPeriodTask = new WithinPeriodTask(taskDescription, dataformat.format(formattedtimestart), dataformat.format(formattedtimeend));
tasklist.addTask(withinPeriodTask);
ui.printAddedMessage(withinPeriodTask, tasklist);
} catch (ArrayIndexOutOfBoundsException e) {
ui.exceptionMessage(" ☹ OOPS!!! The description of a withinPeriodTask cannot be empty.");
} catch (ParseException e) {
ui.exceptionMessage(" ☹ OOPS!!! Format of time is wrong.");
}
}


private static boolean isBye(String input) {
return input.equals("bye");
Expand Down Expand Up @@ -230,4 +255,8 @@ private static boolean isDelete(String input) {
private static boolean isFind(String input) {
return input.startsWith("find");
}

private static boolean isWithinPeriodTask(String input) {
return input.startsWith("within");
}
}
2 changes: 1 addition & 1 deletion src/main/java/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Deadline extends Task implements Serializable {
/**
* Creates a Deadline instance and initialises the required attributes.
* @param description Description of the deadline.
* @param by Deadline of the task.
* @param by Deadline of the task in format "dd/MM/yyyy HHmm".
*/
public Deadline(String description, String by) {
super(description);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Event extends Task implements Serializable {
/**
* Creates an Event instance and initialises the required attributes.
* @param description Description of the event.
* @param at Time of the event.
* @param at Time of the event in format "dd/MM/yyyy HHmm".
*/
public Event(String description, String at) {
super(description);
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/task/WithinPeriodTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package task;

import java.io.Serializable;

/**
* Task containing information of a Task to be done within a period.
*/
public class WithinPeriodTask extends Task implements Serializable {
protected String start;
protected String end;

/**
* Creates a WithinPeriodTask instance and initialises the required attributes.
* @param description Description of the task.
* @param start Start time of the task in format "dd/MM/yyyy HHmm".
* @param end End time of the task in format "dd/MM/yyyy HHmm".
*/
public WithinPeriodTask(String description, String start, String end) {
super(description);
this.start = start;
this.end = end;
}

/**
* Returns a string status of the WithinPeriodTask task.
* @return The task's status icon, description, start and end times.
*/
@Override
public String giveTask() {
return "[W]" + super.giveTask() + "(between: " + start + " and " + end + ")";
}
}
13 changes: 13 additions & 0 deletions src/test/java/WithinPeriodTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import org.junit.jupiter.api.Test;
import task.WithinPeriodTask;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class WithinPeriodTaskTest {

@Test
public void WithinPeriodTaskTest() {
WithinPeriodTask withinPeriodTask = new WithinPeriodTask("collect certificate ", "02/12/2019 1800", "05/12/2019 1500" );
assertEquals(withinPeriodTask.giveTask(), "[W][✘] collect certificate (between: 02/12/2019 1800 and 05/12/2019 1500)");
}
}

0 comments on commit 089b73f

Please sign in to comment.