Skip to content

Commit

Permalink
Merge pull request #6 from RusdiHaizim/branch-B-Reminders
Browse files Browse the repository at this point in the history
[CS2113T-W13-2]-RusdiHaizim-B-Reminders
  • Loading branch information
RusdiHaizim authored Sep 15, 2019
2 parents b5bc6de + a8f7d7d commit 382e2bf
Show file tree
Hide file tree
Showing 15 changed files with 289 additions and 51 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ run {

dependencies {
compile files("${System.properties['java.home']}/../lib/tools.jar")
compile group: 'com.joestelmach', name: 'natty', version: '0.6'
testImplementation 'org.junit.jupiter:junit-jupiter:5.5.0'
}

Expand Down
5 changes: 2 additions & 3 deletions data/saved_data.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
T|✓|eat food
E|✗|event|now
T|✗|eat
D|✗|a|0600 9/15/2019
D|✗|b|0500 9/15/2019
Empty file added data/test_data.txt
Empty file.
12 changes: 4 additions & 8 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package duke;

import duke.commands.Command;
import duke.commands.AddCommand;
import duke.commands.DeleteCommand;
import duke.commands.DoneCommand;
import duke.commands.ExitCommand;
import duke.commands.FindCommand;
import duke.commands.ListCommand;
import duke.commands.*;
import duke.tasks.Deadline;
import duke.tasks.Event;
import duke.tasks.Task;
Expand All @@ -28,6 +22,8 @@ public static Command parse(String input) throws DukeException {
return new ExitCommand();
} else if (input.equals("list")) {
return new ListCommand();
} else if (input.equals("reminder")) {
return new ReminderCommand();
} else if (input.length() > 4 && input.substring(0, 4).equals("find")) {
return new FindCommand(input);
} else if (input.length() > 4 && input.substring(0, 4).equals("done")) {
Expand Down Expand Up @@ -88,7 +84,7 @@ public static String runTodo(ArrayList<Task> data, String input, int state) {
* 2 : Returns null string with checked task
* @return String which highlights what Duke processed
*/
public static String runDeadline(ArrayList<Task> data, String input, int state) {
public static String runDeadline(ArrayList<Task> data, String input, int state) throws DukeException {
StringBuilder stringBuilder = new StringBuilder();
input = input.substring(9);
int startOfBy = input.indexOf("/");
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/duke/commands/AddCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package duke.commands;

import com.joestelmach.natty.DateGroup;
import duke.DukeException;
import duke.Storage;
import duke.TaskList;
Expand Down Expand Up @@ -42,6 +43,7 @@ public AddCommand(CmdType cmdType, String str) throws DukeException {
*/
@Override
public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException {

switch (type) {
case TODO:
ui.showMessage(Parser.runTodo(tasks.getData(), input, 0));
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/duke/commands/ReminderCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package duke.commands;

import duke.Storage;
import duke.TaskList;
import duke.Ui;
import duke.tasks.Task;

import java.util.ArrayList;

public class ReminderCommand extends Command {

@Override
public void execute(TaskList tasks, Ui ui, Storage storage) {
ArrayList<Task> deadlineList = new ArrayList<>();
for (Task task : tasks.getData()) {
if (task.getTaskType() == Task.TaskType.DEADLINE) {
deadlineList.add(task);
}
}
deadlineList.sort((o1, o2) -> {
if (o1.getDateTime() == null) {
return 1;
} else if (o2.getDateTime() == null) {
return -1;
}
if (o1.getDateTime().before(o2.getDateTime())) {
return -1;
} else if (o1.getDateTime().after(o2.getDateTime())) {
return 1;
}
return 0;
});
int idx = 1;
if (deadlineList.size() > 0) {
ui.showMessage("Here are the upcoming Deadlines:");
for (Task task : deadlineList) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(idx++).append(".");
stringBuilder.append(task.getFullString());
ui.showMessage(stringBuilder.toString());
}
} else {
ui.showError("Empty List!");
}
}
}
23 changes: 14 additions & 9 deletions src/main/java/duke/tasks/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package duke.tasks;

import com.joestelmach.natty.DateGroup;
import com.joestelmach.natty.Parser;
import duke.DukeException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Deadline extends Task {
private String by;
Expand All @@ -14,16 +19,16 @@ public class Deadline extends Task {
* of the task inputted by user
* @param by The details of when task is to be done
*/
public Deadline(String description, String by) {
public Deadline(String description, String by) throws DukeException {
super(description);
this.by = by;

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
sdf.setLenient(false);
taskType = TaskType.DEADLINE;
try {
dateNow = sdf.parse(by);
} catch (ParseException e) {
//throw new DukeException("Tasks.Task does not have dd/MM/yyyy HHmm date-time format!");
Parser parser = new Parser();
List<DateGroup> groups = parser.parse(by);
dateNow = groups.get(0).getDates().get(0);
} catch (Exception e) {
throw new DukeException(" Date cannot be parsed: " + by);
}
}

Expand All @@ -47,8 +52,8 @@ public String getFullString() {
* @return String containing the date of Task
*/
@Override
public String getDateTime() {
return dateNow.toString();
public Date getDateTime() {
return dateNow;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/duke/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Event extends Task {
public Event(String description, String at) {
super(description);
this.at = at;

taskType = TaskType.EVENT;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HHmm");
sdf.setLenient(false);
try {
Expand All @@ -42,8 +42,8 @@ public String toString() {
* @return String containing the date of Task
*/
@Override
public String getDateTime() {
return dateNow.toString();
public Date getDateTime() {
return dateNow;
}

@Override
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/duke/tasks/Task.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package duke.tasks;


import java.util.Date;

public abstract class Task {
String description;
private boolean isDone;
protected TaskType taskType;

public enum TaskType {
TODO, DEADLINE, EVENT
}

/**
* Initialises description of task and sets it to !isDone.
Expand Down Expand Up @@ -42,7 +49,11 @@ public void markAsDone() {
this.isDone = true;
}

public abstract String getDateTime();
public abstract Date getDateTime();

public abstract String getExtra();

public TaskType getTaskType() {
return taskType;
}
}
5 changes: 4 additions & 1 deletion src/main/java/duke/tasks/ToDo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.tasks;

import java.util.Date;

public class ToDo extends Task {

/**
Expand All @@ -9,6 +11,7 @@ public class ToDo extends Task {
*/
public ToDo(String description) {
super(description);
taskType = TaskType.TODO;
}

/**
Expand Down Expand Up @@ -40,7 +43,7 @@ public String getExtra() {
* @return Null String
*/
@Override
public String getDateTime() {
public Date getDateTime() {
return null;
}
}
44 changes: 38 additions & 6 deletions src/test/java/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
import duke.DukeException;
import duke.Parser;
import duke.TaskList;
import duke.commands.Command;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

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

public class DeadlineTest {
private static final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private static final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private static final PrintStream originalOut = System.out;
private static final PrintStream originalErr = System.err;

@BeforeEach
void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}

@AfterEach
void restoreStreams() {
outContent.reset();
System.setOut(originalOut);
System.setErr(originalErr);
}

@Test
public void test(String input) throws DukeException {
void test(String input) throws DukeException {
setUpStreams();
TaskList taskList = new TaskList();
Parser.runDeadline(taskList.getData(), input, 1);
assertEquals(taskList.get(0).getFullString(), "[D][✗] test (by: 0000)");
Command c = Parser.parse(input);
c.execute(taskList, DukeTest.ui, DukeTest.storage);
String exp = "Got it. I've added this task: \n [D][✗] test (by: 0000)\nNow you have 1 tasks in the list.";
assertEquals(exp, outContent.toString().trim());
restoreStreams();
}

@Test
public void examBy_Date(String input) throws DukeException {
void examBy_Date(String input) throws DukeException {
setUpStreams();
TaskList taskList = new TaskList();
Parser.runDeadline(taskList.getData(), input, 1);
assertEquals(taskList.get(0).getFullString(), "[D][✗] exam (by: 01/01/2019)");
Command c = Parser.parse(input);
c.execute(taskList, DukeTest.ui, DukeTest.storage);
String exp = "Got it. I've added this task: \n [D][✗] exam (by: 01/01/2019)\nNow you have 1 tasks in the list.";
assertEquals(exp, outContent.toString().trim());
restoreStreams();
}

}
30 changes: 28 additions & 2 deletions src/test/java/DukeTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import duke.DukeException;
import org.junit.jupiter.api.Test;
import duke.Storage;
import duke.Ui;
import org.junit.jupiter.api.*;
import java.io.File;
import java.io.IOException;

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

public class DukeTest {
public static Ui ui;
public static Storage storage;

private void testSetup() throws DukeException {
new File("./data").mkdir();
File file = new File("./data/test_data.txt");
try {
file.createNewFile();
} catch (IOException error) {
System.out.println("ERROR, FAILED TO CREATE");
}
ui = new Ui();
storage = new Storage("./data/test_data.txt");
}

@Test
public void test() throws DukeException {
dummyTest();

//Setting up configurations
testSetup();

//Tests for todo
new TodoTest().test("todo test");
new TodoTest().test("todo eat");
new TodoTest().jog("todo jog");
new TodoTest().todo("todo todo");

Expand All @@ -20,6 +42,10 @@ public void test() throws DukeException {
//Test for event
new EventTest().test("event test /at 0000");
new EventTest().birthdayAt_myBday("event bday /at 06/06/2019");

//Test for reminders
new ReminderTest().test();

}

public void dummyTest() {
Expand Down
Loading

0 comments on commit 382e2bf

Please sign in to comment.