Example #1
0
        private void loadRecipes()
        {
            string path = Directory.GetCurrentDirectory();

            path  = path.Remove(path.Length - 10);
            path += @"\Resources\recipes.txt";
            System.IO.StreamReader file = new System.IO.StreamReader(path);    //how do I access specific resource file
            string line;
            bool   end = false;

            // may want to refactor in the future
            while (!file.EndOfStream)
            {
                // create each recipe, populate variables
                Recipe a_recipe = new Recipe();
                line = file.ReadLine();
                a_recipe.setName(line);
                line = file.ReadLine();
                a_recipe.setImagePath(line);
                // ingredient amounts
                line = file.ReadLine();
                string[] str_amounts = line.Split(',');
                double[] amounts     = new double[str_amounts.Length];
                for (int i = 0; i < str_amounts.Length; i++)
                {
                    amounts[i] = Convert.ToDouble(str_amounts[i]);
                }
                a_recipe.setAmounts(amounts);
                // ingredient units
                line = file.ReadLine();
                string[] units = line.Split(',');
                a_recipe.setUnits(units);
                // ingredients
                line = file.ReadLine();
                string[] ingredients = line.Split(',');
                a_recipe.setIngredients(ingredients);
                // ingredient notes
                line = file.ReadLine();
                string[] ing_notes = line.Split(',');
                a_recipe.setIngNotes(ing_notes);
                // process
                string full_process = "";
                while (!end)    // read full process until last line (which starts with *)
                {
                    line = file.ReadLine();
                    if (line[0] == '*')
                    {
                        end  = true;
                        line = line.Substring(1);   // do I want to keep the * in the string for re-writing?
                    }
                    else
                    {
                        line += "\r\n\n";
                    }
                    full_process += line;
                }
                a_recipe.setProcess(full_process);
                end = false;
                // additional notes
                string full_notes = "";
                while (!end)    // read full notes until last line (which starts with *)
                {
                    line = file.ReadLine();
                    if (line[0] == '*')
                    {
                        end  = true;
                        line = line.Substring(1);   // do I want to keep the * in the string for re-writing?
                    }
                    else
                    {
                        line += "\r\n\n";
                    }
                    full_notes += line;
                }
                a_recipe.setNotes(line);
                end = false;

                // add recipe to root list
                recipes.Add(a_recipe);
                num_recipes++;
            }
            file.Close();
            ing_search_results = recipes;
            updateSearchResults(recipes);
        }