static void Main(string[] args) { //生成订单 //public Order(int id, string cname,params ProductDetails[] products ) //public ProductDetails(int pid, string pname, double pprice) OrderService.AddOrder(new Order(001, "客户1", new ProductDetails(01, "商品1", 67))); OrderService.AddOrder(new Order(007, "客户7", new ProductDetails(07, "商品7", 87))); OrderService.AddOrder(new Order(006, "客户6", new ProductDetails(06, "商品6", 199))); OrderService.AddOrder(new Order(003, "客户3", new ProductDetails(03, "商品3", 53), new ProductDetails(04, "商品4", 73), new ProductDetails(05, "商品5", 23))); Console.WriteLine("所有订单如下:"); var query1 = OrderService.QueryAll(); foreach (var q1 in query1) { Console.WriteLine(q1.ToString()); } //将所有的订单序列化为XML文件 Console.WriteLine("将所有的订单序列化为XML文件:"); Console.WriteLine(File.ReadAllText(OrderService.Export(OrderService.orders, "s.xml"))); Order Tobeload = new Order(010, "客户10", new ProductDetails(10, "商品", 100), new ProductDetails(11, "商品11", 110)); List <Order> Neworder = new List <Order>(); Neworder.Add(Tobeload); Console.WriteLine("新产生订单:"); Console.WriteLine(File.ReadAllText(OrderService.Export(Neworder, "n.xml"))); OrderService.Import(OrderService.Export(Neworder, "n.xml")); Console.WriteLine("加入新产生的订单后,所有订单如下: "); var que_2 = OrderService.QueryAll(); foreach (var theorder in que_2) { Console.WriteLine(theorder.ToString()); } /*对订单程序中OrderService的各个Public方法添加测试用例 * //查询订单 * Console.WriteLine("订单按订单号查询:"); * var query2 = OrderService.QueryById(003); * foreach (var q2 in query2) * { * Console.WriteLine(q2.ToString()); * } * * Console.WriteLine("订单按客户名查询:"); * var query3 = OrderService.QueryByClientName("客户7"); * foreach (var q3 in query3) * { * Console.WriteLine(q3.ToString()); * } * * Console.WriteLine("订单按商品号查询:"); * var query4 = OrderService.QueryByProductId(01); * foreach (var q4 in query4) * { * Console.WriteLine(q4.ToString()); * } * * Console.WriteLine("订单按商品名查询:"); * var query5 = OrderService.QueryByProductName("商品6"); * foreach (var q5 in query5) * { * Console.WriteLine(q5.ToString()); * } * * Console.WriteLine("订单按商品价格查询:"); * var query6 = OrderService.QueryByProductPrice(23); * foreach (var q6 in query6) * { * Console.WriteLine(q6.ToString()); * } * * //更改订单 * Console.WriteLine("更改订单:"); * OrderService.UpdateOrder(new Order(001, "客户10", new ProductDetails(10, "商品10", 64))); * Console.WriteLine("更改后的订单如下:"); * var query7 = OrderService.QueryAll(); * foreach (var q7 in query7) * { * Console.WriteLine(q7.ToString()); * } * * //删除订单 * Console.WriteLine("删除订单:"); * OrderService.DeleteOrder(007); * Console.WriteLine("删除后的订单如下:"); * var query8 = OrderService.QueryAll(); * foreach (var q8 in query8) * { * Console.WriteLine(q8.ToString()); * } * */ }
static void Main(string[] args) { try { Customer customer1 = new Customer(1, "Customer1"); Customer customer2 = new Customer(2, "Customer2"); Goods milk = new Goods(1, "Milk", 69.9); Goods eggs = new Goods(2, "eggs", 4.99); Goods apple = new Goods(3, "apple", 5.59); OrderDetail orderDetails1 = new OrderDetail(1, apple, 8); OrderDetail orderDetails2 = new OrderDetail(2, eggs, 2); OrderDetail orderDetails3 = new OrderDetail(3, milk, 1); Order order1 = new Order(1, customer1); Order order2 = new Order(2, customer2); Order order3 = new Order(3, customer2); order1.AddDetails(orderDetails1); order1.AddDetails(orderDetails2); order1.AddDetails(orderDetails3); //order1.AddOrderDetails(orderDetails3); order2.AddDetails(orderDetails2); order2.AddDetails(orderDetails3); order3.AddDetails(orderDetails3); OrderService os = new OrderService(); os.AddOrder(order1); os.AddOrder(order2); os.AddOrder(order3); //查询所有订单 Console.WriteLine("GetAllOrders"); List <Order> orders = os.QueryAllOrders(); foreach (Order od in orders) { Console.WriteLine(od.ToString()); } /* * //用Linq查询订单 * List<Order> ordersForLinq = new List<Order>(); * ordersForLinq.Add(order1); * ordersForLinq.Add(order2); * ordersForLinq.Add(order3); * * //根据订单号查询 * Console.WriteLine("UseLinqToGetOrdersById:'2'"); * var result = from a in ordersForLinq where a.Id == 2 select a; * foreach (var a in result) * { * Console.WriteLine(a.ToString()); * } * * //根据客户名查询 * Console.WriteLine("UseLinqToGetOrdersByCustomerName:Customer1"); * var result2 = from b in ordersForLinq where b.Customer.Name == "Customer1" select b; * foreach (var b in result2) * { * Console.WriteLine(b.ToString()); * } * * //根据商品名查询 * Console.WriteLine("UseLinqToGetOrdersByGoodsName:Milk"); * var result3 = from c in ordersForLinq where c.Goods.Name == "Milk" select c; * foreach (var c in result2) * { * Console.WriteLine(c.ToString()); * } */ //根据客户名查询 Console.WriteLine("GetOrdersByCustomerName:'Customer2'"); orders = os.QueryByCustomerName("Customer2"); foreach (Order od in orders) { Console.WriteLine(od.ToString()); } Console.WriteLine("GetOrdersByGoodsName:'apple'"); orders = os.QueryByGoodsName("apple"); foreach (Order od in orders) { Console.WriteLine(od.ToString()); } Console.WriteLine("Remove order(id=2) and qurey all"); os.RemoveOrder(2); os.QueryAllOrders().ForEach( od => Console.WriteLine(od)); //Xml序列化 OrderService.ordersForXml.Add(order1); OrderService.ordersForXml.Add(order2); OrderService.ordersForXml.Add(order3); os.Export("D:/XmlForText.xml"); //显示XML文本 os.Import("F:/XmlForText.xml"); } catch (Exception e) { Console.WriteLine(e.Message); } }