Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions src/nyc/c4q/ac21/calendar/CalendarPrinter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nyc.c4q.ac21.calendar;

import java.util.Calendar;
import java.util.HashMap;

public class CalendarPrinter
{
Expand All @@ -25,10 +26,53 @@ public class CalendarPrinter
* The date containing the month to print.
*/
public static void printMonthCalendar(Calendar date) {
// FIXME: Write this.
// Use these methods to help you:
// DateTools.getMonthNames()
// DateTools.getNextDay() to loop through days in the month.
//System.out.println(date);
System.out.println();

HashMap <Integer, String> month = DateTools.getMonthNames(); //contains names of months

int currentDay = date.get(Calendar.DAY_OF_MONTH);
int currentMonth = date.get(Calendar.MONTH);
int currentYear = date.get(Calendar.YEAR);

Calendar currentDayToPrint = Calendar.getInstance();
currentDayToPrint.set(currentYear,currentMonth,1);//will all the calendar to print starting the first day of the month


System.out.println(month.get(currentMonth) + " " + currentYear); // Prints out current month and year

int spaces = currentDayToPrint.get(Calendar.DAY_OF_WEEK);

for(int i = 1; i < spaces; i++) { //will add spaces to first week line depending on what day the first day of the month starts on.
System.out.print(" ");
}

while (true) { //prints calendar
int day = currentDayToPrint.get(Calendar.DAY_OF_MONTH);

if(day < 10) {//will determine whether it needs to add an extra space to number for alignment.
System.out.print(" ");
}

if(day == currentDay) { //will print a star next to current day
System.out.print(" *");
} else {
System.out.print(" ");
}

System.out.print(day);

if(currentDayToPrint.get(Calendar.DAY_OF_WEEK) == 7) {
System.out.println();
}

Calendar nextDay = DateTools.getNextDay(currentDayToPrint);
int nextDayMonth = nextDay.get(Calendar.MONTH);
if(nextDayMonth != currentMonth) {// checks to see if new month has started
break;
}
currentDayToPrint = nextDay;
}
}

}
}
68 changes: 58 additions & 10 deletions src/nyc/c4q/ac21/calendar/DST.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,43 @@
*/
public class DST {


/**
* Populates hash maps with the start and end time for DST in each year.
* @param startDates
* A hash map of the start date of DST in each year.
* @param endDates
* A hash map of the end date of DST in each year.
*
* @param startDates A hash map of the start date of DST in each year.
* @param endDates A hash map of the end date of DST in each year.
*/
public static void getDSTDates(HashMap<Integer, Calendar> startDates, HashMap<Integer, Calendar> endDates) {

int intNum = 0;

ArrayList<String> lines = FileTools.readLinesFromFile("dst.csv");

// Each line is of the form "sign,startDate,endDate". Split it.
for (String line : lines) {
int comma1 = line.indexOf(',');
int minus1 = line.indexOf('-');
int minus2 = line.indexOf(minus1, '-');

String year = line.substring(0, minus1);
Integer intYear = Integer.valueOf(year);

//intNum ++;

String start = line.substring(0, comma1);
Calendar startDate = DateTools.parseDate(start);

String end = line.substring(comma1 + 1);
Calendar endDate = DateTools.parseDate(end);

//Integer num = intNum;

startDates.put(intYear, startDate);
endDates.put(intYear, endDate);

}

// FIXME: Write this code!
// Each line in the file is of the form "start,end", where both dates
// are in the same year. This represents the dates DST starts and
Expand All @@ -28,20 +56,40 @@ public static void getDSTDates(HashMap<Integer, Calendar> startDates, HashMap<In

/**
* Returns true if 'date' is during Daylight Savings Time.
* @param date
* The date to check.
* @return
* True if DST is in effect on this date.
*
* @param date The date to check.
* @return True if DST is in effect on this date.
*/
public static boolean isDST(Calendar date) {

boolean found = false;

// Create hash maps to contain the start and end dates for DST in each year.
HashMap<Integer, Calendar> dstStartDates = new HashMap<Integer, Calendar>();
HashMap<Integer, Calendar> dstEndDates = new HashMap<Integer, Calendar>();
// Populate them.
DST.getDSTDates(dstStartDates, dstEndDates);

// FIXME: Write this code!
return false; // Change this!
}

String strDate = DateTools.formatDate(date);
int minus1 = strDate.indexOf("-");
String year = strDate.substring(0, minus1);
Integer intYear = Integer.valueOf(year);

if (dstStartDates.containsKey(intYear)) {

Calendar startDate = dstStartDates.get(intYear);
Calendar endDate = dstEndDates.get(intYear);

if (date.compareTo(startDate) != -1 && date.compareTo(endDate) != 1)
found = true;
}

else
found = false;

return found;

}
}
40 changes: 37 additions & 3 deletions src/nyc/c4q/ac21/calendar/DateTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static String formatDate(Calendar cal) {
* @return
* The date it represents, or null if the date is incorrectly formatted.
*/
public static Calendar parseDate(String date) {
public static Calendar parseDate(String date) { // -- makes sure input is in proper format
if (date.length() == 10 && date.charAt(4) == '-' && date.charAt(7) == '-') {
try {
int year = Integer.valueOf(date.substring(0, 4));
Expand All @@ -40,7 +40,7 @@ public static Calendar parseDate(String date) {
return new GregorianCalendar(year, month - 1, dayOfMonth);
}
} catch (NumberFormatException exception) {
// Fall through.
// Fall through. -- b/c it is not in proper format
}
}
// Didn't work.
Expand All @@ -64,13 +64,47 @@ public static Calendar getNextDay(Calendar cal) {
*/
public static HashMap<Integer, String> getDayOfWeekNames() {
HashMap<Integer, String> names = new HashMap<Integer, String>();
names.put(Calendar.SUNDAY, "Sunday");
names.put(Calendar.MONDAY, "Monday");
names.put(Calendar.TUESDAY, "Tuesday");
names.put(Calendar.WEDNESDAY, "Wednesday");
names.put(Calendar.THURSDAY, "Thursday");
names.put(Calendar.FRIDAY, "Friday");
names.put(Calendar.SATURDAY, "Saturday");

// FIXME: Write this.
return names;
}

/**
* Builds and returns a map from integers representing days of the month to the names of the days of the month.
* @return
* A map with keys 'Calendar.JANUARY' through 'Calendar.DECEMBER' with corresponding month names as values.
*/
public static HashMap<Integer, String> getMonthNames() {
HashMap<Integer, String> names = new HashMap<Integer, String>();
names.put(Calendar.JANUARY, "January");
names.put(Calendar.FEBRUARY, "February");
names.put(Calendar.MARCH, "March");
names.put(Calendar.APRIL, "April");
names.put(Calendar.MAY, "May");
names.put(Calendar.JUNE, "June");
names.put(Calendar.JULY, "July");
names.put(Calendar.AUGUST, "August");
names.put(Calendar.SEPTEMBER, "September");
names.put(Calendar.OCTOBER, "October");
names.put(Calendar.NOVEMBER, "November");
names.put(Calendar.DECEMBER, "December");

// FIXME: Write this.
return null; // Change this!
return names; // Change this!
}

public static void main(String[] args) {
System.out.println(getDayOfWeekNames());
System.out.println(getDayOfWeekNames().get(Calendar.SUNDAY));
System.out.println(getMonthNames());
System.out.println(getMonthNames().get(Calendar.JANUARY));
}

}
15 changes: 13 additions & 2 deletions src/nyc/c4q/ac21/calendar/Holidays.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ public static HashMap<Calendar, String> getHolidays(String holidayType) {

HashMap<Calendar, String> holidays = new HashMap<Calendar, String>();
for (String line : lines) {
// FIXME: Write this.
// Use DateTools.parseDate.

// Separating data within each line.
int comma1 = line.indexOf(',');
String date = line.substring(0, comma1);
int comma2 = line.indexOf(',', comma1 + 1);
String name = line.substring(comma1 + 1, comma2);
String type = line.substring(comma2 + 1);

// Adding only the lines where type matches holidayType.
if (type.equals(holidayType)) {
holidays.put(DateTools.parseDate(date), name);
}

}
return holidays;
}
Expand Down
43 changes: 36 additions & 7 deletions src/nyc/c4q/ac21/calendar/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
import java.util.HashMap;
import java.util.Scanner;

public class Main {
public class Main
{

public static void main(String[] args) {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("date? ");
String dateString = scanner.nextLine();
Calendar date = DateTools.parseDate(dateString);
if (date == null)
if(date == null)
{
return;
}
System.out.println();

System.out.println("date: " + DateTools.formatDate(date));
Expand All @@ -21,23 +25,48 @@ public static void main(String[] args) {

// 1. Show the day of the week.
HashMap<Integer, String> dayOfWeekNames = DateTools.getDayOfWeekNames();
// ...
for (Integer days : dayOfWeekNames.keySet())
{
if(days.equals(date.get(Calendar.DAY_OF_WEEK))) {
System.out.println("day of week: " + dayOfWeekNames.get(days));
}
}

// 2. Show whether this is a work day.
boolean value = false;
HashMap<Integer, Boolean> workDays = WorkDays.getWorkDays();
// ...
for(Integer days : workDays.keySet()) //iterate through the workday keys in hashmap
{
if(days.equals(date.get(Calendar.DAY_OF_WEEK))) //if a match is found
{
value = workDays.get(days); //get value of key and save it to value variable
break; //leave loop
}
}
//determine if it is a work day or not, then print out appropriate message
System.out.println("work day: " + value);


// 3. Show whether this is a national holiday, and if so, which.
HashMap<Calendar, String> holidays = Holidays.getHolidays("National holiday");
if(holidays.containsKey(date))
{
System.out.println("national holiday: " + holidays.get(date));
}
else
{
System.out.println("national holiday: " + "not a national holiday");
}
// ...

// 4. Show whether this date is in DST.
boolean isDST = DST.isDST(date);
// ...
System.out.println("is DST: " + isDST);


// 5. Show the zodiac sign.
String zodiacSign = Zodiac.getZodiacSign(date);
// ...
System.out.println("Zodiac sign: " + zodiacSign);

// 6. Print out the monthly calendar.
CalendarPrinter.printMonthCalendar(date);
Expand Down
24 changes: 19 additions & 5 deletions src/nyc/c4q/ac21/calendar/WorkDays.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,30 @@
import java.util.HashMap;
import java.util.Scanner;

public class WorkDays {
public class WorkDays
{

/**
* Builds a map from day of week to whether this is a work day.
* @return
* A map with keys 'Calendar.MONDAY' through 'Calendar.SUNDAY', indicating whether each is a work day.
*
* @return A map with keys 'Calendar.MONDAY' through 'Calendar.SUNDAY', indicating whether each is a work day.
*/
public static HashMap<Integer, Boolean> getWorkDays() {
public static HashMap<Integer, Boolean> getWorkDays()
{
// FIXME: Write this.
return null; // Change this!
//Make the HashMap
HashMap<Integer, Boolean> getWorkDays = new HashMap<Integer, Boolean>();
getWorkDays.put(Calendar.SUNDAY, false);
getWorkDays.put(Calendar.MONDAY, true);
getWorkDays.put(Calendar.TUESDAY, true);
getWorkDays.put(Calendar.WEDNESDAY, true);
getWorkDays.put(Calendar.THURSDAY, true);
getWorkDays.put(Calendar.FRIDAY, true);
getWorkDays.put(Calendar.SATURDAY, false);
return getWorkDays;
}


}