/// <summary> /// Main function. /// </summary> public static void Run() { Product p = new Product(); XmlTextReader reader = new XmlTextReader(@"C:\Users\Oleh\Documents\Visual Studio 2012\Projects\ОБЧ\ОБЧ\XMLFile1.xml"); p.Read(reader); p.Write(); XmlTextWriter writer = new XmlTextWriter("result.xml", Encoding.UTF8); p.Write(writer); Product n = new Product(); p.Read(reader); p.Write(); p.Write(writer); writer.Close(); Console.WriteLine("\nPress any key to continue"); System.Console.ReadKey(); }
/// <summary> /// Initializes a new instance of the <see cref="Product" /> class with values from Product from parameters box. /// </summary> /// <param name="p">Object of class Product.</param> public Product(Product p) { this.id = p.id; this.name = p.name; this.price = p.price; }
/// <summary> /// writes to xml document /// </summary> /// <param name="doc"> xml document</param> public void SaveOrder(XmlDocument doc) { Product temp = new Product(); XmlNode root = doc.DocumentElement; XmlElement newID = doc.CreateElement("orderId"); XmlAttribute attrID = doc.CreateAttribute("name_id"); attrID.Value = Convert.ToString(this.id); newID.SetAttributeNode(attrID); root.InsertAfter(newID, root.LastChild); foreach (KeyValuePair<int, int> pair in this.products) { XmlElement newProduct = doc.CreateElement("Product"); XmlElement newId = doc.CreateElement("id"); XmlText identifier = doc.CreateTextNode(pair.Key.ToString()); newId.AppendChild(identifier); XmlElement newPrice = doc.CreateElement("price"); XmlText cost = doc.CreateTextNode((temp.PriceById(pair.Key)*pair.Value).ToString()); newPrice.AppendChild(cost); newProduct.AppendChild(newId); newProduct.AppendChild(newPrice); XmlElement newAmount = doc.CreateElement("amount"); XmlText amountStr = doc.CreateTextNode(pair.Value.ToString()); newAmount.AppendChild(amountStr); newProduct.AppendChild(newAmount); newProduct.AppendChild(newProduct); } }
private static void SaveProductDB(XmlDocument doc, Product myProduct) { XmlNode root = doc.DocumentElement; XmlElement newID = doc.CreateElement("ProductId"); XmlAttribute attrID = doc.CreateAttribute("product_id"); attrID.Value = myProduct.Id.ToString(); newID.SetAttributeNode(attrID); root.InsertAfter(newID, root.LastChild); XmlElement prodName = doc.CreateElement("ProductName"); XmlElement prodPrice = doc.CreateElement("ProductPrice"); XmlText pName = doc.CreateTextNode(myProduct.Name); XmlText pPrice = doc.CreateTextNode(myProduct.Price.ToString()); prodName.AppendChild(pName); prodPrice.AppendChild(pPrice); root.InsertAfter(prodName, root.LastChild); root.InsertAfter(prodPrice, root.LastChild); }
/// <summary> /// Adds new product to DataBase. /// </summary> private static void AddNewProduct() { int id; string name; double price; Console.WriteLine("Input id of product"); id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Input name of product"); name = Console.ReadLine(); Console.WriteLine("Input price of product"); price = Convert.ToDouble(Console.ReadLine()); Product product = new Product(id, name, price); DataBase db = DataBase.GetInstance(); db.Add(product); }
public Product GetProductById(int id) { Product retPrd = new Product(); bool found = false; foreach (Product prd in this.Products) { if (prd.Id == id) { retPrd = prd; found = true; break; } else { continue; } } if (found) { return retPrd; } else { throw new ArgumentException("No product with id: >" + id.ToString() + "< found"); } }
public void LoadProducts() { XmlTextReader reader = new XmlTextReader(this.ProductsPath); Product p = new Product(); int id = -1; string name = "no_name"; double price = -1.0; while (reader.Read()) { if (reader.Name == "ProductId" && reader.HasAttributes) { id = Convert.ToInt32(reader.GetAttribute("product_id")); } if (reader.Name == "ProductName") { name = reader.ReadElementContentAsString(); } if (reader.Name == "ProductPrice") { price = reader.ReadElementContentAsDouble(); this.products.Add(new Product(id, name, price)); Console.WriteLine(1); } } reader.Close(); }
/// <summary> /// Initializes a new instance of the <see cref="Products" /> class with values from Product from parameters box. /// </summary> /// <param name="p">Object of class Product.</param> public Products(Products p) { this.product = p.product; this.amount = p.amount; }
public void Add(Product newProduct) { this.products.Add(newProduct); }
/// <summary> /// Initializes a new instance of the <see cref="Products" /> class with values from parameters of constructor. /// </summary> /// <param name="product">Previous information about product.</param> /// <param name="amount">Amount of product</param> public Products(Product product, int amount) { this.product = product; this.amount = amount; }
/// <summary> /// Initializes a new instance of the <see cref="Products" /> class with values from parameters of constructor. /// </summary> /// <param name="id">ID of product.</param> /// <param name="name">Name of product.</param> /// <param name="price">Price of product.</param> /// <param name="amount">Amount of product</param> public Products(int id, string name, double price, int amount) { this.product = new Product(id, name, price); this.amount = amount; }
/// <summary> /// Initializes a new instance of the <see cref="Products" /> class with default values: (0, "Product", 0, 0). /// </summary> public Products() { this.product = new Product(); this.amount = 0; }
/// <summary> /// makes order from text file /// </summary> /// <param name="fullOrder">line of order </param> public void MakeOrder(string fullOrder) { int productID; double productPrice; int counter = 0; this.id = int.Parse(fullOrder.Substring(0, fullOrder.IndexOf(' '))); fullOrder = fullOrder.Remove(0, fullOrder.IndexOf(' ') + 1); for (int i = 0; i < fullOrder.Length; i++) { if (fullOrder[i].Equals(' ')) { counter++; } if (counter == 2) { productID = int.Parse(fullOrder.Substring(0, fullOrder.IndexOf(' '))); fullOrder = fullOrder.Remove(0, fullOrder.IndexOf(' ') + 1); productPrice = double.Parse(fullOrder.Substring(0, fullOrder.IndexOf(' '))); fullOrder = fullOrder.Remove(0, fullOrder.IndexOf(' ') + 1); Product temp = new Product(productID, string.Empty, productPrice); this.products.Add(temp); counter = 0; i = -1; } } }
/// <summary> /// Adds product to order /// </summary> /// <param name="id">id of product</param> public void AddProduct(int identifier) { Product temp = new Product(); XmlTextReader reader = new XmlTextReader(@"XMLFile1.xml"); temp.Price = temp.PriceById(identifier, reader); temp.Id = identifier; this.products.Add(temp); }