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(); } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。