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()); } //查询订单 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)); } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { OrderService service = new OrderService(); while (true) { Console.WriteLine("请选择功能:添加订单;删除订单;查找订单;设置订单;退出系统。"); string mod = Console.ReadLine(); switch (mod) { case "添加订单": List <OrderItem> orderItems = new List <OrderItem>(); while (true) { Console.WriteLine("请输入商品名称:"); string goodname = Console.ReadLine(); Console.WriteLine("请输入商品数量:"); int goodnum = Int32.Parse(Console.ReadLine()); Console.WriteLine("请输入商品单价"); int goodprice = Int32.Parse(Console.ReadLine()); Console.WriteLine("请输入客户名:"); string cusname = Console.ReadLine(); Console.WriteLine("是否购买其他商品:是/否"); string flag = Console.ReadLine(); if (flag == "是") { orderItems.Add(new OrderItem(goodname, goodnum, goodprice)); } else if (flag == "否") { orderItems.Add(new OrderItem(goodname, goodnum, goodprice)); service.AddOrder(cusname, orderItems); break; } } break; case "删除订单": while (true) { Console.WriteLine("请输入订单号:"); int ornum = Int32.Parse(Console.ReadLine()); service.DeleteOrder(ornum); Console.WriteLine("是否继续删除:是/否"); string flag = Console.ReadLine(); if (flag == "是") { continue; } else if (flag == "否") { break; } } break; case "查找订单": while (true) { Console.WriteLine("请输入查找模式:订单号查找/商品名查找/客户名查找"); string mode = Console.ReadLine(); if (mode == "订单号查找") { Console.WriteLine("请输入订单号:"); int num = Int32.Parse(Console.ReadLine()); Console.WriteLine(service.SearchOrderNum(num)); } else if (mode == "商品名查找") { Console.WriteLine("请输入商品名:"); string name = Console.ReadLine(); Console.WriteLine(service.SearchItemName(name)); } else if (mode == "客户名查找") { Console.WriteLine("请输入客户名:"); string name = Console.ReadLine(); Console.WriteLine(service.SearchCustomer(name)); } Console.WriteLine("是否继续查找:是/否"); string flag = Console.ReadLine(); if (flag == "是") { continue; } else if (flag == "否") { break; } } break; case "设置订单": while (true) { Console.WriteLine("请输入订单号:"); int ornum = Int32.Parse(Console.ReadLine()); Console.WriteLine("请输入修改后的订单号"); int ornum1 = Int32.Parse(Console.ReadLine()); service.SetOrderNum(ornum, ornum1); Console.WriteLine("是否继续修改:是/否"); string flag = Console.ReadLine(); if (flag == "是") { continue; } else if (flag == "否") { break; } } break; case "退出系统": break; default: Console.WriteLine("无此功能"); break; } } }