要求:
订单管理的控制台程序,可以实现添加订单、删除订单、修改订单、查询订单、序列化与反序列化订单功能。
主要的类有Order(订单)、OrderItem(订单明细项),OrderService(订单效劳),订单数据可以保管在OrderService中一个List中。在Program里面可以调用OrderService的方法完成各种订单操作。
要求:
(1)使用LINQ语言实现各种查询功能,查询结果依照订单总金额排序返回。
(2)在订单删除、修改失败时,可以产生异常并显示给客户错误信息。
(3)作业的订单和订单明细类需要重写Equals方法,确保添加的订单不反复,每个订单的订单明细不重 复。
(4)订单、订单明细、客户、货物等类添加ToString方法,用来显示订单信息。
(5)OrderService提供排序方法对保管的订单停止排序。默认依照订单号排序,也可以使用Lambda表达式停止自定义排序。
(6)在OrderService中添加一个Export方法,可以将所有的订单序列化为XML文件;添加一个Import方法可以从XML文件中载入订单。- using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Xml.Serialization;namespace exercise20200320
- {classProgram{staticvoidMain(string[] args){AllOrder a =newAllOrder();bool judge_ =true;while(judge_){
- Console.WriteLine("输入1增加订单,输入2删除订单,输入3查询订单,输入4显示所有订单,输入5根据订单号为订单排序,输入6序列化订单,输入7反序列化订单,输入8退出");string choose1 = Console.ReadLine();switch(choose1){case"1": a.addOrder();break;case"2": a.removeOrder();break;case"3": Console.WriteLine("输入1根据订单金额查询订单,输入2根据客户名查询订单");int i = Convert.ToInt32(Console.ReadLine()); a.searchOrder(i);break;case"4": a.ShowOrder();break;case"5": a.order.Sort();break;case"6": a.export();break;case"7": a.import();break;case"8":judge_ =false;break;default: Console.WriteLine("输入错误");break;}}}}[Serializable]publicclassAllOrder:IOrderService//所有订单{public List<Order> order =newList<Order>();publicAllOrder(){}publicvoidexport(){XmlSerializer a =newXmlSerializer(typeof(List<Order>));using(FileStream b =newFileStream("order.xml", FileMode.Create)){
- a.Serialize(b,this.order);}
- Console.WriteLine("序列化完成");}publicvoidimport(){try{XmlSerializer a =newXmlSerializer(typeof(List<Order>));using(FileStream b =newFileStream("order.xml", FileMode.Open)){
- List<Order> c =(List<Order>)a.Deserialize(b);
- Console.WriteLine("反序列化结果:");foreach(Order d in c){
- Console.WriteLine("订单号 客户 日期 总金额");
- Console.WriteLine("----------------------------");
- Console.WriteLine("{0} {1} {2} {3}", d.Id, d.Customer, d.Date, d.Money);
- d.showOrderItem();}}}catch{
- Console.WriteLine("序列化系列操作错误");}}publicvoidShowOrder(){foreach(Order a inthis.order){
- Console.WriteLine("订单号 客户 日期 总金额");
- Console.WriteLine("----------------------------");
- Console.WriteLine("{0} {1} {2} {3}", a.Id,a.Customer,a.Date,a.Money);
- a.showOrderItem();}}publicvoidaddOrder()//增加订单{try{
- Console.WriteLine("请输入订单编号:");int id = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("请输入客户名称:");string customer = Console.ReadLine();
- Console.WriteLine("请输入时间:");string date = Console.ReadLine();Order a =newOrder(id, customer, date);
- Console.WriteLine("输入订单项:");bool judge =true;bool same =false;foreach(Order m inthis.order){if(m.Equals(a)) same =true;}if(same) Console.WriteLine("订单号反复");else{while(judge &&!same){
- Console.WriteLine("请输入物品名称:");string name = Console.ReadLine();
- Console.WriteLine("请输入购置数量:");int number = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("请输入单价:");double price = Convert.ToDouble(Console.ReadLine());
- a.addOrderItem(name, number, price);
- Console.WriteLine("是否继续添加订单项:");string x = Console.ReadLine();if(x =="否") judge =false;elseif(x=="是")continue;elseif(x!="否"&&x!="是"){Exception e =newException();throw e;}}
- order.Add(a);
- a.getAllPrice();
- Console.WriteLine("建立胜利");
- Console.WriteLine("-------------------------");}}catch{
- Console.WriteLine("输入错误");}}publicvoidremoveOrder()//删除订单{try{
- Console.WriteLine("输入订单号删除订单或相应明细:");int id = Convert.ToInt32(Console.ReadLine());int index =0;foreach(Order a inthis.order){if(a.Id == id) index =this.order.IndexOf(a);}
- Console.WriteLine("输入1删除订单,输入2继续删除订单明细");int choose = Convert.ToInt32(Console.ReadLine());switch(choose){case1:this.order.RemoveAt(index); Console.WriteLine("删除胜利"); Console.WriteLine("-----------------");break;case2:this.order[index].showOrderItem();this.order[index].RemoveOrderItem();break;default: Console.WriteLine("输入错误");break;}}catch{
- Console.WriteLine("输入错误");}}publicvoidsearchOrder(int i)//查询订单{try{switch(i){case1:int minNum, maxNum;
- Console.WriteLine("输入要查询的最小金额:");
- minNum = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("输入要查询的最大金额:");
- maxNum = Convert.ToInt32(Console.ReadLine());var query1 =from s1 in order
- where maxNum > s1.Moneyorderby s1.Moneyselect s1;var query3 =from s3 in query1
- where s3.Money > minNum
- orderby s3.Moneyselect s3;
- List<Order> a1 = query3.ToList();foreach(Order b1 in a1){
- Console.WriteLine("订单号 客户 日期 总金额");
- Console.WriteLine("----------------------------");
- Console.WriteLine("{0} {1} {2} {3}", b1.Id, b1.Customer, b1.Date, b1.Money);
- b1.showOrderItem();}break;case2:
- Console.WriteLine("输入客户名称:");string name1 = Console.ReadLine();var query2 =from s2 in order
- where s2.Customer == name1
- orderby s2.Moneyselect s2;
- List<Order> a2 = query2.ToList();foreach(Order b2 in a2){
- Console.WriteLine("订单号 客户 日期 总金额");
- Console.WriteLine("----------------------------");
- Console.WriteLine("{0} {1} {2} {3}", b2.Id, b2.Customer, b2.Date, b2.Money);
- b2.showOrderItem();}break;default: Console.WriteLine("输入错误");break;}}catch{
- Console.WriteLine("输入错误");}}}[Serializable]publicclassOrder:IComparable//单个订单项{publicint Id {get;set;}publicstring Customer {get;set;}publicdouble Money {get;set;}publicstring Date {get;set;}public List<OrderItem> orderItem =newList<OrderItem>();publicOrder()//无参构造函数{this.Id =0;this.Customer =string.Empty;this.Money =0;this.Date =string.Empty;}publicintCompareTo(object obj){Order a = obj as Order;returnthis.Id.CompareTo(a.Id);}publicoverrideboolEquals(object obj){Order a = obj as Order;returnthis.Id == a.Id;}publicoverrideintGetHashCode(){return Convert.ToInt32(Id);}publicOrder(int id,string customer,string date){this.Id = id;this.Customer = customer;this.Date = date;}publicvoidgetAllPrice()//计算总价{double i=0;foreach(OrderItem a inthis.orderItem){
- i = i + a.getPrice();}this.Money = i;}publicvoidaddOrderItem(string name,int number,double price)//添加订单项{OrderItem a =newOrderItem(name, number, price);this.orderItem.Add(a);}publicvoidRemoveOrderItem()//删除订单项{try{
- Console.WriteLine("请输入订单明细序号删除相应订单明细:");int a = Convert.ToInt32(Console.ReadLine());this.orderItem.RemoveAt(a);
- Console.WriteLine("删除胜利");
- Console.WriteLine("-------------------------");}catch{
- Console.WriteLine("输入序号错误");}}publicvoidshowOrderItem()//展示订单项{
- Console.WriteLine("序号 名称 数量 单价");foreach(OrderItem a inthis.orderItem){
-
- Console.WriteLine("-----------------------");
- Console.WriteLine("{0} {1} {2} {3}",this.orderItem.IndexOf(a),a.Name,a.Number,a.Price);}}}[Serializable]publicclassOrderItem//订单明细项{privatestring name;publicstring Name
- {get{return name;}set{
- name =value;}}privateint number;publicint Number
- {get{return number;}set{if(value>=0) number =value;else Console.WriteLine("数量不应该小于0");}}privatedouble price;publicdouble Price
- {get{return price;}set{
- price =value;}}publicOrderItem()//无参构造函数{this.Name =string.Empty;this.Number =0;this.Price =0;}publicOrderItem(string name,int number,double price){this.name = name;this.number = number;this.price = price;}publicdoublegetPrice(){returnthis.number *this.price;}}publicinterfaceIOrderService//包含所有订单功能的接口{voidaddOrder();voidremoveOrder();voidsearchOrder(int i);voidexport();voidimport();}}
复制代码 借鉴请标明出处
才疏学浅,有错误请多指教 |