Example #1
0
        /// <summary>
        /// input from console method
        /// </summary>
        /// <param name="fruitsCount">
        /// Number of fruits types.
        /// </param>
        /// <returns>
        /// List with information about fruits, entered via console.
        /// </returns>
        public static List <Fruit> InputFromConsole(int fruitsCount)
        {
            List <Fruit> fruitsList = new List <Fruit>();

            for (int i = 0; i < fruitsCount; i++)
            {
                Console.Write("Please select the type of fruit (e.g. Сitrus/Other): ");
                string choice = Console.ReadLine().ToLower();

                while (choice != "other" && choice != "citrus" && choice != "c" && choice != "o")
                {
                    Console.WriteLine("You entered an incorrect type, check it and try again.");
                    choice = Console.ReadLine().ToLower();
                }

                if (choice == "other" || choice == "o")
                {
                    Fruit fruit = new Fruit();
                    try
                    {
                        fruit.Input();
                        fruitsList.Add(fruit);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else if (choice == "citrus" || choice == "c")
                {
                    Citrus citrus = new Citrus();
                    try
                    {
                        citrus.Input();
                        fruitsList.Add(citrus);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            return(fruitsList);
        }
Example #2
0
        /// <summary>
        /// The entry point for the application.
        /// </summary>
        /// <returns>
        /// List with information about fruits, get from the file.
        /// </returns>
        public static List <Fruit> InputFromFile()
        {
            List <Fruit> fruitsList         = new List <Fruit>();
            StreamReader readFromFruitsFile = new StreamReader(Constants.INPUT_FRUIT_FILE);
            string       line;

            try
            {
                while ((line = readFromFruitsFile.ReadLine()) != null)
                {
                    string[] currentLine = line.Split('/');

                    switch (currentLine.Length)
                    {
                    case 2:
                        Fruit newFruit = new Fruit();
                        newFruit.Input(currentLine);
                        fruitsList.Add(newFruit);
                        break;

                    case 3:
                        Citrus newCitrus = new Citrus();
                        newCitrus.Input(currentLine);
                        fruitsList.Add(newCitrus);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                readFromFruitsFile.Close();
            }
            return(fruitsList);
        }