Exemplo n.º 1
0
        /// <summary>
        /// Разобрать строку содержащую информацию об ингредиенте
        /// </summary>
        /// <param name="input_string">Строка с информацией об ингредиенте</param>
        /// <returns>Возвращает Ingredient</returns>
        static Ingredient ParseIngredient(string input_string)
        {
            //Формат
            //"ingredient : Name = Мука, Value = 0.3, Calorie = 364, Price = 1.39;"

            Ingredient ingredient = new Ingredient();

            //Определяем позицию после двоеточия
            int    positions   = input_string.LastIndexOf(':');
            string edit_string = input_string.Substring(positions + 1);

            //Разбиваем строку на массив строк
            string[] string_array = edit_string.Split(',', StringSplitOptions.RemoveEmptyEntries);
            //Получаем параметры
            foreach (var item in string_array)
            {
                string[] parameter;

                parameter = item.Split(new char[] { '=' }); //Разбиваем параметр на 2 части
                if (parameter.Length < 2)                   //Проверяем длину
                {
                    continue;
                }
                string type      = parameter[0];
                string str_value = parameter[1];
                double value;

                switch (type)
                {
                case "Name":
                    try
                    {
                        ingredient.Name = str_value;
                    }
                    catch
                    {
                        ingredient.Name = "";
                    }
                    break;

                case "Value":
                    try
                    {
                        str_value = str_value.Replace('.', ',');
                        double.TryParse(str_value, out value);
                        ingredient.Value = value;
                    }
                    catch
                    {
                        ingredient.Value = 0;
                    }
                    break;

                case "Calorie":
                    try
                    {
                        str_value = str_value.Replace('.', ',');
                        double.TryParse(str_value, out value);
                        ingredient.Calorie = value;
                    }
                    catch
                    {
                        ingredient.Calorie = 0;
                    }
                    break;

                case "Price":
                    try
                    {
                        str_value = str_value.Replace('.', ',');
                        double.TryParse(str_value, out value);
                        ingredient.Price = value;
                    }
                    catch
                    {
                        ingredient.Price = 0;
                    }
                    break;
                }
            }
            return(ingredient);
        }
Exemplo n.º 2
0
 public RecipeIngredient(Ingredient ingredient, int amount)
 {
     Ingredient = ingredient;
     Amount     = amount;
 }