Ejemplo n.º 1
0
 public bool IsStartDateLower(SearchDate Dates)
 {
     if (Dates.startDate > Dates.endDate)
     {
         Console.WriteLine("The search Start Date can not be greater than the search End Date in your json file");
         return(false);
     }
     return(true);
 }
Ejemplo n.º 2
0
        public bool ReadRequest(string file)
        {
            JObject jObject      = JObject.Parse(file);
            JToken  jCampsite    = jObject["campsites"];
            JToken  jReservation = jObject["reservations"];
            JToken  jSearch      = jObject["search"];

            ValidRead = true;

            //Get List of CampSite Objects
            if (jCampsite != null && jCampsite.HasValues)
            {
                Campsites = jCampsite.ToObject <List <CampSite> >();
            }
            else
            {
                Console.WriteLine("The Campsite section of your JSON file is not formatted properly or there are no values.");
                ValidRead = false;
                return(false);
            }

            //Get List of Reservation Objects
            if (jReservation != null)
            {
                Reservations = jReservation.ToObject <List <Reservation> >();
            }
            else
            {
                Console.WriteLine("The Reservation section of your JSON file is not formatted properly.");
                ValidRead = false;
                return(false);
            }

            //Get Search Dates
            if (jSearch != null && jSearch.HasValues)
            {
                var search = new SearchDate();
                search.startDate = (DateTime)jSearch["startDate"];
                search.endDate   = (DateTime)jSearch["endDate"];

                if (search.endDate == null || search.startDate == null)
                {
                    Console.WriteLine("The Search section of your JSON file is not formatted properly or there are no values.");
                    ValidRead = false;
                }

                DateRange = search;
            }
            else
            {
                Console.WriteLine("The Search section of your JSON file is not formatted properly");
                ValidRead = false;
                return(false);
            }
            return(true);
        }
Ejemplo n.º 3
0
        //This Method will check for overlapping dates and also find the closest reservation that ends
        //before our search start and closest reservation that begins after our search end for each campsite.
        //It will call the gap rule method to check the day gap between reservations. Setting valid campsites to CampSiteList.
        public void Filter(int gap, List <CampSite> CampSites, List <Reservation> Reservations, SearchDate Dates)
        {
            List <string>   InvalidCampsites = new List <string>();
            List <CampSite> ValidCampsites   = new List <CampSite>();
            DateTime        ClosestToStart   = DateTime.MinValue;
            DateTime        ClosestToEnd     = DateTime.MaxValue;
            DateTime        searchStart      = Dates.startDate;
            DateTime        searchEnd        = Dates.endDate;
            var             stoppedAt        = 0;


            //If there are no other reservations then all campsites are valid
            if (Reservations.Count == 0)
            {
                CampSiteList = CampSites;
                return;
            }

            //Loop through all campsites and their reservations. This method assumes the campsite Ids and reservation campsiteIds
            //are grouped and sorted by id
            for (int i = 0; i < CampSites.Count; i++)
            {
                var  reservationStart = new DateTime();
                var  reservationEnd   = new DateTime();
                var  campsiteID       = CampSites[i].id;
                bool flag             = false;

                //Every new Campsite reset the closest end and start
                ClosestToStart = DateTime.MinValue;
                ClosestToEnd   = DateTime.MaxValue;

                for (int j = stoppedAt; j < Reservations.Count; j++)
                {
                    //pointer to reservation we stopped on
                    stoppedAt = j;

                    //if new campsite id has been hit break the loop
                    if (Reservations[j].campsiteId != campsiteID)
                    {
                        flag = true;
                        break;
                    }

                    reservationStart = Reservations[j].startDate;
                    reservationEnd   = Reservations[j].endDate;

                    //Checks for overlapping dates
                    if (searchStart <= reservationEnd && reservationStart <= searchEnd)
                    {
                        InvalidCampsites.Add(CampSites[i].name);
                    }

                    //Checks reservation to see if it ends closer to the search start
                    if (ClosestToStart < reservationEnd && reservationEnd < searchStart)
                    {
                        ClosestToStart = reservationEnd;
                    }
                    //Checks reservation to see if it begins closer to the search end
                    if (ClosestToEnd > reservationStart && reservationStart > searchEnd)
                    {
                        ClosestToEnd = reservationStart;
                    }
                }

                //Take the ClosestStart and ClosestEnd and check it against the search dates to see if the gap rule is violated
                var validGap = GapCheck(gap, searchStart, searchEnd, ClosestToStart, ClosestToEnd);

                if (!validGap)
                {
                    InvalidCampsites.Add(CampSites[i].name);
                }

                //prevents breaking out of both for loops before we iterate through all campsites
                if (flag)
                {
                    continue;
                }
            }

            //Add all not invalid campsites to the valid campsite list
            for (int i = 0; i < CampSites.Count; i++)
            {
                if (!InvalidCampsites.Contains(CampSites[i].name))
                {
                    ValidCampsites.Add(CampSites[i]);
                }
            }

            CampSiteList = ValidCampsites;
        }
Ejemplo n.º 4
0
        public bool Validate(List <CampSite> CampSites, List <Reservation> Reservations, SearchDate Dates)
        {
            HashSet <int> CampIds = new HashSet <int>();

            if (!IsStartDateLower(Dates))
            {
                return(false);
            }

            //populate hashset to compare against campsiteIds in the reservation list
            for (int i = 0; i < CampSites.Count; i++)
            {
                CampIds.Add(CampSites[i].id);
            }

            if (!IsReservationCampIdValid(CampIds, Reservations))
            {
                return(false);
            }

            return(true);
        }