Beispiel #1
0
        public RequestAvaibility Check(PreReservationViewModel preReservation)
        {
            var result     = new RequestAvaibility();
            var freeTables = GetAvailable(preReservation);

            AddTables(ref freeTables, preReservation.HowManyPeoples, ref result);
            return(result);
        }
Beispiel #2
0
 public static void AddTables(ref List <Table> freeTables, int ammountOfPeople, ref RequestAvaibility result)
 {
     if (ammountOfPeople > 0)
     {
         var exactAmount = freeTables.FirstOrDefault(ft => ft.Capacity == ammountOfPeople);
         if (exactAmount != null)
         {
             result.IsAvailable = true;
             result.TableList.Add(exactAmount);
             ammountOfPeople = 0;
             AddTables(ref freeTables, ammountOfPeople, ref result);
         }
         else
         {
             var totalCapacity = freeTables.Sum(ft => ft.Capacity);
             if (totalCapacity < ammountOfPeople)
             {
                 result.IsAvailable = false;
                 ammountOfPeople    = 0;
                 AddTables(ref freeTables, ammountOfPeople, ref result);
             }
             else
             {
                 var biggerTable = freeTables.Where(ft => ft.Capacity > ammountOfPeople)
                                   .OrderBy(ft => ft.Capacity).FirstOrDefault();
                 if (biggerTable != null)
                 {
                     result.IsAvailable = true;
                     result.TableList.Add(biggerTable);
                     freeTables.Remove(biggerTable);
                     ammountOfPeople = ammountOfPeople - biggerTable.Capacity;
                     AddTables(ref freeTables, ammountOfPeople, ref result);
                 }
                 else
                 {
                     result.IsAvailable = true;
                     var table = freeTables.OrderByDescending(ft => ft.Capacity).FirstOrDefault();
                     result.TableList.Add(table);
                     freeTables.Remove(table);
                     ammountOfPeople = ammountOfPeople - table.Capacity;
                     AddTables(ref freeTables, ammountOfPeople, ref result);
                 }
             }
         }
     }
 }