Exemplo n.º 1
0
        //主操作函数
        static public void mainService(OrderService myOrder)
        {
            Console.WriteLine("选择您要进行的服务: 1.添加订单; 2.查询订单;" +
                              "3.打印全部订单; 4.打印金额大于10000的订单; 5.输出xml; 6.退出");
            int whichFunction = 0;

            while (whichFunction == 0)
            {
                try
                {
                    whichFunction = int.Parse(Console.ReadLine());
                    if (whichFunction < 1 || whichFunction > 6)
                    {
                        throw new System.FormatException();
                    }
                }
                catch (System.FormatException e)
                {
                    whichFunction = 0;
                    Console.Write(e.Message);
                    Console.WriteLine("请重新输入");
                }
            }
            switch (whichFunction)
            {
            case 1:
                myOrder.NewOrder();
                break;

            case 2:
                SearchOrder(myOrder);
                break;

            case 3:
                try
                {
                    myOrder.WriteResult(myOrder.ListOfOrder);
                }
                catch (NoSearchResult e)
                {
                    Console.WriteLine(e.NoResult);
                }
                break;

            case 4:
                List <Order> Morethanres = myOrder.SearchMoreThan(myOrder.ListOfOrder, 10000);
                myOrder.WriteResult(Morethanres);
                break;

            case 5:
                myOrder.Export(myOrder.ListOfOrder, @"AllOrder.txt");
                break;

            case 6:
                return;

            default:
                break;
            }
            mainService(myOrder);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                OrderItem apple = new OrderItem(1, "apple", 10.0, 80);
                OrderItem egg   = new OrderItem(2, "eggs", 1.2, 200);
                OrderItem milk  = new OrderItem(3, "milk", 50, 10);

                Order order1 = new Order(1, "Customer1", new List <OrderItem> {
                    apple, egg, milk
                });
                Order order2 = new Order(2, "Customer2", new List <OrderItem> {
                    egg, milk
                });
                Order order3 = new Order(3, "Customer2", new List <OrderItem> {
                    apple, milk
                });

                OrderService os = new OrderService();
                os.AddOrder(order1);
                os.AddOrder(order2);
                os.AddOrder(order3);
                os.Export(@"./orders.xml");

                Console.WriteLine("GetAllOrders");
                List <Order> orders = os.orders;
                orders.ForEach(o => Console.WriteLine(o));
                Console.WriteLine("");

                os.Sort((o1, o2) => o1.TotalPrice.CompareTo(o2.TotalPrice));
                Console.WriteLine("GetAllOrders After sort");
                orders = os.orders;
                orders.ForEach(o => Console.WriteLine(o));
                Console.WriteLine("");

                Console.WriteLine("GetOrdersByCustomerName:'Customer2'");
                orders = os.QuaryByCustomer("Customer2");
                orders.ForEach(o => Console.WriteLine(o));
                Console.WriteLine("");

                Console.WriteLine("GetOrdersByGoodsName:'apple'");
                orders = os.QuaryByName("apple");
                orders.ForEach(o => Console.WriteLine(o));
                Console.WriteLine("");

                Console.WriteLine("Remove order(id=2) and qurey all");
                os.ReMove(2);
                orders.ForEach(o => Console.WriteLine(o));
                Console.WriteLine("");

                Console.WriteLine("Import from ./orders.xml");
                OrderService os2 = new OrderService();
                os2.Import("./orders.xml");
                os2.orders.ForEach(order => Console.WriteLine(order));
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadKey();
            }
        }