Skip to content

Commit

Permalink
Merge pull request #92 from Raghav-B/master
Browse files Browse the repository at this point in the history
Add B-FindFreeTimes
  • Loading branch information
Raghav-B authored Sep 19, 2019
2 parents 28a3108 + 177ad3f commit a9f4039
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 20 deletions.
8 changes: 5 additions & 3 deletions data/saved_tasks.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
0/E/0/pizza party /20/09/2019 1800 to 20/09/2019 2000
1/E/0/pon lecture /20/09/2019 1600 to 20/09/2019 1800
2/E/0/not do work /20/09/2019 2359 to 23/09/2019 0800
0/E/0/pizza party /20/09/2019 0600 to 20/09/2019 0800
1/E/0/pon lecture /20/09/2019 0900 to 20/09/2019 1000
2/E/0/not do work /20/09/2019 1200 to 20/09/2019 1500
3/E/0/die /20/09/2019 2100 to 20/09/2019 2300
4/E/0/ohman /20/09/2019 0200 to 20/09/2019 1900
9 changes: 9 additions & 0 deletions src/main/java/duke/EventDateTimeComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package duke;
import duke.items.Event;
import java.util.Comparator;

public class EventDateTimeComparator implements Comparator<Event> {
public int compare(Event first, Event second) {
return first.getEventStartTimeObj().getAt().compareTo(second.getEventStartTimeObj().getAt());
}
}
11 changes: 5 additions & 6 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package duke;

import duke.commands.AddCommand;
import duke.commands.Command;
import duke.commands.FindCommand;
import duke.commands.NumCommand;
import duke.commands.ViewScheduleCommand;
import duke.commands.*;
import duke.exceptions.InsufficientInfoException;
import duke.exceptions.BadInputException;

Expand Down Expand Up @@ -105,7 +101,6 @@ private Command handleListInput(String listInput) throws BadInputException,
//Commands which require numerical input.
case "done":
command = new NumCommand(Command.CommandType.DONE, Integer.parseInt(keyword[1]));

break;
case "delete": {
command = new NumCommand(Command.CommandType.DELETE, Integer.parseInt(keyword[1]));
Expand All @@ -115,6 +110,10 @@ private Command handleListInput(String listInput) throws BadInputException,
command = new NumCommand(Command.CommandType.SNOOZE, Integer.parseInt(keyword[1]));
break;
}
case "findfreetime": {
command = new FindFreeTimeCommand(Command.CommandType.FINDFREETIME, Integer.parseInt(keyword[1]));
break;
}

//Commands which require string input.
case "todo":
Expand Down
96 changes: 87 additions & 9 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
package duke;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import java.util.*;
import duke.exceptions.BadInputException;
import duke.items.Task;
import duke.items.Todo;
import duke.items.Deadline;
import duke.items.Event;
import duke.items.Snooze;
import duke.items.*;

/**
* Manages the list of (different types of classes),
Expand Down Expand Up @@ -99,6 +92,7 @@ public void addEventItem(int index, String event, String at, String doAfter) {
System.out.println("Event item added: " + event);
System.out.println("Event happens at: " + at);
setListIndex(index + 1); //Next open index.

} catch (BadInputException e) {
System.out.println(e);
}
Expand Down Expand Up @@ -232,4 +226,88 @@ public void printReminders() {
}
}
}

public void findFreeHours(int reqFreeHours) {
if (reqFreeHours < 0) {
System.out.println("Please enter an hour value >= 0.");
return;
}

// Creating temporary ArrayList of events.
ArrayList<Event> eventSortedList = new ArrayList<>();
for (int i = 0; i < taskList.size(); i++) {
if (taskList.get(i) instanceof Event) {
eventSortedList.add((Event) taskList.get(i));
}
}
eventSortedList.sort(new EventDateTimeComparator());
if (eventSortedList.size() <= 1) {
System.out.println("You need at least 2 events to run this command!");
return;
}
// Array is definitely sorted at this point.

// Printing out for testing purposes.
for (int i = 0; i < eventSortedList.size(); i++) {
System.out.println(eventSortedList.get(i).toString());
}

DateTime latestEndTime = eventSortedList.get(0).getEventEndTimeObj();
Event eventBeforeFreeTime = eventSortedList.get(0);
int curMaxFreeHours = 0;
boolean freeTimeFound = false;

for (int i = 1; i < eventSortedList.size(); i++) {
DateTime nextStartTime = eventSortedList.get(i).getEventStartTimeObj();
DateTime nextEndTime = eventSortedList.get(i).getEventEndTimeObj();

int compare = latestEndTime.getAt().compareTo(nextStartTime.getAt());
// latestEndTime is earlier than nextStartTime
if (compare < 0) {
// Getting number of hours between latestEndTime and nextStartTime
long ms = nextStartTime.getAt().getTime() - latestEndTime.getAt().getTime();
int potentialMaxFreeHours = Math.round((float)ms / (1000 * 60 * 60));
System.out.println(potentialMaxFreeHours);

if (potentialMaxFreeHours >= curMaxFreeHours) {
curMaxFreeHours = potentialMaxFreeHours;
eventBeforeFreeTime = eventSortedList.get(i - 1);
}

// Since curEndTime is earlier than or equal to nextStartTime, it is guaranteed that
// our latestEndTime will be equiv to nextEndTime - since this definitely
// takes place after curEndTime.
latestEndTime = nextEndTime;

if (curMaxFreeHours >= reqFreeHours) {
eventBeforeFreeTime = eventSortedList.get(i - 1);
freeTimeFound = true;
break;
}
}

// If curEndTime is later than or equal to nextStartTime - this only happens in the
// event of a clash between events, i.e. events running concurrently.
else {
// Assuming the (next) clashing event takes place perfectly within the current event
// we keep the value of latestEndTime untouched.
if (nextEndTime.getAt().getTime() <= latestEndTime.getAt().getTime()) {
// Leave latestEndTime untouched.
}

// Else, if the clashing event happens to end after the current event, we need to update
// out latestEndTime value accordingly.
else {
latestEndTime = nextEndTime;
}
}
}

if (!freeTimeFound) {
eventBeforeFreeTime = eventSortedList.get(eventSortedList.size() - 1);
}

System.out.println("The earliest free time I found was after the following event:\n +" +
eventBeforeFreeTime.toString());
}
}
2 changes: 1 addition & 1 deletion src/main/java/duke/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class Command {

public enum CommandType {
TODO, DEADLINE, EVENT, BYE, LIST, REMINDER, SNOOZE, DONE, DELETE, FIND, BAD, VIEW
TODO, DEADLINE, EVENT, BYE, LIST, REMINDER, SNOOZE, DONE, DELETE, FIND, BAD, VIEW, FINDFREETIME
}

protected CommandType type;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/duke/commands/FindFreeTimeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke.commands;

import duke.Storage;
import duke.TaskList;
import duke.Ui;

public class FindFreeTimeCommand extends Command {
int reqFreeHours;

public FindFreeTimeCommand(CommandType type, int reqFreeHours) {
super(type);
this.reqFreeHours = reqFreeHours;
}

@Override
public void execute(TaskList list, Ui ui, Storage storage) {
list.findFreeHours(reqFreeHours);
}
}
1 change: 0 additions & 1 deletion src/main/java/duke/items/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.util.Calendar;
import java.util.Date;
import java.util.List;
//TODO: Import an existing datetime class or write a better one.
/**
* Stores date and time information by field - day, month, year, hour, minute.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/duke/items/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public Date getDate() {
}


public DateTime getEventStartTimeObj() {
return eventStartTime;
}

public DateTime getEventEndTimeObj() {
return eventEndTime;
}

@Override
public void snooze() {
Calendar newStartDate = Calendar.getInstance();
Expand Down

0 comments on commit a9f4039

Please sign in to comment.