using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Warehouse
{
    class Program
    {
        static void Main(string[] args)
        {
            Kaese Gouda = new Kaese();
            Gouda.Name = "Gouda";
            Gouda.Preis = 1.99m;
            Gouda.Waehrung = "euro";
            Gouda.Herkunftsland = "Niederlande";
            Kaese Emmentaler = new Kaese();
            Emmentaler.Name = "Emmentaler";
            Emmentaler.Preis = 2.49m;
            Emmentaler.Waehrung = "euro";
            Emmentaler.Herkunftsland = "Belgien";
            Wurst TuehringerRostbratwurst = new Wurst();
            TuehringerRostbratwurst.Name = "Tuehringer Rostbratwurst";
            TuehringerRostbratwurst.Preis = 1.39m;
            TuehringerRostbratwurst.Waehrung = "euro";
            TuehringerRostbratwurst.Herkunftsland = "Deutschland";

            WarehouseAdministration WarenListe = new WarehouseAdministration();
            WarenListe.AddItem(Gouda);
            WarenListe.AddItem(Emmentaler);
            WarenListe.AddItem(TuehringerRostbratwurst);
            WarenListe.ExportToXML("warehouse.xml");
            WarehouseAdministration WarenListe2 = WarenListe.ImportFromXML("warehouse.xml");
        }
    }

    public class WarehouseAdministration
    {
        [XmlElement(typeof(Kaese)), XmlElement(typeof(Yoghurt)), XmlElement(typeof(Wurst)), XmlElement(typeof(Parfuem))]
        public List<Ware> liste;

        public WarehouseAdministration()
        {
            liste = new List<Ware>();
        }

        public void AddItem(Ware ware)
        {
            liste.Add(ware);
        }

        public void ExportToXML(string fileName)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(WarehouseAdministration));
            StreamWriter myWriter = new StreamWriter(fileName);
            mySerializer.Serialize(myWriter,this);
            myWriter.Close();
        }

        public WarehouseAdministration ImportFromXML(string fileName)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(WarehouseAdministration));
            FileStream myFileStream = new FileStream(fileName, FileMode.Open);
            WarehouseAdministration warenliste = (WarehouseAdministration)mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return warenliste;
        }
    }

    abstract public class Ware
    {
        [XmlAttribute]
        public string Name;
        public decimal Preis;
        public string Waehrung;
        public string Herkunftsland;
    }
    abstract public class Nahrungsmittel : Ware { }
    abstract public class Kosmetik : Ware { }
    abstract public class Milchprodukte : Nahrungsmittel { }
    abstract public class Fleischwaren : Nahrungsmittel { }
    abstract public class Duft : Kosmetik { }
    public class Kaese : Milchprodukte { }
    public class Yoghurt : Milchprodukte { }
    public class Wurst : Fleischwaren { }
    public class Parfuem : Duft { }
}
