コード例 #1
0
 public Dish(int id, string name, Enums.DishType type, bool multTimes = false)
 {
     this.Id                  = id;
     this.Name                = name;
     this.DishType            = type;
     this.IsAbleMultipleTimes = multTimes;
 }
コード例 #2
0
        public string MakeOrder(string order)
        {
            // base case no input or no csv
            if (order.Contains(",") == false)
            {
                return("error");
            }

            // Read in time of day
            string[]        orderPieces = order.Split(',');
            Enums.TimeOfDay timeOfDay;
            try
            {
                timeOfDay = (Enums.TimeOfDay)Enum.Parse(typeof(Enums.TimeOfDay), orderPieces[0], true);    // 3rd arg takes care of capitalization
            }
            catch (Exception)
            {
                // Invalid time of day. exception overhead for this simple case is not ideal,
                // but more flexible to future TimeOfDay values than hardcoded switches
                return("error");
            }

            // Base case no items
            if (orderPieces.Length < 2)
            {
                return("error");
            }

            // Main loop. skip 1st element (time). Validate as we go to retain to the point of error.
            // Save items to hash w/count & postpone building string until completed or error.
            var  orderHash    = new Dictionary <KeyValuePair <Enums.TimeOfDay, Enums.DishType>, int>();
            bool errorReached = false;

            for (int i = 1; i < orderPieces.Length; i++)
            {
                // Base Case if item not an int or not a dishtype, error out
                int intItem = new int();
                if (int.TryParse(orderPieces[i], out intItem) == false ||
                    Enum.IsDefined(typeof(Enums.DishType), intItem) == false)
                {
                    errorReached = true;
                    break;
                }
                Enums.DishType dishType = (Enums.DishType)intItem;

                // If this combo exists in menu
                KeyValuePair <Enums.TimeOfDay, Enums.DishType> dish = new KeyValuePair <Enums.TimeOfDay, Enums.DishType>(timeOfDay, dishType);
                if (dishesAvailable.ContainsKey(dish) == true)
                {
                    if (orderHash.ContainsKey(dish) == false)
                    {
                        orderHash.Add(dish, 1);
                    }
                    else if (multipleDishesAllowed.Contains(dish) == true)
                    {
                        // Already ordered, but OK to duplicate
                        orderHash[dish]++;
                    }
                    else
                    {
                        // Already ordered, Not OK to duplicate
                        errorReached = true;
                        break;
                    }
                }
                else
                {
                    //time/dish combo not available to order.
                    errorReached = true;
                    break;
                }
            }

            // Inputs collected. translate into english descriptions in correct order.
            return(ConvertOrderHashToString(orderHash, timeOfDay, errorReached));
        }
コード例 #3
0
 public static IList <Dish> GetDisherByType(Enums.DishType type)
 {
     return(GetDishes().Where(x => x.DishType.Equals(type)).ToList());
 }