From 8182ac435a16a8a77e506c8ebe65deb413144f24 Mon Sep 17 00:00:00 2001 From: RosmaryFC Date: Sun, 29 Mar 2015 17:19:26 -0400 Subject: [PATCH 1/8] Initial commit --- .../c4q/ac21/calendar/CalendarPrinter.java | 2 + src/nyc/c4q/ac21/calendar/DateTools.java | 40 +++++++++++++++++-- src/nyc/c4q/ac21/calendar/Main.java | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java index 74f7a5e..f818c9e 100644 --- a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java +++ b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java @@ -29,6 +29,8 @@ public static void printMonthCalendar(Calendar date) { // Use these methods to help you: // DateTools.getMonthNames() // DateTools.getNextDay() to loop through days in the month. + } + } 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/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index 8e1fa0d..d390ea5 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -21,6 +21,7 @@ public static void main(String[] args) { // 1. Show the day of the week. HashMap dayOfWeekNames = DateTools.getDayOfWeekNames(); + // ... // 2. Show whether this is a work day. From 609b1fb09e23a40b1de1577554a178c6b221c059 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Sun, 29 Mar 2015 19:58:40 -0400 Subject: [PATCH 2/8] me and rosmary finished FIXMEs --- src/nyc/c4q/ac21/calendar/DST.java | 68 ++++++++++++++++++++++++----- src/nyc/c4q/ac21/calendar/Main.java | 12 ++--- 2 files changed, 65 insertions(+), 15 deletions(-) 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/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index 8e1fa0d..ea2a294 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -19,7 +19,7 @@ public static void main(String[] args) { // FIXME: Write the rest of this method! - // 1. Show the day of the week. + /*// 1. Show the day of the week. HashMap dayOfWeekNames = DateTools.getDayOfWeekNames(); // ... @@ -29,18 +29,20 @@ public static void main(String[] args) { // 3. Show whether this is a national holiday, and if so, which. HashMap holidays = Holidays.getHolidays("National holiday"); - // ... + // ... */ // 4. Show whether this date is in DST. - boolean isDST = DST.isDST(date); + //boolean isDST = DST.isDST(date); + + System.out.println(DST.isDST(date)); // ... - // 5. Show the zodiac sign. + /* // 5. Show the zodiac sign. String zodiacSign = Zodiac.getZodiacSign(date); // ... // 6. Print out the monthly calendar. - CalendarPrinter.printMonthCalendar(date); + CalendarPrinter.printMonthCalendar(date); */ } } From 85106ff6d27147abdd6830bac366d5b5b622bafa Mon Sep 17 00:00:00 2001 From: tashsmit Date: Sun, 29 Mar 2015 20:03:19 -0400 Subject: [PATCH 3/8] DST class complete --- .../c4q/ac21/calendar/CalendarPrinter.java | 2 + src/nyc/c4q/ac21/calendar/DateTools.java | 40 +++++++++++++++++-- src/nyc/c4q/ac21/calendar/Main.java | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java index 74f7a5e..f818c9e 100644 --- a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java +++ b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java @@ -29,6 +29,8 @@ public static void printMonthCalendar(Calendar date) { // Use these methods to help you: // DateTools.getMonthNames() // DateTools.getNextDay() to loop through days in the month. + } + } 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/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index ea2a294..1af0218 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -21,6 +21,7 @@ public static void main(String[] args) { /*// 1. Show the day of the week. HashMap dayOfWeekNames = DateTools.getDayOfWeekNames(); + // ... // 2. Show whether this is a work day. From 324b2a0af4158b9abf608eed6d23d905e6f0c5f1 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Wed, 1 Apr 2015 22:24:08 -0400 Subject: [PATCH 4/8] charlyn- workDays --- src/nyc/c4q/ac21/calendar/WorkDays.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/nyc/c4q/ac21/calendar/WorkDays.java b/src/nyc/c4q/ac21/calendar/WorkDays.java index 8c9ea0c..adb1a7d 100644 --- a/src/nyc/c4q/ac21/calendar/WorkDays.java +++ b/src/nyc/c4q/ac21/calendar/WorkDays.java @@ -13,7 +13,19 @@ public class WorkDays { */ 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; } + } + + From 34320c68d0532c3849a613726e53570493376efe Mon Sep 17 00:00:00 2001 From: tashsmit Date: Thu, 2 Apr 2015 13:39:59 -0400 Subject: [PATCH 5/8] Update Holidays.java --- src/nyc/c4q/ac21/calendar/Holidays.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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; } From 0d2bfc2cea670bcc77866766919ca5047b4aa72c Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 3 Apr 2015 10:46:15 -0400 Subject: [PATCH 6/8] changes to workdays --- src/nyc/c4q/ac21/calendar/WorkDays.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/nyc/c4q/ac21/calendar/WorkDays.java b/src/nyc/c4q/ac21/calendar/WorkDays.java index adb1a7d..2d440df 100644 --- a/src/nyc/c4q/ac21/calendar/WorkDays.java +++ b/src/nyc/c4q/ac21/calendar/WorkDays.java @@ -4,14 +4,16 @@ 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. //Make the HashMap HashMap getWorkDays = new HashMap(); @@ -24,7 +26,7 @@ public static HashMap getWorkDays() { getWorkDays.put(Calendar.SATURDAY, false); return getWorkDays; } - + } From 421c3efc0a8a9879689c434f5d383adb31854178 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Fri, 3 Apr 2015 11:20:49 -0400 Subject: [PATCH 7/8] main updated - need print calendar --- src/nyc/c4q/ac21/calendar/Main.java | 54 +++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/src/nyc/c4q/ac21/calendar/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index 1af0218..ee837e9 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -4,46 +4,72 @@ 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)); // FIXME: Write the rest of this method! - /*// 1. Show the day of the week. + // 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); + boolean isDST = DST.isDST(date); + System.out.println("is DST: " + isDST); - System.out.println(DST.isDST(date)); - // ... - /* // 5. Show the zodiac sign. + // 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); */ + //CalendarPrinter.printMonthCalendar(date); } } From 96cdddc6420adabf1ee27cf92961f3986e965580 Mon Sep 17 00:00:00 2001 From: tashsmit Date: Mon, 6 Apr 2015 13:41:08 -0400 Subject: [PATCH 8/8] print calendar done --- .../c4q/ac21/calendar/CalendarPrinter.java | 54 ++++++++++++++++--- src/nyc/c4q/ac21/calendar/Main.java | 2 +- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/nyc/c4q/ac21/calendar/CalendarPrinter.java b/src/nyc/c4q/ac21/calendar/CalendarPrinter.java index f818c9e..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,12 +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/Main.java b/src/nyc/c4q/ac21/calendar/Main.java index ee837e9..593a62a 100644 --- a/src/nyc/c4q/ac21/calendar/Main.java +++ b/src/nyc/c4q/ac21/calendar/Main.java @@ -69,7 +69,7 @@ public static void main(String[] args) System.out.println("Zodiac sign: " + zodiacSign); // 6. Print out the monthly calendar. - //CalendarPrinter.printMonthCalendar(date); + CalendarPrinter.printMonthCalendar(date); } }