internal static void Display(Bouquet bouquet) { Console.WriteLine("*************Букет из {0} цветка(-ов)*************", bouquet.BouquetCount); for (int i = 0; i < bouquet.BouquetCount; i++) { Console.WriteLine("{0} - {1}, {2} руб", i + 1, bouquet.flowers[i].Name, bouquet.flowers[i].Price); } Console.ReadKey(); }
internal static void SerializeToBinFile(Bouquet bouquet) { BinaryFormatter bf = new BinaryFormatter(); using (FileStream fs = new FileStream("BouquetFromBinFile.bin", FileMode.Create)) { bf.Serialize(fs, bouquet.flowers); } Console.WriteLine("Букет записан в бинарный файл"); Console.ReadKey(); }
internal static void SerializeToXmlFile(Bouquet bouquet) { XmlSerializer xs = new XmlSerializer(typeof(List <Flower>), new Type[] { typeof(List <Rose>), typeof(List <Tulip>), typeof(List <Lily>) }); using (FileStream fs = new FileStream("BouquetFromXmlFile.xml", FileMode.Create)) { xs.Serialize(Console.Out, bouquet.flowers); xs.Serialize(fs, bouquet.flowers); } Console.WriteLine("\nБукет записан в XML файл"); Console.ReadKey(); }
internal static void WriteToTextFile(Bouquet bouquet) { StreamWriter fileOut = new StreamWriter("BouquetFromTextFile.txt", false); foreach (var flower in bouquet.flowers) { fileOut.WriteLine("{0},{1},{2}", flower.Code, flower.Name, flower.Price); } fileOut.Close(); Console.WriteLine("Букет записан в текстовый файл"); Console.ReadKey(); }
public static void Create(ref Bouquet bouquet) { if (bouquet.BouquetCount > 0) { bouquet = new Bouquet(); } Console.WriteLine("Введите количество цветков в букете: "); bouquet.BouquetCount = Int32.Parse(Console.ReadLine()); for (int i = 0; i < bouquet.BouquetCount; i++) { AddFlowerToBouquet(bouquet, false); } Console.WriteLine("Букет создан."); Console.ReadKey(); }
internal static void DeserializeFromXmlFile(ref Bouquet bouquet) { if (bouquet.BouquetCount > 0) { bouquet = new Bouquet(); } XmlSerializer xs = new XmlSerializer(typeof(List <Flower>), new Type[] { typeof(List <Rose>), typeof(List <Tulip>), typeof(List <Lily>) }); using (FileStream fs = new FileStream("BouquetFromXmlFile.xml", FileMode.Open)) { bouquet.flowers = (List <Flower>)xs.Deserialize(fs); bouquet.BouquetCount = bouquet.flowers.Count; } Console.WriteLine("Букет прочитан из XML файла"); Console.ReadKey(); }
internal static void DeserializeFromBinFile(ref Bouquet bouquet) { if (bouquet.BouquetCount > 0) { bouquet = new Bouquet(); } BinaryFormatter bf = new BinaryFormatter(); using (FileStream fs = new FileStream("BouquetFromBinFile.bin", FileMode.Open)) { bouquet.flowers = (List <Flower>)bf.Deserialize(fs); bouquet.BouquetCount = bouquet.flowers.Count; } Console.WriteLine("Букет прочитан из бинарного файла"); Console.ReadKey(); }
internal static void AddFlowerToBouquet(Bouquet bouquet, bool changeCount = true) { Console.WriteLine("Выберете цветок:\n1 - Лилия\n2 - Роза\n3 - Тюльпан"); int flowerNumber = Int32.Parse(Console.ReadLine()); Console.WriteLine("Введите цену в руб: "); double flowerPrice = Double.Parse(Console.ReadLine()); Flower flower = FindFlower(flowerNumber, flowerPrice); bouquet.AddFlower(flower); if (changeCount == true) { bouquet.BouquetCount++; } Console.WriteLine("Цветок добавлен в букет."); Console.ReadKey(); }
internal static void ReadFromTextFile(ref Bouquet bouquet) { if (bouquet.BouquetCount > 0) { bouquet = new Bouquet(); } StreamReader fileIn = new StreamReader("BouquetFromTextFile.txt", Encoding.Default); string line; while ((line = fileIn.ReadLine()) != null) { string[] parts = line.Split(','); Flower flower = FindFlower(Int32.Parse(parts[0]), Double.Parse(parts[2])); bouquet.AddFlower(flower); bouquet.BouquetCount++; } fileIn.Close(); Console.WriteLine("Букет прочитан из текстововго файла"); Console.ReadKey(); }
internal static void DisplayPrice(Bouquet bouquet) { Console.WriteLine("Цена букета составляет {0:0.00} руб.", bouquet.GetTotalPrice()); Console.ReadKey(); }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; Bouquet bouquet = new Bouquet(); while (true) { Console.Clear(); Console.WriteLine("Меню:"); Console.WriteLine("1 - Создать новый букет"); Console.WriteLine("2 - Добавить цветок"); Console.WriteLine("3 - Просмотреть букет"); Console.WriteLine("4 - Рассчитать стоимость букета"); Console.WriteLine("\n******TXT******"); Console.WriteLine("5 - Прочитать букет из текстового файла"); Console.WriteLine("6 - Записать букет в текстовый файл"); Console.WriteLine("\n******BIN******"); Console.WriteLine("7 - Прочитать букет из бинарного файла"); Console.WriteLine("8 - Записать букет в бинарный файл"); Console.WriteLine("\n******XML******"); Console.WriteLine("9 - Прочитать букет из XML файла"); Console.WriteLine("10 - Записать букет в XML файл"); Console.WriteLine("\n11 - Выход"); Console.Write("\nВаш выбор: "); try { int choice = Int32.Parse(Console.ReadLine()); switch (choice) { case 1: BouquetManager.Create(ref bouquet); break; case 2: BouquetManager.AddFlowerToBouquet(bouquet); break; case 3: BouquetManager.Display(bouquet); break; case 4: BouquetManager.DisplayPrice(bouquet); break; case 5: BouquetManager.ReadFromTextFile(ref bouquet); break; case 6: BouquetManager.WriteToTextFile(bouquet); break; case 7: BouquetManager.DeserializeFromBinFile(ref bouquet); break; case 8: BouquetManager.SerializeToBinFile(bouquet); break; case 9: BouquetManager.DeserializeFromXmlFile(ref bouquet); break; case 10: BouquetManager.SerializeToXmlFile(bouquet); break; case 11: Environment.Exit(0); break; default: Console.WriteLine("Неверный выбор"); Console.ReadKey(); break; } } catch (OutOfMemoryException error) { Console.WriteLine("Возникла ошибка нехватки памяти для нового объекта: {0}", error.Message); Console.ReadKey(); } catch (IndexOutOfRangeException error) { Console.WriteLine("Возникла ошибка выхода индекса массива за границу массива: {0}", error.Message); Console.ReadKey(); } catch (FormatException error) { Console.WriteLine("Возникла ошибка формата данных: {0}", error.Message); Console.ReadKey(); } catch (OverflowException error) { Console.WriteLine("Возникло переполнение: {0}", error.Message); Console.ReadKey(); } catch (Exception error) { Console.WriteLine("Возникла ошибка: {0}", error.Message); Console.ReadKey(); } } }