溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)簡(jiǎn)單訂單管理程序

發(fā)布時(shí)間:2022-05-30 10:46:10 來(lái)源:億速云 閱讀:146 作者:zzz 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“C#如何實(shí)現(xiàn)簡(jiǎn)單訂單管理程序”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“C#如何實(shí)現(xiàn)簡(jiǎn)單訂單管理程序”文章能幫助大家解決問(wèn)題。

訂單管理的控制臺(tái)程序,能夠?qū)崿F(xiàn)添加訂單、刪除訂單、修改訂單、查詢訂單、序列化與反序列化訂單功能。

主要的類有Order(訂單)、OrderItem(訂單明細(xì)項(xiàng)),OrderService(訂單服務(wù)),訂單數(shù)據(jù)可以保存在OrderService中一個(gè)List中。在Program里面可以調(diào)用OrderService的方法完成各種訂單操作。

要求:

(1)使用LINQ語(yǔ)言實(shí)現(xiàn)各種查詢功能,查詢結(jié)果按照訂單總金額排序返回。
(2)在訂單刪除、修改失敗時(shí),能夠產(chǎn)生異常并顯示給客戶錯(cuò)誤信息。
(3)作業(yè)的訂單和訂單明細(xì)類需要重寫(xiě)Equals方法,確保添加的訂單不重復(fù),每個(gè)訂單的訂單明細(xì)不重 復(fù)。
(4)訂單、訂單明細(xì)、客戶、貨物等類添加ToString方法,用來(lái)顯示訂單信息。
(5)OrderService提供排序方法對(duì)保存的訂單進(jìn)行排序。默認(rèn)按照訂單號(hào)排序,也可以使用Lambda表達(dá)式進(jìn)行自定義排序。
(6)在OrderService中添加一個(gè)Export方法,可以將所有的訂單序列化為XML文件;添加一個(gè)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ù)訂單號(hào)為訂單排序,輸入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("輸入錯(cuò)誤"); 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("訂單號(hào) 客戶 日期 總金額");
                        Console.WriteLine("----------------------------");
                        Console.WriteLine("{0} {1} {2} {3}", d.Id, d.Customer, d.Date, d.Money);
                        d.showOrderItem();
                    }
                }
            }
            catch
            {
                Console.WriteLine("序列化系列操作錯(cuò)誤");
            }
        }
        public void ShowOrder()
        {
            
            foreach (Order a in this.order) {
                Console.WriteLine("訂單號(hào) 客戶 日期 總金額");
                Console.WriteLine("----------------------------");
                Console.WriteLine("{0} {1} {2} {3}", a.Id,a.Customer,a.Date,a.Money);
                a.showOrderItem();
            }
        }
        public void addOrder()          //增加訂單
        {
            try
            {
                Console.WriteLine("請(qǐng)輸入訂單編號(hào):");
                int id = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("請(qǐng)輸入客戶名稱:");
                string customer = Console.ReadLine();
                Console.WriteLine("請(qǐng)輸入時(shí)間:");
                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("訂單號(hào)重復(fù)");
                else
                {
                    while (judge && !same)
                    {
                        Console.WriteLine("請(qǐng)輸入物品名稱:");
                        string name = Console.ReadLine();
                        Console.WriteLine("請(qǐng)輸入購(gòu)買(mǎi)數(shù)量:");
                        int number = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("請(qǐng)輸入單價(jià):");
                        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("輸入錯(cuò)誤");
            }

        }
        public void removeOrder()           //刪除訂單
        {
            try
            {
                Console.WriteLine("輸入訂單號(hào)刪除訂單或相應(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("輸入錯(cuò)誤"); break;
                }
            }
            catch
            {
                Console.WriteLine("輸入錯(cuò)誤");
            }
            
        }

        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("訂單號(hào) 客戶 日期 總金額");
                            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("訂單號(hào) 客戶 日期 總金額");
                            Console.WriteLine("----------------------------");
                            Console.WriteLine("{0} {1} {2} {3}", b2.Id, b2.Customer, b2.Date, b2.Money);
                            b2.showOrderItem();
                        }
                        break;
                    default: Console.WriteLine("輸入錯(cuò)誤"); break;

                }
            }
            catch
            {
                Console.WriteLine("輸入錯(cuò)誤");
            }
        }

        
    }
    [Serializable]
    public class Order:IComparable  //單個(gè)訂單項(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()//無(wú)參構(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()  //計(jì)算總價(jià)
        {
            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("請(qǐng)輸入訂單明細(xì)序號(hào)刪除相應(yīng)訂單明細(xì):");
                int a = Convert.ToInt32(Console.ReadLine());
                this.orderItem.RemoveAt(a);
                Console.WriteLine("刪除成功");
                Console.WriteLine("-------------------------");
            }
            catch
            {
                Console.WriteLine("輸入序號(hào)錯(cuò)誤");
            }
        }

        public void showOrderItem()  //展示訂單項(xiàng)
        {
            Console.WriteLine("序號(hào) 名稱 數(shù)量 單價(jià)");
            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()//無(wú)參構(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();

    }
}

關(guān)于“C#如何實(shí)現(xiàn)簡(jiǎn)單訂單管理程序”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI