Code
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; //для BinaryFormatter
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Linq;
using System.Text;
//Разработать класс, объект которого реализует абстракцию «Блюдо».
//Разработать необходимые поля, методы и свойства,
//обеспечивающие изменение отдельных составных частей объекта и вывод его содержимого.
//Поля:
// - Название
// - Цена
// - Тип («холодное», «горячее», «десерт»)
//------------------------------------------------------------------
//Класс «Блюдо» должен обеспечивать путем использования перегруженных операций следующие возможности:
//b) перегрузить операцию сложения и операцию вычитания для увеличения или уменьшения цены блюда соответственно.
//c) перегрузить операцию приведения к типу long объекта типа «Блюдо», чтобы получить цену блюда.
namespace ConsoleApplication1
{
[Serializable]
class menu:food
{
public void add(food f)
{
_arr.Add(f.ToString());
}
public void GrantWish(string wish)//желание
{
_arr.Add(wish);
string FileName = _arr.Count.ToString();
FileStream fs;
BinaryFormatter formatter = new BinaryFormatter();
try
{
fs = new FileStream(FileName, FileMode.Create);
try
{
formatter.Serialize(fs, this);
}
catch (SerializationException e)
{
Console.WriteLine("Ошибка сериализации: " + e.Message);
}
finally
{
fs.Close();
Console.WriteLine("Выполнено!");
}
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\nОшибка при создании файла");
return;
}
}
/// ////////////////////////////////
public void Show__arr(int wishNumber)
{
string FileName = wishNumber.ToString();
FileStream fs;
BinaryFormatter formatter = new BinaryFormatter();
food gi = new food();
try
{
fs = new FileStream(FileName, FileMode.Open);
try
{
gi = (food)formatter.Deserialize(fs);
}
catch (SerializationException e)
{
Console.WriteLine("Ошибка десириализации: " + e.Message);
}
finally
{
fs.Close();
Console.WriteLine("\nСвойства:\n--------------");
foreach (string s in gi._arr)
{
Console.WriteLine("\t" + s);
}
}
}
catch (FileNotFoundException exc)
{
Console.WriteLine(exc.Message + "\nФайл не найден!");
return;
}
catch
{
Console.WriteLine("Не удается открыть файл!");
return;
}
}
//public void DeleteFiles()
//{
// int count = 1;
// string FileName;
// while (count <= _arr.Count)
// {
// FileName = count.ToString();
// if (File.Exists(FileName))
// {
// File.Delete(FileName);
// }
// count++;
// }
//}
}
[Serializable]
class food
{
private List<string> __arr;
private string _name;
private int _price;
private string _type;
public food()
{
_name = "Borw";
_price = 25;
_type = "first";
__arr = new List<string>();
}
public food(string n, int p, string t)
{
_name = n;
_price = p;
_type = t;
}
public food(int p)
{
_price = p;
}
public string Name//свойства
{
get { return _name; }
set { _name = value; }
}
public List<string> _arr//свойства
{
get { return __arr; }
}
public int Price
{
get { return _price; }
set { _price = value; }
}
public string type
{
get { return _type; }
set { _type = value; }
}
public override string ToString()
{
return (" Name: " + _name + " Type: " + _type + " Price: " + _price);
}
public static food operator +(food f, int n)
{
food rez = new food(f.Price + n);
return rez;
}
public static food operator -(food f, int n)
{
food rez = new food(f.Price - n);
return rez;
}
public static implicit operator long(food c)
{
long p = c.Price;
return p;
}
}
class Program
{
static void Main(string[] args)
{
food f = new food("Sup", 6, "first");
Console.WriteLine(f.ToString());//simple output
Console.WriteLine((f + 2).ToString());//+
Console.WriteLine((f - 2).ToString());//-
long m = f;
Console.WriteLine(m.ToString());//==long
menu menu2 = new menu();
menu2.add((f=new food( "SUP", 250, "borw")));
menu2.GrantWish(f.ToString());
}
}
}