using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Formatters.Binary;

namespace Serialisierung
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Product p1 = new Product("Milch");
            Product p2 = new Product("Zucker");
            Product p3 = new Product("Eier");

            Product[] p = { p1, p2, p3 };

            Customer c = new Customer();
            c.Firstname = "Shinja";
            c.Lastname = "Strasser";
            c.Products = p;

            SerializeBinary(c);
            SerializeSoap(c);

            DeserializeBinary();
            DeserializeSoap();

            Console.ReadLine();
        }

        public static void SerializeBinary(Customer customer)
        {
            FileStream fs = new FileStream(@"customer.bin", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, customer);
            fs.Close();
        }

        public static void SerializeSoap(Customer customer)
        {
            FileStream fs = new FileStream(@"customer.soap", FileMode.Create);
            SoapFormatter sf = new SoapFormatter();
            sf.Serialize(fs, customer);
            fs.Close();
        }

        public static void DeserializeBinary()
        {
            FileStream fs = new FileStream(@"customer.bin", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            Customer c = (Customer)bf.Deserialize(fs);

            Console.WriteLine(c.Firstname);
            Console.WriteLine(c.Lastname);

            for (int i = 0; i < c.OrderCounter.Length; i++)
            {
                Console.WriteLine(c.OrderCounter[i].ToString());
            }

            fs.Close();
        }

        public static void DeserializeSoap()
        {
            FileStream fs = new FileStream(@"customer.soap", FileMode.Open);
            SoapFormatter sf = new SoapFormatter();
            Customer c = (Customer)sf.Deserialize(fs);

            Console.WriteLine(c.Firstname);
            Console.WriteLine(c.Lastname);

            for (int i = 0; i < c.OrderCounter.Length; i++)
            {
                Console.WriteLine(c.OrderCounter[i].ToString());
            }

            fs.Close();
        }
    }

}
