> Documentation of FRQ1

- toc: true 
- badges: true
- comments: true
- categories: [jupyter]
- image: images/chart-preview.png
package com.nighthawk.spring_portfolio.mvc.calendar;

import java.util.*;

// Prototype Implementation

public class APCalendar {

    /**
     * Returns true if year is a leap year and false otherwise.
     * isLeapYear(2019) returns False
     * isLeapYear(2016) returns True
     */
    public static boolean isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    return true;
                } else {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns the value representing the day of the week
     * 0 denotes Sunday,
     * 1 denotes Monday, ...,
     * 6 denotes Saturday.
     * 
     * firstDayOfYear(2019) returns 2 for Tuesday.
     */

    public static int firstDayOfYear(int year) {
        Date currentDate = new Date(year - 1900, 0, 1);

        return currentDate.getDay();
    }

    /**
     * Returns n, where month, day, and year specify the nth day of the year.
     * This method accounts for whether year is a leap year.
     * dayOfYear(1, 1, 2019) return 1
     * dayOfYear(3, 1, 2017) returns 60, since 2017 is not a leap year
     * dayOfYear(3, 1, 2016) returns 61, since 2016 is a leap year.
     */
    public static int dayOfYear(int month, int day, int year) {
        int num = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(1, 0);
        map.put(2, 31);
        map.put(3, 59);
        map.put(4, 90);
        map.put(5, 120);
        map.put(6, 151);
        map.put(7, 181);
        map.put(8, 212);
        map.put(9, 243);
        map.put(10, 273);
        map.put(11, 304);
        map.put(12, 334);
        if (isLeapYear(year) && month > 2) {
            num = map.get(month) + day + 1;

        } else {
            num = map.get(month) + day;
        }

        return num;
    }

    /**
     * Returns the number of leap years between year1 and year2, inclusive.
     * Precondition: 0 <= year1 <= year2
     */
    public static int numberOfLeapYears(int year1, int year2) {
        int count = 0;
        for (int i = year1; i < year2 + 1; i++) {
            if (isLeapYear(i)) {
                count += 1;
            }
        }
        return count;
    }

    /**
     * Returns the value representing the day of the week for the given date
     * Precondition: The date represented by month, day, year is a valid date.
     */
    public static int dayOfWeek(int month, int day, int year) {
        int start = firstDayOfYear(year);
        int finalDay = start + dayOfYear(month, day, year);
        int remainder1 = finalDay % 7;
        return remainder1-1;
    }

    /** Tester method */
    public static void main(String[] args) {
        // Private access modifiers
        // System.out.println("firstDayOfYear: " + APCalendar.firstDayOfYear(2024));
        // System.out.println("dayOfYear: " + APCalendar.dayOfYear(1, 1, 2022));

        // // Public access modifiers
        // System.out.println("isLeapYear: " + APCalendar.isLeapYear(2022));
        // System.out.println("numberOfLeapYears: " + APCalendar.numberOfLeapYears(2000,
        // 2022));
         System.out.println("dayOfWeek: " + APCalendar.dayOfWeek(9, 21, 1999));
    }

}

Getters and Setters/JSON

package com.nighthawk.spring_portfolio.mvc.calendar;

/** Simple POJO
 * Used to Interface with APCalendar
 * The toString method(s) prepares object for JSON serialization
 * Note... this is NOT an entity, just an abstraction
 */
class Year {
   private int year;
   private boolean isLeapYear;

   // zero argument constructor
   public Year() {}

   /* year getter/setters */
   public int getYear() {
      return year;
   }
   public void setYear(int year) {
      this.year = year;
      this.setIsLeapYear(year);
   }

   /* isLeapYear getter/setters */
   public boolean getIsLeapYear(int year) {
      return APCalendar.isLeapYear(year);
   }
   private void setIsLeapYear(int year) {  // this is private to avoid tampering
      this.isLeapYear = APCalendar.isLeapYear(year);
   }

   /* isLeapYearToString formatted to be mapped to JSON */
   public String isLeapYearToString(){
      return ( "{ \"year\": "  +this.year+  ", " + "\"isLeapYear\": "  +this.isLeapYear+ " }" );
   }

   /* standard toString placeholder until class is extended */
   public String toString() {
      return isLeapYearToString();
   }
}

Front End

package com.nighthawk.spring_portfolio.mvc.calendar;

public class Day {
    private int year;
    private int month;
    private int day;
    private int dayOfYear;
    private int dayOfWeek;
    private int firstDayOfYear;

    // zero argument constructor
    public Day(int month, int year, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.setDayOfWeek();
        this.setDayOfYear();
        this.setFirstDayOfYear();
    }

    // public void setDate(int month, int year, int day) {
    //     this.year = year;
    //     this.month = month;
    //     this.day = day;
    //     this.setDayOfWeek();
    //     this.setDayOfYear();
    //     this.setFirstDayOfYear();
    // }

    /* dayOfYear getter/setters */
    public int getDayOfYear() {
        return APCalendar.dayOfYear(month, day, year);
    }

    private void setDayOfYear() { // this is private to avoid tampering
        this.dayOfYear = APCalendar.dayOfYear(month, day, year);
    }

    public int getFirstDayOfYear() {
        return APCalendar.firstDayOfYear(year);
    }

    private void setFirstDayOfYear() { // this is private to avoid tampering
        this.firstDayOfYear = APCalendar.firstDayOfYear(year);
    }

    /* dayOfWeek getter/setters */
    public int getDayOfWeek() {
        return APCalendar.dayOfWeek(month, day, year);
    }

    private void setDayOfWeek() { // this is private to avoid tampering
        this.dayOfWeek = APCalendar.dayOfWeek(month, day, year);
    }

    /* isLeapYearToString formatted to be mapped to JSON */
    public String jSONFormat() {
        return String.format(
                "{ \"year\": %d, \"month\": %d, \"day\": %d, \"dayOfWeek\": %d, \"dayOfYear\": %d, \"firstDayOfYear\": %d}",
                this.year,
                this.month, this.day, this.dayOfWeek, this.dayOfYear, this.firstDayOfYear);
    }

    /* standard toString placeholder until class is extended */
    public String toString() {
        return jSONFormat();
    }

    public static void main(String[] args) {
        Day dayobj = new Day(3, 2016, 1);
        // dayobj.setDate(3, 2016, 1);

        System.out.println(dayobj.jSONFormat());
    }
}

Controller

package com.nighthawk.spring_portfolio.mvc.calendar;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Random;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Calendar API
 * Calendar Endpoint: /api/calendar/isLeapYear/2022, Returns:
 * {"year":2020,"isLeapYear":false}
 */
@RestController
@RequestMapping("/api/calendar")
public class CalendarApiController {

  /**
   * GET isLeapYear endpoint
   * ObjectMapper throws exceptions on bad JSON
   *
   * @throws JsonProcessingException
   * @throws JsonMappingException
   */
  @GetMapping("/isLeapYear/{year}")
  public ResponseEntity<JsonNode> getIsLeapYear(@PathVariable int year)
      throws JsonMappingException, JsonProcessingException {
    // Backend Year Object
    Year year_obj = new Year();
    year_obj.setYear(year); // evaluates Leap Year

    // Turn Year Object into JSON
    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree(year_obj.isLeapYearToString()); // this requires exception handling

    return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
  }

  @GetMapping("/numberOfLeapYears/{year1}/{year2}")
  public ResponseEntity<JsonNode> numberOfLeapYears(@PathVariable int year1, @PathVariable int year2)
      throws JsonMappingException, JsonProcessingException {

    // Turn Year Object into JSON
    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree("{ \"count\": " + APCalendar.numberOfLeapYears(year1, year2) + " } "); // handling

    return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
  }

  @GetMapping("/date/{year}/{month}/{day}")
  public ResponseEntity<JsonNode> dayInfo(@PathVariable int year, @PathVariable int month, @PathVariable int day)
      throws JsonMappingException, JsonProcessingException {

    // Turn Year Object into JSON
    Day dayobj = new Day(month, year, day);
    // dayobj.setStuff(month, year, day);

    ObjectMapper mapper = new ObjectMapper();
    JsonNode json = mapper.readTree(dayobj.jSONFormat()); // handling

    return ResponseEntity.ok(json); // JSON response, see ExceptionHandlerAdvice for throws
  }



}