Exemplo n.º 1
0
        //Find the number of available seats in the section list
        public int FindSectionByAvailableSeats(List <TheaterSection> sections, int availableSeats)
        {
            int            i       = 0;
            TheaterSection section = new TheaterSection();

            section.AvailableSeats = availableSeats;

            sections.Sort(new CompareAvailableSeats());
            CompareAvailableSeats compareAvailable = new CompareAvailableSeats();
            int sectionNo = sections.BinarySearch(section, compareAvailable);

            if (sectionNo > 0)
            {
                for (i = sectionNo - 1; i >= 0; i--)
                {
                    TheaterSection s = sections[i];

                    if (s.AvailableSeats != availableSeats)
                    {
                        break;
                    }
                }

                sectionNo = i + 1;
            }

            return(sectionNo);
        }
        //Method to Find the section based on availability of seats
        private int findSectionByAvailableSeats(List <TheaterSection> sections, int availableSeats)
        {
            int i = 0;

            sections.Sort();
            TheaterSection SearchKey = new TheaterSection();

            SearchKey.AvailableSeats = availableSeats;
            int Scount = sections.BinarySearch(SearchKey, new SeatCompare());

            if (Scount > 0)
            {
                for (i = Scount - 1; i >= 0; i--)
                {
                    TheaterSection s = sections[i];

                    if (s.AvailableSeats != availableSeats)
                    {
                        break;
                    }
                    Scount = i + 1;
                }
            }


            return(Scount);
        }
Exemplo n.º 3
0
 //To map theater details to the request  object
 public void MapTheaterDetails(TheaterLayout layout, TheaterRequest request, TheaterSection section)
 {
     request.RowNumber      = section.RowNumber;
     request.SectionNumber  = section.SectionNumber;
     section.AvailableSeats = section.AvailableSeats - request.RequestedSeats;
     layout.UsableSeats     = layout.UsableSeats - request.RequestedSeats;
     request.IsOk           = true;
 }
        //Process the layout string and map the theater section
        public TheaterLayout GetTheaterLayout(string rawLayout)
        {
            TheaterLayout         theaterLayout = new TheaterLayout();
            List <TheaterSection> sectionsList  = new List <TheaterSection>();
            int totalCapacity = 0;
            var rows          = rawLayout.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            for (int i = 0; i < rows.Length - 1; i++)
            {
                var sections = rows[i].Split(null);

                for (int j = 0; j < sections.Length; j++)
                {
                    int value;

                    try
                    {
                        value = int.Parse(sections[j]);
                    }
                    catch (Exception)
                    {
                        throw new Exception(
                                  "'" + sections[j] + "'" + " is invalid section capacity. Please correct it.");
                    }

                    totalCapacity = totalCapacity + value;

                    var section = new TheaterSection
                    {
                        RowNumber      = i + 1,
                        SectionNumber  = j + 1,
                        AvailableSeats = value
                    };

                    sectionsList.Add(section);
                }
            }

            theaterLayout.TotalSeats  = totalCapacity;
            theaterLayout.UsableSeats = totalCapacity;
            theaterLayout.Sections    = sectionsList;

            return(theaterLayout);
        }
        //Method to Create Layout structure Object from raw input data
        public Structure getTheaterLayout(string rawLayout)
        {
            Structure             theaterLayout = new Structure();
            TheaterSection        section;
            List <TheaterSection> sectionsList = new List <TheaterSection>();
            int totalCapacity = 0, value;

            string[] rows = rawLayout.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            string[] sections;

            for (int Rnum = 0; Rnum < rows.Length; Rnum++)
            {
                sections = rows[Rnum].Split(' ');
                for (int SecNum = 0; SecNum < sections.Length; SecNum++)
                {
                    try
                    {
                        value = Convert.ToInt32(sections[SecNum]);
                    }
                    catch (System.FormatException)
                    {
                        throw new System.FormatException("'" + sections[SecNum] + "'" + " is invalid section capacity. Please correct it.");
                    }
                    totalCapacity          = totalCapacity + value;
                    section                = new TheaterSection();
                    section.RowNumber      = Rnum + 1;
                    section.SectionNumber  = SecNum + 1;
                    section.Seats          = value;
                    section.AvailableSeats = value;
                    sectionsList.Add(section);
                }
            }
            theaterLayout.TotalSeats     = totalCapacity;
            theaterLayout.AvailableSeats = totalCapacity;
            theaterLayout.SectionList    = sectionsList;
            return(theaterLayout);
        }
        //Process the Ticket Requests and Layout
        public List <TheaterRequest> ProcessTicketRequests(TheaterLayout layout, List <TheaterRequest> requests)
        {
            for (int i = 0; i < requests.Count; i++)
            {
                TheaterRequest request = requests[i];
                if (request.IsOk)
                {
                    continue;
                }

                if (request.RequestedSeats > layout.UsableSeats)
                {
                    request.RowNumber     = -2;
                    request.SectionNumber = -2;
                    continue;
                }

                var sections = layout.Sections;

                foreach (var section in sections)
                {
                    if (request.RequestedSeats == section.AvailableSeats)
                    {
                        _theaterSearchHelper.MapTheaterDetails(layout, request, section);
                        break;
                    }

                    if (request.RequestedSeats < section.AvailableSeats)
                    {
                        int requestNo = _theaterSearchHelper.FindComplementRequest(requests,
                                                                                   section.AvailableSeats - request.RequestedSeats, i);

                        if (requestNo == -1)
                        {
                            int sectionNo = _theaterSearchHelper.FindSectionByAvailableSeats(sections, request.RequestedSeats);

                            if (sectionNo >= 0)
                            {
                                TheaterSection perfectSection = sections[sectionNo];

                                _theaterSearchHelper.MapTheaterDetails(layout, request, perfectSection);
                                break;
                            }

                            _theaterSearchHelper.MapTheaterDetails(layout, request, section);
                            break;
                        }

                        _theaterSearchHelper.MapTheaterDetails(layout, request, section);

                        TheaterRequest complementRequest = requests[requestNo];

                        _theaterSearchHelper.MapTheaterDetails(layout, complementRequest, section);

                        break;
                    }
                }

                if (!request.IsOk)
                {
                    request.RowNumber     = -1;
                    request.SectionNumber = -1;
                }
            }

            return(requests);
        }
        public virtual void processTicketRequests(Structure layout, List <Requests> requests)
        {
            for (int i = 0; i < requests.Count; i++)
            {
                Requests request = requests[i];
                if (request.VerifyComplete)
                {
                    continue;
                }

                /*
                 * -2 is an indicator when we can't handle the party.
                 */
                if (request.NoOfTickets > layout.AvailableSeats)
                {
                    request.RowNumber     = -2;
                    request.SectionNumber = -2;
                    continue;
                }

                List <TheaterSection> sections = layout.SectionList;

                for (int j = 0; j < sections.Count; j++)
                {
                    TheaterSection section = sections[j];

                    if (request.NoOfTickets == section.AvailableSeats)
                    {
                        request.SectionNumber  = section.SectionNumber;
                        request.RowNumber      = section.RowNumber;
                        layout.AvailableSeats  = layout.AvailableSeats - request.NoOfTickets;
                        section.AvailableSeats = section.AvailableSeats - request.NoOfTickets;
                        request.VerifyComplete = true;
                        break;
                    }
                    else if (request.NoOfTickets < section.AvailableSeats)
                    {
                        int requestNo = findCompleteRequest(requests, section.AvailableSeats - request.NoOfTickets, i);

                        if (requestNo != -1)
                        {
                            request.SectionNumber  = section.SectionNumber;
                            request.RowNumber      = section.RowNumber;
                            layout.AvailableSeats  = layout.AvailableSeats - request.NoOfTickets;
                            section.AvailableSeats = section.AvailableSeats - request.NoOfTickets;
                            request.VerifyComplete = true;

                            Requests completeRequest = requests[requestNo];

                            completeRequest.SectionNumber  = section.SectionNumber;
                            completeRequest.RowNumber      = section.RowNumber;
                            layout.AvailableSeats          = layout.AvailableSeats - completeRequest.NoOfTickets;
                            section.AvailableSeats         = section.AvailableSeats - completeRequest.NoOfTickets;
                            completeRequest.VerifyComplete = true;

                            break;
                        }
                        else
                        {
                            int sectionNo = findSectionByAvailableSeats(sections, request.NoOfTickets);

                            if (sectionNo >= 0)
                            {
                                TheaterSection perferctSection = sections[sectionNo];

                                request.RowNumber              = perferctSection.RowNumber;
                                request.SectionNumber          = perferctSection.SectionNumber;
                                perferctSection.AvailableSeats = perferctSection.AvailableSeats - request.NoOfTickets;
                                layout.AvailableSeats          = layout.AvailableSeats - request.NoOfTickets;
                                request.VerifyComplete         = true;
                                break;
                            }
                            else
                            {
                                request.RowNumber      = section.RowNumber;
                                request.SectionNumber  = section.SectionNumber;
                                section.AvailableSeats = section.AvailableSeats - request.NoOfTickets;
                                layout.AvailableSeats  = layout.AvailableSeats - request.NoOfTickets;
                                request.VerifyComplete = true;
                                break;
                            }
                        }
                    }
                }

                /*
                 * -1 is an indicator when we need to split the party.
                 */
                if (!request.VerifyComplete)
                {
                    request.RowNumber     = -1;
                    request.SectionNumber = -1;
                }
            }

            Console.WriteLine("Final Distribution of Seats by Name , Seat .\n");

            foreach (Requests request in requests)
            {
                Console.WriteLine(request.Status);
            }
        }