/// <summary>
            /// Construct the mapper for camper requests in the CSV file
            /// </summary>
            /// <param name="activityDefinitions">List of activity definitions</param>
            public CamperRequestsMap(List <ActivityDefinition> activityDefinitions)
            {
                _activityDefinitionByName = activityDefinitions.ToDictionary(
                    ad => ad.Name, ad => ad);

                int index       = 0;
                int camperIndex = index;

                Map(m => m.Camper).ConvertUsing(row => {
                    _lastReadCamper = new Camper
                    {
                        LastName  = row.GetField(camperIndex),
                        FirstName = row.GetField(camperIndex + 1)
                    };
                    return(_lastReadCamper);
                });
                index += 2;
                int mateIndex = index;

                Map(m => m.CabinMate).ConvertUsing(row =>
                {
                    string cabinMateLastName = row.GetField(mateIndex);
                    _lastCabinMate           = String.IsNullOrEmpty(cabinMateLastName)
                        ? null
                        : new Camper {
                        LastName = cabinMateLastName
                    };
                    return(_lastCabinMate);
                });
                index++;
                int activityIndex = index;

                Map(m => m.ActivityRequests).ConvertUsing(row => {
                    return(new List <ActivityRequest> {
                        new ActivityRequest {
                            Rank = 1, Activity = GetActivityForName(row.GetField(activityIndex))
                        },
                        new ActivityRequest {
                            Rank = 2, Activity = GetActivityForName(row.GetField(activityIndex + 1))
                        },
                        new ActivityRequest {
                            Rank = 3, Activity = GetActivityForName(row.GetField(activityIndex + 2))
                        },
                        new ActivityRequest {
                            Rank = 4, Activity = GetActivityForName(row.GetField(activityIndex + 3))
                        }
                    });
                });
                index += 4;
                int alternateIndex = index;

                Map(m => m.AlternateActivity).ConvertUsing(row =>
                {
                    return(GetActivityForName(row.GetField(alternateIndex)));
                });
            }
Beispiel #2
0
        /// <summary>
        /// Try to add a camper to an activity block. Checks the max capacity for the defn
        /// </summary>
        /// <param name="camper">Camper to add</param>
        /// <returns>true if the camper could be added</returns>
        public bool TryAddCamper(Camper camper)
        {
            bool didAdd = false;

            if (_assignedCampers.Count < ActivityDefinition.MaximumCapacity)
            {
                _assignedCampers.Add(camper);
                didAdd = true;
            }
            return(didAdd);
        }
        /// <summary>
        /// Try to assign a camper to existing blocks of the activity.
        /// </summary>
        /// <param name="camper">Camper to assign</param>
        /// <param name="limitByOptimal">Do not exceed optimal capacity</param>
        /// <returns>true if the camper was assigned</returns>
        public bool TryAssignCamperToExistingActivityBlock(Camper camper, Boolean limitByOptimal)
        {
            bool didAssign = false;

            int capacity      = (limitByOptimal) ? OptimalCapacity : MaximumCapacity;
            var firstFitBlock = _scheduledBlocks.Find(
                b => camper.IsAvailableInTimeSlot(b.TimeSlot) &&
                b.AssignedCampers.Count < capacity);

            if (firstFitBlock != null)
            {
                // If this assign fails something is wrong because it was checked in the find.
                didAssign = camper.TryAssignBlock(firstFitBlock);
            }

            return(didAssign);
        }
        /// <summary>
        /// Try to assign the camper to a new activity block for the activity.
        /// </summary>
        /// <param name="camper">Camper to assign</param>
        /// <param name="activityDefinition">Activity to assign to</param>
        /// <returns>true if the camper was assigned</returns>
        public bool TryAssignCamperToNewActivityBlock(Camper camper)
        {
            // Go through the campers unused slots and try to create to create
            // an activity block.
            Boolean didAssignCamper = false;

            for (int slotNumber = 0; slotNumber < ActivityBlock.MaximumTimeSlots && !didAssignCamper; slotNumber++)
            {
                if (camper.IsAvailableInTimeSlot(slotNumber))
                {
                    var newActivityBlock = TryCreateBlock(slotNumber);
                    didAssignCamper = camper.TryAssignBlock(newActivityBlock);
                }
            }

            return(didAssignCamper);
        }
        public static List <ActivityDefinition> ReadScheduleFromCsvString(String csvSchedule,
                                                                          ILogger logger)
        {
            try
            {
                List <ActivityDefinition> activityDefinitions = new List <ActivityDefinition>();
                using (var streamReader = new StringReader(csvSchedule))
                {
                    var csvReader = new CsvReader(streamReader, new CsvConfiguration(CultureInfo.InvariantCulture)
                    {
                        HasHeaderRecord = true,
                        HeaderValidated = null
                    });

                    // Skip over the header line
                    csvReader.Read();

                    ActivityDefinition          currentActivityDefinition = null;
                    Dictionary <string, Camper> campersByName             = new Dictionary <string, Camper>();

                    while (csvReader.Read())
                    {
                        string activityName = csvReader.GetField(0);
                        if (!string.IsNullOrWhiteSpace(activityName))
                        {
                            currentActivityDefinition = new ActivityDefinition {
                                Name = activityName
                            };
                            activityDefinitions.Add(currentActivityDefinition);
                        }
                        if (currentActivityDefinition == null)
                        {
                            throw new Exception("Malformed file: no activity at start");
                        }
                        int timeSlot = csvReader.GetField <int>(1);
                        currentActivityDefinition._scheduledBlocks.Add(new ActivityBlock
                        {
                            ActivityDefinition = currentActivityDefinition,
                            TimeSlot           = timeSlot
                        });
                        // Next is the collection of camper names.
                        int    camperIndex = 0;
                        object camperFullNameObject;
                        while (csvReader.TryGetField(typeof(string), 2 + camperIndex, out camperFullNameObject))
                        {
                            // Find or create the camper
                            string camperFullName = (string)camperFullNameObject;
                            Camper camper;
                            if (!campersByName.TryGetValue(camperFullName, out camper))
                            {
                                string[] camperNameParts = camperFullName.Split(',');
                                camper = new Camper
                                {
                                    FirstName = camperNameParts[1].Trim('"').Trim(),
                                    LastName  = camperNameParts[0].Trim('"').Trim()
                                };
                                campersByName.Add(camperFullName, camper);
                            }
                            // Add camper to activity
                            currentActivityDefinition.ScheduledBlocks[timeSlot].AssignedCampers.Add(camper);

                            // Add activity to camper
                            camper.ScheduledBlocks.Add(currentActivityDefinition.ScheduledBlocks[timeSlot]);

                            camperIndex++;
                        }
                    }
                }
                return(activityDefinitions);
            }
            catch (CsvHelperException e)
            {
                KeyNotFoundException keyNotFoundException = e.InnerException as KeyNotFoundException;
                if (keyNotFoundException != null)
                {
                    logger.LogError($"Error parsing input text {csvSchedule}: {keyNotFoundException.Message}");
                }
                else
                {
                    logger.LogError($"Exception parsing input text {csvSchedule}: {e.Message}");
                }
            }
            catch (Exception e)
            {
                logger.LogError($"Exception parsing input text {csvSchedule}: {e.Message}");
            }
            return(null);
        }
Beispiel #6
0
 /// <summary>
 /// Remove the camper from the activity block
 /// </summary>
 /// <param name="camper">Camper to remove</param>
 public void RemoveCamper(Camper camper)
 {
     _assignedCampers.Remove(camper);
 }
Beispiel #7
0
 /// <summary>
 /// Add the camper without concern for limits
 /// </summary>
 /// <param name="camper">Camper to add</param>
 public void AddCamper(Camper camper)
 {
     _assignedCampers.Add(camper);
 }