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

namespace Finance
{
    class Program
    {
        static void Main(string[] args)
        {
            Address adresse = new Address("Hugo Müller","Zum Irrtum 12","37121","Velbert-Neviges");
            PurchaseOrder auftrag = new PurchaseOrder(adresse,"2005-08-02",4.55m);
            OrderItem item1 = new OrderItem("Asus MyPal 610","PDA",270.65m,1);
            OrderItem item2 = new OrderItem("Rey Color A4 100",8.99m,5);
            auftrag.OrderedItems[0] = item1;
            auftrag.OrderedItems[1] = item2;
            auftrag.ExportToXML("purchaseorder.xml");
            PurchaseOrder auftrag2 = auftrag.ImportFromXML("purchaseorder.xml");
        }
    }

    public class PurchaseOrder
    {
        public Address ShipTo;
        public string OrderDate;
        [XmlArray("Items")]
        [XmlArrayItem("Item")]
        public OrderItem[] OrderedItems;
        public decimal ShipCost;
        public decimal SubTotal
        {
            get
            {
                decimal subTotal = 0;
                foreach (OrderItem item in OrderedItems)
                    subTotal += item.UnitPrice;
                return subTotal;
            }
        }
        public decimal TotalCost
        {
            get { return SubTotal + ShipCost; }
        }
        public PurchaseOrder() {}
        public PurchaseOrder(Address shipTo, string orderDate, decimal shipCost)
        {
            ShipTo = shipTo;
            OrderDate = orderDate;
            ShipCost = shipCost;
            OrderedItems = new OrderItem[2];
        }

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

        public PurchaseOrder ImportFromXML(string fileName)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(PurchaseOrder));
            FileStream myFileStream = new FileStream(fileName, FileMode.Open);
            return (PurchaseOrder) mySerializer.Deserialize(myFileStream);
        }
    }
    
    public class Address
    {
        [XmlAttribute]
        public string Name;
        [XmlAttribute]
        public string Street;
        [XmlAttribute]
        public string Zip;
        [XmlAttribute]
        public string City;
        public Address() {}
        public Address(string name, string street, string zip, string city)
        {
            Name = name;
            Street = street;
            Zip = zip;
            City = city;
        }
    }

    public class OrderItem
    {
        [XmlAttribute]
        public string ItemName;
        [XmlAttribute]
        public string Description;
        [XmlAttribute]
        public decimal UnitPrice;
        [XmlAttribute]
        public int Quantity;
        public decimal LineTotal
        {
            get { return UnitPrice * Quantity; }
        }
        public OrderItem() { }
        public OrderItem(string itemName, decimal unitPrice, int quantity)
        {
            ItemName = itemName;
            UnitPrice = unitPrice;
            Quantity = quantity;
        }
        public OrderItem(string itemName, string description, decimal unitPrice, int quantity)
        {
            ItemName = itemName;
            Description = description;
            UnitPrice = unitPrice;
            Quantity = quantity;
        }
    }
}
