Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create TDD style functional black box test for getSlotNumbers() in the code belo

ID: 3588391 • Letter: C

Question

Create TDD style functional black box test for getSlotNumbers() in the code below.

Black Box Test:

    Arrange

    Act

    Assert

using System;

using System.Collections.Generic;

namespace Schedule_App

{

    public class PresentationSchedule

    {

        private Dictionary<int, Schedule> sectionSchedule = new Dictionary<int, Schedule>();

        private List<Slot> makeSlots(int maxSlots)

        {

            List<Slot> unusedMeetingSlots = new List<Slot>();

            for (int i = 1; i <= maxSlots; i++)

            {

                Slot slot = new Slot();

                slot.slotNumber = i;

                slot.isUsed = false;

                unusedMeetingSlots.Add(slot);

            }

            return unusedMeetingSlots;

        }

        public PresentationSchedule()

        {

            // Make a mock schedule as seed data

            createNewSchedule(4

                              , true

                              , new List<DateTime>() { DateTime.Parse("10/1/2017").Date

                                                            , DateTime.Parse("11/1/2017").Date

                                                            , DateTime.Parse("12/1/2017").Date}

                              , 1);

        }

        public bool createNewSchedule(int defaultNumberOfSlots

                                     , bool isPublishable

                                     , List<DateTime> meetingDates

                                     , int sectionId)

        {

            Schedule schedule = new Schedule();

            schedule.defaultNumberOfSlots = defaultNumberOfSlots;

            schedule.isPublishable = isPublishable;

            Dictionary<DateTime, List<Slot>> slotsByDate = new Dictionary<DateTime, List<Slot>>();

            foreach (DateTime meetingDate in meetingDates)

            {

                slotsByDate.Add(meetingDate.Date, makeSlots(schedule.defaultNumberOfSlots));

            }

            schedule.slotsByDate = slotsByDate;

            sectionSchedule.Add(sectionId, schedule);

            return true;

        }

        public int getDefaultNumberOfSlots(int sectionId)

        {

            return sectionSchedule[sectionId].defaultNumberOfSlots;

        }

        public bool configurePresentationSchedule(int sectionId

                                                  , DateTime firstDate

                                                  , DateTime lastDate

                                                  , int numSlots

                                                  , List<DateTime> ExcludeDates

                                                  , bool isPublishable)

        {

            // Assume it will work unless a problem is encountered

            bool isSuccessful = true;

            if (sectionSchedule.ContainsKey(sectionId))

            {

                // There is already a schedule defined for this section

                isSuccessful = false;

                return isSuccessful;

            }

            if (lastDate.Date < firstDate.Date)

            {

                // illogical start and end dates

                isSuccessful = false;

                return isSuccessful;

            }

            const int MIN_NUMBER_OF_SLOTS = 1;

            const int MAX_NUMBER_OF_SLOTS = 5;

            if ((numSlots < MIN_NUMBER_OF_SLOTS) || (numSlots > MAX_NUMBER_OF_SLOTS))

            {

                // fails min or max presentation slots per class period

                isSuccessful = false;

                return isSuccessful;

            }

            if (firstDate.DayOfWeek != lastDate.DayOfWeek)

            {

                // classes must meet on the same day of the week

                isSuccessful = false;

                return isSuccessful;

           }

            // TBD: Need to check firstDate and LastDate against the academic year and semester

            // generate a list of dates based on first and last date

            List<DateTime> meetingDays = makeListOfMeetingDays(firstDate.Date, lastDate.Date);

            // if there are excluded dates then remove them

            if (ExcludeDates.Count > 0)

            {

                foreach (DateTime excludeDate in ExcludeDates)

                {

                    meetingDays.RemoveAll(meetingDay => meetingDay.Date == excludeDate.Date);

                    //slotsByDate.Remove(excludeDate);

                }

            }

            createNewSchedule(numSlots

                              , isPublishable

                              , meetingDays

                              , sectionId);

            return isSuccessful;

        }

        private List<DateTime> makeListOfMeetingDays(DateTime firstDate, DateTime lastDate)

        {

            List<DateTime> listOfDates = new List<DateTime>();

            listOfDates.Add(firstDate.Date);

            DateTime dateMarker = firstDate.Date;

            while (dateMarker.Date < lastDate.Date)

            {

                listOfDates.Add(dateMarker.AddDays(7));

                dateMarker = dateMarker.AddDays(7).Date;

            }

            return listOfDates;

        }

        public Dictionary<DateTime, List<Slot>> getAvailableSlots(int sectionId)

        {

            Dictionary<DateTime, List<Slot>> checkSchedule;

            if (sectionSchedule.ContainsKey(sectionId))

            {

                checkSchedule = sectionSchedule[sectionId].slotsByDate;

                // remove used slots

                foreach (List<Slot> slots in checkSchedule.Values)

                {

                    slots.RemoveAll(item => item.isUsed);

                }

            }

            else

            {

                // return an empty list because the sectionId does not exist

                checkSchedule = new Dictionary<DateTime, List<Slot>>();

            }

            return checkSchedule;

        }

        public bool selectSlot(int sectionId, int studentId, DateTime slotDate, int slotNum)

        {

            Dictionary<DateTime, List<Slot>> slotsByDate = sectionSchedule[sectionId].slotsByDate;

            List<Slot> slots = slotsByDate[slotDate.Date];

            bool success = false;

            foreach (Slot slot in slots)

            {

                if (slot.slotNumber == slotNum && !slot.isUsed)

                {

                    success = true;

                    slot.isUsed = true;

                    slot.studentId = studentId;

                }

            }

            return success;

        }

        public Schedule getScheduleBySection(int sectionId)

        {

            if (sectionSchedule.ContainsKey(sectionId))

            {

                return sectionSchedule[sectionId];

            }

            else

            {

                // no schedule exists for specified schedule

                // so return an empty schedule

                return new Schedule();

            }

        }

        public int getSlotNumbers(int sectionId, DateTime meetingDate)

        {

            int numSlots = 0;

            if (sectionSchedule.ContainsKey(sectionId))

            {

                if (sectionSchedule[sectionId].slotsByDate.ContainsKey(meetingDate.Date))

                    numSlots = sectionSchedule[sectionId].slotsByDate[meetingDate.Date].Count;

            }

            return numSlots;

        }

        public bool modifySlotNumbers(int sectionId, DateTime meetingDate, int numSlots)

        {

            List<Slot> slots;

            bool isSuccessful = false;

            if (numSlots > 0)

            {

                if (sectionSchedule.ContainsKey(sectionId))

                {

                    if (sectionSchedule[sectionId].slotsByDate.ContainsKey(meetingDate.Date))

                    {

                        slots = makeSlots(numSlots);

                        // remove the old key and value from the dictionary

                        sectionSchedule[sectionId].slotsByDate.Remove(meetingDate.Date);

                        // add the new key and value from the dictionary

                        sectionSchedule[sectionId].slotsByDate.Add(meetingDate.Date, slots);

                        // update the return value

                        isSuccessful = true;

                    }

                }

            }

            return isSuccessful;

        }

    }

}

Explanation / Answer

using System;

using System.Collections.Generic;

namespace Schedule_App

{

    public class PresentationSchedule

    {

        private Dictionary<int, Schedule> sectionSchedule = new Dictionary<int, Schedule>();

        private List<Slot> makeSlots(int maxSlots)

        {

            List<Slot> unusedMeetingSlots = new List<Slot>();

            for (int i = 1; i <= maxSlots; i++)

            {

                Slot slot = new Slot();

                slot.slotNumber = i;

                slot.isUsed = false;

                unusedMeetingSlots.Add(slot);

            }

            return unusedMeetingSlots;

        }

        public PresentationSchedule()

        {

            // Make a mock schedule as seed data

            createNewSchedule(4

                              , true

                              , new List<DateTime>() { DateTime.Parse("10/1/2017").Date

                                                            , DateTime.Parse("11/1/2017").Date

                                                            , DateTime.Parse("12/1/2017").Date}

                              , 1);

        }

        public bool createNewSchedule(int defaultNumberOfSlots

                                     , bool isPublishable

                                     , List<DateTime> meetingDates

                                     , int sectionId)

        {

            Schedule schedule = new Schedule();

            schedule.defaultNumberOfSlots = defaultNumberOfSlots;

            schedule.isPublishable = isPublishable;

            Dictionary<DateTime, List<Slot>> slotsByDate = new Dictionary<DateTime, List<Slot>>();

            foreach (DateTime meetingDate in meetingDates)

            {

                slotsByDate.Add(meetingDate.Date, makeSlots(schedule.defaultNumberOfSlots));

            }

            schedule.slotsByDate = slotsByDate;

            sectionSchedule.Add(sectionId, schedule);

            return true;

        }

        public int getDefaultNumberOfSlots(int sectionId)

        {

            return sectionSchedule[sectionId].defaultNumberOfSlots;

        }

        public bool configurePresentationSchedule(int sectionId

                                                  , DateTime firstDate

                                                  , DateTime lastDate

                                                  , int numSlots

                                                  , List<DateTime> ExcludeDates

                                                  , bool isPublishable)

        {

            // Assume it will work unless a problem is encountered

            bool isSuccessful = true;

            if (sectionSchedule.ContainsKey(sectionId))

            {

                // There is already a schedule defined for this section

                isSuccessful = false;

                return isSuccessful;

            }

            if (lastDate.Date < firstDate.Date)

            {

                // illogical start and end dates

                isSuccessful = false;

                return isSuccessful;

            }