public void addDetail(orderDetail details) //添加细节 { foreach (orderDetail detailI in detailList) { if (details.Equals(detailI)) //判断细节是否重复 { Console.WriteLine("订单重复!"); return; } if (detailI.productKind == details.productKind) //同样商品进行合并 { detailI.productNum += details.productNum; Console.WriteLine("商品数量添加成功"); return; } } detailList.Add(details); }
public bool Equals(orderDetail other) { //this非空,obj如果为空,则返回false if (ReferenceEquals(null, other)) { return(false); } //如果为同一对象,必然相等 if (ReferenceEquals(this, other)) { return(true); } //对比各个字段值 if (this.orderDetailId == other.orderDetailId) { return(true); } return(true); }
static void Main(string[] args) { orderService orderManage = new orderService(); string menuKey; //功能选择 cilent customer1 = new cilent(1, "Customer1"); cilent customer2 = new cilent(2, "Customer2"); List <cilent> cilentList = new List <cilent>(); cilentList.Add(customer1); cilentList.Add(customer2); //客户列表 product milk = new product(69.9, 1, "Milk"); product eggs = new product(4.99, 2, "eggs"); product apple = new product(5.99, 3, "apple"); orderDetail orderDetails1 = new orderDetail(1, apple, 8); orderDetail orderDetails2 = new orderDetail(2, eggs, 2); orderDetail orderDetails3 = new orderDetail(3, milk, 1); detailList.Add(orderDetails1); detailList.Add(orderDetails2); detailList.Add(orderDetails3); //订单详细列表(简化输入) try { do { Console.WriteLine("请输入功能选择:1.添加订单2.删除订单3.排列订单 4.查询订单 5.价格排序订单 6.输出XML 0.退出");//功能选择 menuKey = Console.ReadLine(); switch (menuKey) { case "1": Console.WriteLine("请输入订单编号"); //新建订单 uint id = uint.Parse(Console.ReadLine()); Console.WriteLine("请输入客户ID(1 or 2)"); int cilentId = int.Parse(Console.ReadLine()); if (cilentId > cilentList.Count) { throw new MyException("cilent does no exsit"); } order editNow = new order(); editNow = orderManage.addOrder(id, cilentList[cilentId - 1]); Console.WriteLine("请输入订单详细ID(1 or 2 or 3)"); //添加订单详细内容 int detailId = int.Parse(Console.ReadLine()); if (detailId > detailList.Count) { throw new MyException("detail does no exsit"); } editNow.addDetail(detailList[detailId - 1]); break; case "2": Console.WriteLine("请输入删除的订单编号"); //删除订单 uint id2 = uint.Parse(Console.ReadLine()); orderManage.deleteOrder(id2); break; case "3": //输出显示订单 orderManage.printOrder(); break; case "4": //查询订单 Console.WriteLine("请输入查询关键字"); orderManage.searchOrder(Console.ReadLine()); break; case "5": //Lambda价格排序 orderManage.priceSort(); break; case "6": orderManage.CreateXmlFile(); break; case "7": orderManage.ReadXml(orderManage.orderList); break; default: break; } } while (menuKey != "0"); } catch (MyException e) { Console.WriteLine(e.Message); } catch (System.FormatException) { Console.WriteLine("格式错误"); } catch (System.IO.IOException) { Console.WriteLine("输入错误"); } finally { Console.ReadLine(); } }