Skip to content

Commit

Permalink
Implement Calendar (#98)
Browse files Browse the repository at this point in the history
* Add Calendar

Modify DateTime, TaskList to accomodate Calendar.
Add CalendarSelectorException.
  • Loading branch information
TCK1997 authored and rssujay committed Oct 17, 2019
1 parent 0c956cc commit 37afeb6
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 5 deletions.
84 changes: 82 additions & 2 deletions src/main/java/spinbox/DateTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTime implements Comparable<DateTime> {
Expand Down Expand Up @@ -31,15 +32,44 @@ public Date getDateTime() {
return dateTime;
}

/**
* Return the day of the month.
* @return day of the month
*/
public int getDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
return calendar.get(Calendar.DAY_OF_MONTH);
}

/**
* Return the day of the week.
* @return day of the week
*/
public int getDayOfWeek() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
return calendar.get(Calendar.DAY_OF_WEEK);
}

/**
* Return hour.
* @return hour
*/
public int getHour() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
return calendar.get(Calendar.HOUR_OF_DAY);
}

/**
* Converts the Date object back to the string version in the format of MM/dd/yyyy HH:mm
* This can be reused to create an identical dateTime object.
* @return String equivalent of Date object.
*/
public String toString() {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm");
String dateString = dateFormat.format(dateTime);
return dateString;
return dateFormat.format(dateTime);
}

public boolean before(DateTime dateTime) {
Expand All @@ -59,5 +89,55 @@ public int compareTo(DateTime dateTimeTwo) {
return this.getDateTime().compareTo(dateTimeTwo.getDateTime());
}

/**
* Return another DateTime with date
* set as start of the week relative to this DateTime.
* @return start of the week
*/
public DateTime getStartOfTheWeek() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
calendar.add(Calendar.DATE, -1);
}
return new DateTime(calendar.getTime());
}

/**
* Return another DateTime with date
* set as end of the week relative to this DateTime.
* @return end of the week
*/
public DateTime getEndOfTheWeek() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
calendar.add(Calendar.DATE, 1);
}
return new DateTime(calendar.getTime());
}

/**
* Return another DateTime with date
* set as start of the month relative to this DateTime.
* @return start of the month
*/
public DateTime getStartOfTheMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
calendar.set(Calendar.DAY_OF_MONTH, 1);
return new DateTime(calendar.getTime());
}

/**
* Return another DateTime with date
* set as end of the month relative to this DateTime.
* @return end of the month
*/
public DateTime getEndOfTheMonth() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateTime);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return new DateTime(calendar.getTime());
}
}
24 changes: 23 additions & 1 deletion src/main/java/spinbox/containers/lists/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,29 @@ public List<String> viewList() {
List<String> output = new ArrayList<>();
output.add("Here are the tasks in your module:");
for (int i = 0; i < list.size(); i++) {
output.add((Integer.toString(i + 1) + ". " + list.get(i).toString()));
output.add(((i + 1) + ". " + list.get(i).toString()));
}
return output;
}

/**
* Return list of task that overlaps with start and end interval.
* @param startInterval start of the interval.
* @param endInterval end of the interval.
* @return list of task
*/
public List<String> viewListInterval(DateTime startInterval, DateTime endInterval) {
Task currentTask;
List<String> output = new ArrayList<>();
output.add("Here are the task in your module:");
for (int i = 0; i < list.size(); i++) {
currentTask = list.get(i);
if (currentTask.isSchedulable()) {
Schedulable task = (Schedulable) currentTask;
if (task.isOverlapping(startInterval, endInterval)) {
output.add(((i + 1) + ". " + task.toString()));
}
}
}
return output;
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/spinbox/entities/Calendar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spinbox.entities;

import spinbox.DateTime;
import spinbox.containers.lists.TaskList;
import spinbox.exceptions.CalendarSelectorException;

import java.util.List;

public class Calendar {
private static final String MIDNIGHT = " 00:00";
private static final String BEFORE_MIDNIGHT = " 23:59";
DateTime startDate;
DateTime endDate;
int modifier;

public Calendar(int modifier, String date) throws CalendarSelectorException {
this.modifier = modifier;
setDates(date);
}

private void setDates(String date) throws CalendarSelectorException {
switch (modifier) {
case 1:
startDate = new DateTime(date + MIDNIGHT);
endDate = new DateTime(date + BEFORE_MIDNIGHT);
break;
case 2:
startDate = new DateTime(date + MIDNIGHT).getStartOfTheWeek();
endDate = new DateTime(date + BEFORE_MIDNIGHT).getEndOfTheWeek();
break;
case 3:
startDate = new DateTime(date + MIDNIGHT).getStartOfTheMonth();
endDate = new DateTime(date + BEFORE_MIDNIGHT).getEndOfTheMonth();
break;
default:
throw new CalendarSelectorException();
}
}

public List<String> tasksInCalendar(TaskList taskList) {
return taskList.viewListInterval(startDate, endDate);
}
}
10 changes: 10 additions & 0 deletions src/main/java/spinbox/exceptions/CalendarSelectorException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package spinbox.exceptions;

public class CalendarSelectorException extends SpinBoxException {
private static final String ERROR_MESSAGE = "Error: Please supply 1, 2, 3 to corresponding"
+ " to day, month or year for calendar selection";

public CalendarSelectorException() {
super(ERROR_MESSAGE);
}
}
53 changes: 51 additions & 2 deletions src/test/java/DateTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import spinbox.exceptions.SpinBoxException;
import org.junit.jupiter.api.Test;

import java.time.DayOfWeek;

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

public class DateTimeTest {

@Test
public void correctDateInputShouldCreateReusableString() throws SpinBoxException {
public void dateTimeCreation_variousDateTimeString_successfulCreationAndExpectedStringOutput() {
DateTime test = new DateTime("05/02/2020 12:00");
assertEquals("05/02/2020 12:00", test.toString());

Expand All @@ -23,7 +25,7 @@ public void correctDateInputShouldCreateReusableString() throws SpinBoxException
}

@Test
public void parseDifferentDateFormattedString() throws SpinBoxException {
public void dateTimeCreation_differentlyFormattedDateTimeString_successfulCreationAndExpectedStringOutput() {
DateTime test = new DateTime("the day before 12/16/2019 4pm");
assertEquals("12/15/2019 16:00", test.toString());

Expand All @@ -38,4 +40,51 @@ public void parseDifferentDateFormattedString() throws SpinBoxException {
assertEquals("12/25/2019 06:00", santaVacationStartDate.toString());
assertEquals("12/24/2020 23:59", santaVacationEndDate.toString());
}

@Test
public void getDayOfMonth_DateTimeString_expectedDay() {
DateTime test = new DateTime("10/16/2019 12:33");
assertEquals(16, test.getDayOfMonth());
}

@Test
public void getDayOfWeek_DateTimeString_expectedDay() {
DateTime test = new DateTime("10/16/2019 12:33");
assertEquals(4, test.getDayOfWeek());
}

@Test
public void getHour_DateTimeString_expectedHour() {
String date = "10/16/2019 12:33";
DateTime test = new DateTime(date);
assertEquals(12, test.getHour());
}

@Test
public void getStartOfTheWeek_DateTimeString_expectedDateTimeString() {
String date = "10/16/2019";
DateTime test = new DateTime(date + " 00:00");
assertEquals("10/13/2019 00:00", test.getStartOfTheWeek().toString());
}

@Test
public void getEndOfTheWeek_DateTimeString_expectedDateTimeString() {
String date = "10/16/2019";
DateTime test = new DateTime(date + " 23:59");
assertEquals("10/19/2019 23:59", test.getEndOfTheWeek().toString());
}

@Test
public void getStartOfTheMonth_DateTimeString_expectedDateTimeString() {
String date = "10/16/2019";
DateTime test = new DateTime(date + " 00:00");
assertEquals("10/01/2019 00:00", test.getStartOfTheMonth().toString());
}

@Test
public void getEndOfTheMonth_DateTimeString_expectedDateTimeString() {
String date = "10/16/2019";
DateTime test = new DateTime(date + " 23:59");
assertEquals("10/31/2019 23:59", test.getEndOfTheMonth().toString());
}
}

0 comments on commit 37afeb6

Please sign in to comment.