C#實(shí)現(xiàn)簡單訂單管理程序
本文實(shí)例為大家分享了C#實(shí)現(xiàn)訂單管理程序的具體代碼,供大家參考,具體內(nèi)容如下
訂單管理的控制臺程序,能夠?qū)崿F(xiàn)添加訂單、刪除訂單、修改訂單、查詢訂單、序列化與反序列化訂單功能。
主要的類有Order(訂單)、OrderItem(訂單明細(xì)項(xiàng)),OrderService(訂單服務(wù)),訂單數(shù)據(jù)可以保存在OrderService中一個List中。在Program里面可以調(diào)用OrderService的方法完成各種訂單操作。
要求:
(1)使用LINQ語言實(shí)現(xiàn)各種查詢功能,查詢結(jié)果按照訂單總金額排序返回。
(2)在訂單刪除、修改失敗時,能夠產(chǎn)生異常并顯示給客戶錯誤信息。
(3)作業(yè)的訂單和訂單明細(xì)類需要重寫Equals方法,確保添加的訂單不重復(fù),每個訂單的訂單明細(xì)不重 復(fù)。
(4)訂單、訂單明細(xì)、客戶、貨物等類添加ToString方法,用來顯示訂單信息。
(5)OrderService提供排序方法對保存的訂單進(jìn)行排序。默認(rèn)按照訂單號排序,也可以使用Lambda表達(dá)式進(jìn)行自定義排序。
(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根據(jù)訂單號為訂單排序,輸入6序列化訂單,輸入7反序列化訂單,輸入8退出"); ? ? ? ? ? ? ? ? string choose1 = Console.ReadLine(); ? ? ? ? ? ? ? ? switch (choose1) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? case "1": a.addOrder(); break; ? ? ? ? ? ? ? ? ? ? case "2": a.removeOrder(); break; ? ? ? ? ? ? ? ? ? ? case "3": Console.WriteLine("輸入1根據(jù)訂單金額查詢訂單,輸入2根據(jù)客戶名查詢訂單"); 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("反序列化結(jié)果:"); ? ? ? ? ? ? ? ? ? ? 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("輸入訂單項(xiàng):"); ? ? ? ? ? ? ? ? bool judge = true; ? ? ? ? ? ? ? ? bool same = false; ? ? ? ? ? ? ? ? foreach(Order m in this.order) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? if (m.Equals(a)) same = true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (same) Console.WriteLine("訂單號重復(fù)"); ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? while (judge && !same) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("請輸入物品名稱:"); ? ? ? ? ? ? ? ? ? ? ? ? string name = Console.ReadLine(); ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("請輸入購買數(shù)量:"); ? ? ? ? ? ? ? ? ? ? ? ? int number = Convert.ToInt32(Console.ReadLine()); ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("請輸入單價:"); ? ? ? ? ? ? ? ? ? ? ? ? double price = Convert.ToDouble(Console.ReadLine()); ? ? ? ? ? ? ? ? ? ? ? ? a.addOrderItem(name, number, price); ? ? ? ? ? ? ? ? ? ? ? ? Console.WriteLine("是否繼續(xù)添加訂單項(xiàng):"); ? ? ? ? ? ? ? ? ? ? ? ? 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("輸入訂單號刪除訂單或相應(yīng)明細(xì):"); ? ? ? ? ? ? ? ? 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繼續(xù)刪除訂單明細(xì)"); ? ? ? ? ? ? ? ? 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 ?//單個訂單項(xiàng) ? ? { ? ? ? ? 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()//無參構(gòu)造函數(shù) ? ? ? ? { ? ? ? ? ? ? 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) ? //添加訂單項(xiàng) ? ? ? ? {? ? ? ? ? ? ? OrderItem a = new OrderItem(name, number, price); ? ? ? ? ? ? this.orderItem.Add(a); ? ? ? ? } ? ? ? ? public void RemoveOrderItem() //刪除訂單項(xiàng) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("請輸入訂單明細(xì)序號刪除相應(yīng)訂單明細(xì):"); ? ? ? ? ? ? ? ? int a = Convert.ToInt32(Console.ReadLine()); ? ? ? ? ? ? ? ? this.orderItem.RemoveAt(a); ? ? ? ? ? ? ? ? Console.WriteLine("刪除成功"); ? ? ? ? ? ? ? ? Console.WriteLine("-------------------------"); ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? ? ? Console.WriteLine("輸入序號錯誤"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public void showOrderItem() ?//展示訂單項(xiàng) ? ? ? ? { ? ? ? ? ? ? Console.WriteLine("序號 名稱 數(shù)量 單價"); ? ? ? ? ? ? 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 ? ? ? ? ? ? ? //訂單明細(xì)項(xiàng) ? ? { ? ? ? ? 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("數(shù)量不應(yīng)該小于0"); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? private double price; ? ? ? ? public double Price ? ? ? ? { ? ? ? ? ? ? get ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return price; ? ? ? ? ? ? } ? ? ? ? ? ? set ? ? ? ? ? ? { ? ? ? ? ? ? ? ? price = value; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public OrderItem()//無參構(gòu)造函數(shù) ? ? ? ? { ? ? ? ? ? ? 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(); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#采用HttpWebRequest實(shí)現(xiàn)保持會話上傳文件到HTTP的方法
這篇文章主要介紹了C#采用HttpWebRequest實(shí)現(xiàn)保持會話上傳文件到HTTP的方法,很實(shí)用的功能,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)運(yùn)行狀態(tài)堆疊柱狀圖
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)運(yùn)行狀態(tài)堆疊柱狀圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02C#中LINQ的Select與SelectMany函數(shù)使用
這篇文章主要介紹了C#中LINQ的Select與SelectMany函數(shù)使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08C#實(shí)現(xiàn)創(chuàng)建,刪除,查找,配置虛擬目錄實(shí)例詳解
這篇文章主要介紹了C#創(chuàng)建,刪除,查找,配置虛擬目錄的方法,以實(shí)例形式較為詳細(xì)的分析了C#針對虛擬目錄的創(chuàng)建、刪除、查找等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C# 創(chuàng)建MDB數(shù)據(jù)庫、并存放表格數(shù)據(jù)的案例
這篇文章主要介紹了C# 創(chuàng)建MDB數(shù)據(jù)庫、并存放表格數(shù)據(jù)的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01