C#如何实现简单订单管理程序
这篇文章主要介绍“C#如何实现简单订单管理程序”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“C#如何实现简单订单管理程序”文章能帮助大家解决问题。
订单管理的控制台程序,能够实现添加订单、删除订单、修改订单、查询订单、序列化与反序列化订单功能。
主要的类有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{ class Program { static void Main(string[] args) { AllOrder a = new AllOrder(); 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] public class AllOrder:IOrderService //所有订单 { public List<Order> order = new List<Order>(); public AllOrder() { } public void export() { XmlSerializer a = new XmlSerializer(typeof(List<Order>)); using (FileStream b = new FileStream("order.xml", FileMode.Create)) { a.Serialize(b, this.order); } Console.WriteLine("序列化完成"); } public void import() { try { XmlSerializer a = new XmlSerializer(typeof(List<Order>)); using (FileStream b = new FileStream("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("序列化系列操作错误"); } } public void ShowOrder() { foreach (Order a in this.order) { Console.WriteLine("订单号 客户 日期 总金额"); Console.WriteLine("----------------------------"); Console.WriteLine("{0} {1} {2} {3}", a.Id,a.Customer,a.Date,a.Money); a.showOrderItem(); } } public void addOrder() //增加订单 { try { Console.WriteLine("请输入订单编号:"); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入客户名称:"); string customer = Console.ReadLine(); Console.WriteLine("请输入时间:"); string date = Console.ReadLine(); Order a = new Order(id, customer, date); Console.WriteLine("输入订单项:"); bool judge = true; bool same = false; foreach(Order m in this.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; else if(x=="是") continue; else if(x!="否"&&x!="是"){ Exception e = new Exception(); throw e; } } order.Add(a); a.getAllPrice(); Console.WriteLine("建立成功"); Console.WriteLine("-------------------------"); } } catch { Console.WriteLine("输入错误"); } } public void removeOrder() //删除订单 { try { Console.WriteLine("输入订单号删除订单或相应明细:"); int id = Convert.ToInt32(Console.ReadLine()); int index = 0; foreach (Order a in this.order) { if (a.Id == id) index = this.order.IndexOf(a); } Console.WriteLine("输入1删除订单,输入2继续删除订单明细"); int choose = Convert.ToInt32(Console.ReadLine()); switch (choose) { case 1: this.order.RemoveAt(index); Console.WriteLine("删除成功"); Console.WriteLine("-----------------"); break; case 2: this.order[index].showOrderItem(); this.order[index].RemoveOrderItem(); break; default: Console.WriteLine("输入错误"); break; } } catch { Console.WriteLine("输入错误"); } } public void searchOrder(int i) //查询订单 { try { switch (i) { case 1: 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.Money orderby s1.Money select s1; var query3 = from s3 in query1 where s3.Money > minNum orderby s3.Money select 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; case 2: Console.WriteLine("输入客户名称:"); string name1 = Console.ReadLine(); var query2 = from s2 in order where s2.Customer == name1 orderby s2.Money select 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] public class Order:IComparable //单个订单项 { public int Id { get; set; } public string Customer { get; set; } public double Money { get; set; } public string Date { get; set; } public List<OrderItem> orderItem = new List<OrderItem>(); public Order()//无参构造函数 { this.Id = 0; this.Customer = string.Empty; this.Money = 0; this.Date = string.Empty; } public int CompareTo(object obj) { Order a = obj as Order; return this.Id.CompareTo(a.Id); } public override bool Equals(object obj) { Order a = obj as Order; return this.Id == a.Id; } public override int GetHashCode() { return Convert.ToInt32(Id); } public Order(int id,string customer,string date) { this.Id = id; this.Customer = customer; this.Date = date; } public void getAllPrice() //计算总价 { double i=0; foreach(OrderItem a in this.orderItem) { i = i + a.getPrice(); } this.Money = i; } public void addOrderItem(string name,int number,double price) //添加订单项 { OrderItem a = new OrderItem(name, number, price); this.orderItem.Add(a); } public void RemoveOrderItem() //删除订单项 { try { Console.WriteLine("请输入订单明细序号删除相应订单明细:"); int a = Convert.ToInt32(Console.ReadLine()); this.orderItem.RemoveAt(a); Console.WriteLine("删除成功"); Console.WriteLine("-------------------------"); } catch { Console.WriteLine("输入序号错误"); } } public void showOrderItem() //展示订单项 { Console.WriteLine("序号 名称 数量 单价"); foreach (OrderItem a in this.orderItem) { Console.WriteLine("-----------------------"); Console.WriteLine("{0} {1} {2} {3}",this.orderItem.IndexOf(a),a.Name,a.Number,a.Price); } } } [Serializable] public class OrderItem //订单明细项 { private string name; public string Name { get { return name; } set { name = value; } } private int number; public int Number { get { return number; } set { if (value >= 0) number = value; else Console.WriteLine("数量不应该小于0"); } } private double price; public double Price { get { return price; } set { price = value; } } public OrderItem()//无参构造函数 { this.Name = string.Empty; this.Number = 0; this.Price = 0; } public OrderItem(string name, int number, double price) { this.name = name; this.number = number; this.price = price; } public double getPrice() { return this.number * this.price; } } public interface IOrderService //包含所有订单功能的接口 { void addOrder(); void removeOrder(); void searchOrder(int i); void export(); void import(); }}
关于“C#如何实现简单订单管理程序”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341