public List <string> GetTheaterSeats(List <string> inputs) { StringBuilder layout = new StringBuilder(); StringBuilder ticketRequests = new StringBuilder(); //Parse the Theater Layout and Ticket Requests GetLayoutAndTicketRequests(inputs, layout, ticketRequests); try { //Get Theater Layout TheaterLayout theaterLayout = _theaterSearch.GetTheaterLayout(layout.ToString()); //Get Ticket Requests List <TheaterRequest> requests = _theaterSearch.GetTicketRequests(ticketRequests.ToString()); //Process Ticket Requests var listTheaterRequest = _theaterSearch.ProcessTicketRequests(theaterLayout, requests); //Build Response return(_theaterSearch.GetSeatInformation(listTheaterRequest)); } catch (Exception e) { Console.WriteLine("Internal Error Occured : " + e.Message); Console.ReadLine(); } return(null); }
//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; }
public void ProcessTicketRequests_Test() { //Arrange var theaterLayout = new TheaterLayout() { UsableSeats = 10, TotalSeats = 10, Sections = new List <TheaterSection>() { new TheaterSection() { AvailableSeats = 5, RowNumber = 1, SectionNumber = 1 }, new TheaterSection() { AvailableSeats = 5, RowNumber = 2, SectionNumber = 2 } } }; var theaterRequest = new List <TheaterRequest>() { new TheaterRequest() { IsOk = false, PersonName = "Smith", RequestedSeats = 5, RowNumber = 1, SectionNumber = 1 } }; Mock <ITheaterSearchHelper> mockTheaterSearchHelper = new Mock <ITheaterSearchHelper>(); TheaterSeatingSearch theaterSeatingSearch = new TheaterSeatingSearch(mockTheaterSearchHelper.Object); //Act var result = theaterSeatingSearch.ProcessTicketRequests(theaterLayout, theaterRequest); //Assert Assert.AreEqual(result.Count, 1); }
//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); }
public void GetTheaterSeats_Test() { //Arrange string responseText = "Smith Sorry, we can't handle your party."; var inputs = new List <string>() { "5 5" }; var theaterSeats = new List <string>() { responseText }; var theaterLayout = new TheaterLayout() { UsableSeats = 10, TotalSeats = 10, Sections = new List <TheaterSection>() { new TheaterSection() { AvailableSeats = 5, RowNumber = 1, SectionNumber = 1 }, new TheaterSection() { AvailableSeats = 5, RowNumber = 2, SectionNumber = 2 } } }; var theaterRequest = new List <TheaterRequest>() { new TheaterRequest() { IsOk = false, PersonName = "Smith", RequestedSeats = 100, RowNumber = 1, SectionNumber = 1 } }; Mock <ITheaterSearch> mockITheaterSearch = new Mock <ITheaterSearch>(); //Act mockITheaterSearch.Setup(x => x.GetTheaterLayout(It.IsAny <string>())).Returns(theaterLayout); mockITheaterSearch.Setup(x => x.GetTicketRequests(It.IsAny <string>())).Returns(theaterRequest); mockITheaterSearch .Setup(x => x.ProcessTicketRequests(It.IsAny <TheaterLayout>(), It.IsAny <List <TheaterRequest> >())) .Returns(theaterRequest); mockITheaterSearch.Setup((x => x.GetSeatInformation(It.IsAny <List <TheaterRequest> >()))).Returns(theaterSeats); TheaterSeat theaterSeat = new TheaterSeat(mockITheaterSearch.Object); var result = theaterSeat.GetTheaterSeats(inputs); //Assert Assert.AreEqual(result.FirstOrDefault(), responseText); }
//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); }