diff --git a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java index 74f7a5e..1170041 100644 --- a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java +++ b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java @@ -1,6 +1,7 @@ package nyc.c4q.ac21.calendar; import java.util.Calendar; +import java.util.HashMap; public class CalendarPrinter { @@ -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 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; + } } -} +} \ No newline at end of file diff --git a/src/nyc/c4q/ac21/calendar/DST.java b/src/nyc/c4q/ac21/calendar/DST.java index ba5a212..c8d723e 100644 --- a/src/nyc/c4q/ac21/calendar/DST.java +++ b/src/nyc/c4q/ac21/calendar/DST.java @@ -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 startDates, HashMap endDates) { + + int intNum = 0; + ArrayList 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 @@ -28,12 +56,14 @@ public static void getDSTDates(HashMap startDates, HashMap dstStartDates = new HashMap(); HashMap dstEndDates = new HashMap(); @@ -41,7 +71,25 @@ public static boolean isDST(Calendar date) { 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; + + } } diff --git a/src/nyc/c4q/ac21/calendar/DateTools.java b/src/nyc/c4q/ac21/calendar/DateTools.java index e391fd7..7ebc94e 100644 --- a/src/nyc/c4q/ac21/calendar/DateTools.java +++ b/src/nyc/c4q/ac21/calendar/DateTools.java @@ -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)); @@ -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. @@ -64,13 +64,47 @@ public static Calendar getNextDay(Calendar cal) { */ public static HashMap getDayOfWeekNames() { HashMap names = new HashMap(); + 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 getMonthNames() { + HashMap names = new HashMap(); + 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)); } } diff --git a/src/nyc/c4q/ac21/calendar/Holidays.java b/src/nyc/c4q/ac21/calendar/Holidays.java index c30f247..c64837a 100644 --- a/src/nyc/c4q/ac21/calendar/Holidays.java +++ b/src/nyc/c4q/ac21/calendar/Holidays.java @@ -23,8 +23,19 @@ public static HashMap getHolidays(String holidayType) { HashMap holidays = new HashMap(); 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; } diff --git a/src/nyc/c4q/ac21/calendar/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index 8e1fa0d..593a62a 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -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)); @@ -21,23 +25,48 @@ public static void main(String[] args) { // 1. Show the day of the week. HashMap 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 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 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); diff --git a/src/nyc/c4q/ac21/calendar/WorkDays.java b/src/nyc/c4q/ac21/calendar/WorkDays.java index 8c9ea0c..2d440df 100644 --- a/src/nyc/c4q/ac21/calendar/WorkDays.java +++ b/src/nyc/c4q/ac21/calendar/WorkDays.java @@ -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 getWorkDays() { + public static HashMap getWorkDays() + { // FIXME: Write this. - return null; // Change this! + //Make the HashMap + HashMap getWorkDays = new HashMap(); + 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; } + } + +