PageRenderTime 26ms CodeModel.GetById 23ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

/Katmai_June_CTP/ShoppingCart/ShoppingCartService/CS/ShoppingCartService/Invoice.cs

#
C# | 93 lines | 72 code | 7 blank | 14 comment | 0 complexity | 9948effe47291b38c031b73b576a570f MD5 | raw file
 1//-----------------------------------------------------------------------
 2//  This file is part of the Microsoft Code Samples.
 3// 
 4//  Copyright (C) Microsoft Corporation.  All rights reserved.
 5// 
 6//This source code is intended only as a supplement to Microsoft
 7//Development Tools and/or on-line documentation.  See these other
 8//materials for detailed information regarding Microsoft code samples.
 9// 
10//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13//PARTICULAR PURPOSE.
14//-----------------------------------------------------------------------
15
16#region Using directives
17
18using System;
19using System.Collections.Generic;
20using System.IO;
21using System.Text;
22using System.Xml;
23
24#endregion
25
26namespace Microsoft.Samples.SqlServer
27{
28    public class Invoice
29    {
30        private int m_numberItems;
31        public int NumberItems
32        {
33            get { return m_numberItems; }
34        }
35
36        private double m_totalAmount;
37        public double TotalAmount
38        {
39            get { return m_totalAmount; }
40        }
41
42        private Dictionary<int, LineItem> m_shoppingCart;
43        public Invoice(Dictionary<int, LineItem> shoppingCart)
44        {
45            m_shoppingCart = shoppingCart;
46            foreach (int itemNumber in m_shoppingCart.Keys)
47            {
48                LineItem li = m_shoppingCart[itemNumber];
49                m_numberItems += li.Quantity;
50                m_totalAmount += li.Price;
51            }
52        }
53
54        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Double.ToString")]
55        public Stream Serialize()
56        {
57            MemoryStream memstream = new MemoryStream();
58            XmlWriter writer = new XmlTextWriter(memstream, Encoding.ASCII);
59            writer.WriteStartElement("Invoice");
60            foreach (int itemNumber in m_shoppingCart.Keys)
61            {
62                LineItem li = m_shoppingCart[itemNumber];
63                writer.WriteStartElement("LineItem");
64                writer.WriteStartElement("ItemNumber");
65                writer.WriteString(li.ItemNumber.ToString());
66                writer.WriteEndElement();
67                writer.WriteStartElement("Name");
68                writer.WriteString(li.Name);
69                writer.WriteEndElement();
70                writer.WriteStartElement("UnitPrice");
71                writer.WriteString(li.UnitPrice.ToString());
72                writer.WriteEndElement();
73                writer.WriteStartElement("Quantity");
74                writer.WriteString(li.Quantity.ToString());
75                writer.WriteEndElement();
76                writer.WriteStartElement("Price");
77                writer.WriteString(li.Price.ToString());
78                writer.WriteEndElement();
79                writer.WriteEndElement();
80            }
81            writer.WriteStartElement("NumberItems");
82            writer.WriteString(NumberItems.ToString());
83            writer.WriteEndElement();
84            writer.WriteStartElement("TotalAmount");
85            writer.WriteString(TotalAmount.ToString());
86            writer.WriteEndElement();
87            writer.WriteEndElement();
88            writer.Flush();
89            memstream.Seek(0, SeekOrigin.Begin);
90            return memstream;
91        }
92    }
93}