Esempio n. 1
0
        /// <summary>
        /// Load Fruits from File
        /// </summary>
        /// <param name="path">Path to file</param>
        /// <returns>FruitsBasket(colletion)</returns>
        public static FruitsBasket LoadFromFile(string path)
        {
            FruitsBasket freshFruits = new FruitsBasket();

            string[] fruits = FileHelper.FileToString(path).Split(new char[] { '\n' });

            using (StreamReader sr = new StreamReader(path))
            {
                while (!sr.EndOfStream)
                {
                    string[] fruit = sr.ReadLine().Split(new char[] { '-', '/' });
                    if (fruit.Length == 2)
                    {
                        Fruit newFruit = new Fruit();
                        newFruit.Input(fruit);
                        freshFruits.Add(newFruit);
                    }
                    else if (fruit.Length == 3)
                    {
                        Fruit newFruit = new Citrus();
                        newFruit.Input(fruit);
                        freshFruits.Add(newFruit);
                    }
                }
            }

            return(freshFruits);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            FruitsBasket fruitsBasket = new FruitsBasket();

            Console.WriteLine(currentDir);

            if (FileHelper.CheckFile(currentDir + FreshFruitData))
            {
                //Load Fruits from File
                Console.WriteLine("Load Fruits in Basket from File");
                fruitsBasket = FileHelper.LoadFromFile(currentDir + FreshFruitData);
            }
            else
            {
                //Load Fruits from Console
                Console.WriteLine("Load Fruits in Basket from console");
                for (int i = 0; i < 4; i++)
                {
                    if (i < 2)
                    {
                        fruitsBasket.Add(new Fruit());
                        fruitsBasket.FruitsColection[i].Input();
                    }
                    else
                    {
                        fruitsBasket.Add(new Citrus());
                        fruitsBasket.FruitsColection[i].Input();
                    }
                }
            }
            //FileHelper.SaveToFile(currentDir + FreshFruitData, fruitsBasket.FruitsColection);
            Console.WriteLine("\nPrint All Fruits\n");
            foreach (var fruit in fruitsBasket.FruitsColection)
            {
                fruit.Print();
            }

            //Print list fruit with color
            Console.Write("\nEnter Fruits color: ");
            string color = Console.ReadLine();
            IEnumerable <Fruit> fruitsChoosenColor = fruitsBasket.ShowFruitWithColor(fruitsBasket.FruitsColection, color);

            if (fruitsChoosenColor.Count() > 0)
            {
                Console.Write($"\nAll Fruits with color {color}\n");
                foreach (var fruit in fruitsChoosenColor)
                {
                    fruit.Print();
                }
            }
            else
            {
                Console.Write($"\nCan't find fruits with color{color}\n");
            }

            //Sort Fruits by parameter
            Console.Write("\nSort Fruits by name\n");
            IEnumerable <Fruit> sortedFruits = fruitsBasket.Sort("");

            //fruitsBasket.Sort();

            //Print Sorted Fruits in File
            Console.Write("\nSave To file sorted Fruits\n");
            FileHelper.SaveToFile(currentDir + SortedFruits, sortedFruits);
            //FileHelper.SaveToFile(SortedFruits, fruitsBasket.FFruits);

            Console.WriteLine("");

            //Print Sorted Fruits in console
            Console.Write("\nShow sorted Fruits\n");
            foreach (var fruit in sortedFruits)
            {
                fruit.Print();
            }

            Console.Write("\nSerialize Fruits\n");
            SerializerHelper.SerializeXml(fruitsBasket, currentDir + FreshFruitXML);

            Console.Write("\nDeserialize Fruits\n");

            if (!FileHelper.CheckFile(currentDir + FreshFruitXML))
            {
                throw new Exception("File not Found");
            }

            FruitsBasket deserializeFruits = (FruitsBasket)SerializerHelper.XmlToObject <FruitsBasket>(
                FileHelper.FileToString(currentDir + FreshFruitXML));

            if (deserializeFruits == null)
            {
                throw new Exception("XML Parsing Error");
            }

            Console.Write("\nShow deserialize Fruits\n");
            foreach (var fruit in deserializeFruits.FruitsColection)
            {
                fruit.Print();
            }

            Console.Write("\nPress any key....");
            Console.ReadKey();
        }