public static void Main(string[] args) { string[] buf = File.ReadAllLines("Data.txt"); int size = buf.Length; List<Order> orders = new List<Order>(); for (int i = 0; i < buf.Length; i++) { buf[i] = buf[i].PadRight(buf[i].Length + 1, ' '); Order temp = new Order(); temp.MakeOrder(buf[i]); orders.Add(temp); } for (int i = 0; i < orders.Count; i++) { orders[i].Write(); } string curFile = @"Result.xml"; if (!File.Exists(curFile)) { XmlWriter fs = XmlWriter.Create(curFile); fs.WriteStartElement("head"); fs.WriteFullEndElement(); fs.Close(); } XmlDocument document = new XmlDocument(); document.Load(curFile); XmlElement root = document.DocumentElement; //orders[0].AddProduct(1); //orders[0].RemoveProduct(1); for (int i = 0; i < orders.Count; i++) { orders[i].SaveOrder(document); } document.Save("Result.xml"); Order a = new Order(); a.ReturnOrderById(1); Console.ReadKey(); }
/// <summary> /// gets order by id /// </summary> /// <param name="identifier"> id of order</param> /// <returns>order with id</returns> public Order ReturnOrderById(int identifier) { int id = -1; int prodID = 0; int prodAmount = 0; XmlTextReader reader = new XmlTextReader("Result.xml"); Order tempOrder = new Order(); tempOrder.ID = identifier; while (reader.Read()) { if (reader.Name == "orderId" && reader.HasAttributes) { id = Convert.ToInt32(reader.GetAttribute("name_id")); if (id == identifier) { while (reader.Read()) { if (reader.Name == "id") { prodID = reader.ReadElementContentAsInt(); } if (reader.Name == "amount") { prodAmount = reader.ReadElementContentAsInt(); tempOrder.AddProduct(prodID, prodAmount); } if (reader.Name == "orderId") { return tempOrder; } } } } } return tempOrder; }
private static void PerformOrder() { Order order=new Order(); bool ordering=true; try { while(ordering) { Console.WriteLine("Enter 1 to add new product 0 to end order:"); char option=Convert.ToChar(Console.ReadLine()); switch(option) { case '0': { ordering=false; break; } case '1': { DataBase database=DataBase.Instance; database.SetConnections(Constants.dataBasePath); database.LoadProducts(); Console.WriteLine("Enter id of product:"); int id = Convert.ToInt32(Console.ReadLine()); //order.AddProduct(database.GetProductById(id),1); break; } default: { throw new Exception(); } } } } catch (Exception e) { Console.WriteLine(""); } }
public Order GetOrderById(int id) { Order retOrd = new Order(); bool found = false; foreach (Order ord in this.Orders) { if (ord.ID == id) { retOrd = ord; found = true; break; } else { continue; } } if (found) { return retOrd; } else { throw new ArgumentException("No user with id: >" + id.ToString() + "< found"); } }
public void Add(Order newOrder) { this.orders.Add(newOrder); }
/// <summary> /// gets order by id /// </summary> /// <param name="Id"> id of order</param> /// <returns>order with id</returns> public Order ReturnOrderById(int Id) { int id = -1; int prodID = 0; XmlTextReader raeder = new XmlTextReader("Result.xml"); Order tempOrder = new Order(); while (raeder.Read()) { if (raeder.Name == "userId" && raeder.HasAttributes) { id = Convert.ToInt32(raeder.GetAttribute("name_id")); if (id == Id) { while (raeder.Read()) { if (raeder.Name == "id") { prodID = raeder.ReadElementContentAsInt(); tempOrder.AddProduct(prodID); } if (raeder.Name == "userId") { return tempOrder; } } } } } return tempOrder; }