Exemplo n.º 1
0
        private static void HandleOptions()
        {
            OrderService orderService = new OrderService();

            while (true)
            {
                Console.WriteLine("  菜  单");
                Console.WriteLine("1、添加订单");
                Console.WriteLine("2、删除订单");
                Console.WriteLine("3、修改订单");
                Console.WriteLine("4、查询订单");
                Console.WriteLine("5、显示所有订单");
                Console.WriteLine("6、序列化为xml文件");
                Console.WriteLine("7、从xml文件载入订单");
                Console.WriteLine("8、退出系统");
                Console.Write("请选择需要进行的操作:");
                try
                {
                    int           option      = int.Parse(Console.ReadLine());
                    XmlSerializer xmlSer      = new XmlSerializer(typeof(List <Order>));
                    string        xmlFileName = "orders.xml";
                    switch (option)
                    {
                    case 1:
                        orderService.Add();
                        break;

                    case 2:
                        orderService.Remove();
                        break;

                    case 3:
                        orderService.Modify();
                        break;

                    case 4:
                        orderService.Query();
                        break;

                    case 5:
                        orderService.Show();
                        break;

                    case 6:
                        orderService.Export(xmlSer, xmlFileName);
                        break;

                    case 7:
                        orderService.Import(xmlSer, xmlFileName);
                        break;

                    case 8:
                        return;

                    default:
                        Console.WriteLine("请输入1到8之间的数字!任意键继续");
                        Console.ReadKey();
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //测试
            OrderService service = new OrderService();

            //添加订单
            Order myOrder01 = service.AddOrder("20181111001", "Lily", "13245678901");
            Order myOrder02 = service.AddOrder("20181111002", "Mike", "15466666666");

            try
            {
                //给订单添加条目(商品)
                myOrder01.AddProduct("apple");
                myOrder01.AddProduct("sofa");
                myOrder02.AddProduct("banana");

                //修改订单中的内容
                myOrder01.ChangeProduct("apple", "number", 12); //使apple的数量变为12
                myOrder01.ChangeProduct("apple", "price", 5);   //使apple单价变为5
                myOrder01.ChangeProduct("sofa", "number", 3);
                myOrder01.ChangeProduct("sofa", "price", 5000);
                myOrder02.AddProduct("banana");//再增加一个banana
                myOrder02.ChangeProduct("banana", "price", 7);

                //显示订单内容
                myOrder01.ShowOrder();
                myOrder02.ShowOrder();

                //按照客户名字查找订单
                List <Order> myOrder = service.FindOrderByCustomer("Mike");
                foreach (Order order in myOrder)
                {
                    order.ShowOrder();
                }

                //service.AddOrder("01","Lily");

                //按照订单中的商品返回订单
                List <Order> pdtOrder = service.FindOrderByProduct("banana");

                //查找总额大于10000的订单
                List <Order> bigOrder = service.FindOrderOverWan();

                ////序列化订单
                //// OrderService.Export();
                //XmlSerializer xmlser = new XmlSerializer(typeof(List<Order>));
                //string xmlFileName = "orders.xml";
                //OrderService.XmlSerialize(xmlser, xmlFileName, OrderService.orderList);
                //Console.WriteLine("XML序列化订单:");
                ////显示文本
                //string xml = File.ReadAllText(xmlFileName);
                //Console.WriteLine(xml);

                ////反序列化订单
                //List<Order> orderList2 = OrderService.XmlDeserialize(xmlser, xmlFileName)
                //as List<Order>;
                //foreach (Order order in OrderService.orderList)
                //    Console.WriteLine(order);

                //序列化订单
                service.Export();
                //反序列化订单
                service.Import("orders.xml");

                service.ShowExploration();

                //删除订单
                service.DeleteOrder(myOrder01);
                service.DeleteOrder(myOrder02);
            }
            catch (MyException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("操作失败!");
            }
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            try
            {
                Customer customer1 = new Customer(1, "Customer1");
                Customer customer2 = new Customer(2, "Customer2");

                Goods milk  = new Goods(1, "Milk", 69.9);
                Goods eggs  = new Goods(2, "eggs", 4.99);
                Goods apple = new Goods(3, "apple", 5.59);

                OrderDetail orderDetails1 = new OrderDetail(1, apple, 8);
                OrderDetail orderDetails2 = new OrderDetail(2, eggs, 2);
                OrderDetail orderDetails3 = new OrderDetail(3, milk, 1);

                Order order1 = new Order(1, customer1);
                Order order2 = new Order(2, customer2);
                Order order3 = new Order(3, customer2);
                order1.AddDetails(orderDetails1);
                order1.AddDetails(orderDetails2);
                order1.AddDetails(orderDetails3);
                //order1.AddOrderDetails(orderDetails3);
                order2.AddDetails(orderDetails2);
                order2.AddDetails(orderDetails3);
                order3.AddDetails(orderDetails3);

                OrderService os = new OrderService();
                os.AddOrder(order1);
                os.AddOrder(order2);
                os.AddOrder(order3);

                Console.WriteLine("GetAllOrders");
                List <Order> orders = os.QueryAllOrders();
                foreach (Order od in orders)
                {
                    Console.WriteLine(od.ToString());
                }

                Console.WriteLine("Find bigger than 70:");
                os.GetBigger().ForEach(
                    od => Console.WriteLine(od));

                Console.WriteLine("Please input the goods' name you want to search:");
                string str = Console.ReadLine();
                os.QueryByGoodsName(str).ForEach(
                    od => Console.WriteLine(od));

                Console.WriteLine("GetOrdersByCustomerName:'Customer2'");
                orders = os.QueryByCustomerName("Customer2");
                foreach (Order od in orders)
                {
                    Console.WriteLine(od.ToString());
                }

                Console.WriteLine("GetOrdersByGoodsName:'apple'");
                orders = os.QueryByGoodsName("apple");
                foreach (Order od in orders)
                {
                    Console.WriteLine(od.ToString());
                }

                Console.WriteLine("Remove order(id=2) and qurey all");
                os.RemoveOrder(2);
                os.QueryAllOrders().ForEach(
                    od => Console.WriteLine(od));

                os.Export(order1, "E:\\ccc.txt");

                Type  type   = typeof(Order);
                Order order4 = OrderService.Import("E:\\ccc.txt", type) as Order;
                Console.WriteLine("imported order:\n" + order4.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            OrderService orderService = new OrderService();
            int          flag         = 0;

            while (true)
            {
                Console.WriteLine("请选择需要执行的操作(输入数字):");
                Console.WriteLine("1.添加新订单");
                Console.WriteLine("2.删除订单");
                Console.WriteLine("3.查询订单");
                Console.WriteLine("4.修改订单");
                Console.WriteLine("5.查看订单列表");
                Console.WriteLine("6.输出订单");
                Console.WriteLine("7.导入订单");
                Console.WriteLine("8.退出");
                try
                {
                    flag = Int32.Parse(Console.ReadLine());
                    if (flag < 1 || flag > 8)
                    {
                        throw new OrderException();
                    }
                    else
                    {
                        switch (flag)
                        {
                        case 1:
                            orderService.AddOrder();
                            break;

                        case 2:
                            orderService.DeleteOrder();
                            break;

                        case 3:
                            orderService.Search();
                            break;

                        case 4:
                            orderService.EditOrder();
                            break;

                        case 5:
                            orderService.DisplayOrderList();
                            break;

                        case 6:
                            orderService.Export(@"output.xml");
                            break;

                        case 7:
                            orderService.Import(@"output.xml");
                            break;

                        case 8:
                            return;
                        }
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("输入有误!");
                }
            }
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            //订单1
            List <OrderDetail> orderDetails1 = new List <OrderDetail>();
            OrderDetail        od1_1         = new OrderDetail("笔", 1, "北京", 3);

            orderDetails1.Add(od1_1);
            OrderDetail od1_2 = new OrderDetail("纸", 0.5, "上海", 1000);

            orderDetails1.Add(od1_2);
            Order order1 = new Order(1, "张三", "13911111111", orderDetails1);

            //订单2
            List <OrderDetail> orderDetails2 = new List <OrderDetail>();
            OrderDetail        od2_1         = new OrderDetail("书包", 100, "北京", 2);

            orderDetails2.Add(od2_1);
            OrderDetail od2_2 = new OrderDetail("电脑", 10000, "上海", 1);

            orderDetails2.Add(od2_2);
            Order order2 = new Order(2, "张四", "15311111111", orderDetails2);

            //订单3
            List <OrderDetail> orderDetails3 = new List <OrderDetail>();
            OrderDetail        od3_1         = new OrderDetail("书包", 100, "北京", 1);

            orderDetails3.Add(od3_1);
            OrderDetail od3_2 = new OrderDetail("笔记本", 5, "上海", 30);

            orderDetails3.Add(od3_2);
            Order order3 = new Order(3, "张五", "13923222222", orderDetails3);

            List <Order> orders = new List <Order>();

            orders.Add(order1);
            orders.Add(order2);
            orders.Add(order3);

            //订单服务
            OrderService orderService = new OrderService(orders);

            orderService.ShowAllOrders();

            //XML序列化
            string xmlFileName = "s.xml";

            orderService.Export(xmlFileName);
            Console.WriteLine("XML序列化\n");
            //显示XML文本
            string xml = File.ReadAllText(xmlFileName);

            Console.WriteLine(xml);

            Console.WriteLine("XML反序列化\n");
            OrderService os = OrderService.Import(xmlFileName);

            os.ShowAllOrders();

            //对订单的操作
            int n = 1;

            while (n > 0)
            {
                Console.WriteLine("请输入整数表示操作 1.添加订单 2.删除订单(订单号) 3.修改订单 4.查询订单(订单号) 5.查询订单(购买人) 6.查询订单(订单内的商品)7.查找金额大于10000的订单 0退出");
                n = int.Parse(Console.ReadLine());
                Order find = new Order();
                switch (n)
                {
                //1.添加订单
                case 1:
                {
                    string s1, s2, s; int j;
                    Console.WriteLine("请输入一个新的订单");
                    Order newOrder;

                    Console.WriteLine("请输入订单号");
                    s1 = Console.ReadLine();
                    j  = int.Parse(s1);
                    Console.WriteLine("请输入购买人");
                    s2 = Console.ReadLine();
                    Console.WriteLine("请输入购买人电话");
                    s = Console.ReadLine();
                    Console.WriteLine("请输入购买商品的数量");
                    int goodCount = 0;
                    goodCount = int.Parse(Console.ReadLine());
                    List <OrderDetail> ods = new List <OrderDetail>();
                    string             s3, s4;
                    double             d1;
                    for (int i = 0; i < goodCount; i++)
                    {
                        Console.WriteLine("请输入商品名");
                        s3 = Console.ReadLine();
                        Console.WriteLine("请输入价格");
                        d1 = double.Parse(Console.ReadLine());
                        Console.WriteLine("请输入产地");
                        s4 = Console.ReadLine();
                        OrderDetail AOd = new OrderDetail(s3, d1, s4, goodCount);
                        ods.Add(AOd);
                    }
                    newOrder = new Order(j, s2, s, ods);


                    Console.WriteLine("添加第这个订单后");
                    orderService.AddOrder(newOrder);
                    Console.WriteLine();
                    orderService.ShowAllOrders();
                    Console.WriteLine();
                    break;
                }

                //2.删除订单(订单号)
                case 2:
                {
                    string s = "";

                    Console.WriteLine("请输入想删除的订单号的订单");
                    s = Console.ReadLine();

                    try
                    {
                        orderService.DelOrder(orderService.SearchByOrdNum(s));
                        orderService.ShowAllOrders();
                        Console.WriteLine("删除一个订单之后");
                        Console.WriteLine();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    break;
                }

                //3.修改订单(订单名)
                case 3:
                {
                    try
                    {
                        Console.WriteLine("把订单名张三改成李四");
                        orderService.ChangeOrder("张三", "李四");
                        orderService.ShowAllOrders();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine();
                    break;
                }


                //4.查询订单(订单号)
                case 4:
                {
                    Console.WriteLine("请输入想查询的订单号的订单");
                    string sNum = "";
                    sNum = Console.ReadLine();
                    try
                    {
                        find = orderService.SearchByOrdNum(sNum);
                        find.ShowOrder();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine();
                    break;
                }

                //5.查询订单(购买人)
                case 5:
                {
                    Console.WriteLine("请输入想查询的订单号的购买人");
                    string sName = "";
                    sName = Console.ReadLine();
                    try
                    {
                        find = orderService.SearchByBuyerName(sName);
                        find.ShowOrder();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine();
                    break;
                }

                //6.查询订单(商品名)
                case 6:
                {
                    Console.WriteLine("请输入想查询的订单号的商品名");
                    string gName = "";
                    gName = Console.ReadLine();
                    try
                    {
                        find = orderService.SearchByGoodsName(gName);
                        find.ShowOrder();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine();
                    break;
                }

                case 7:
                {
                    try
                    {
                        Console.WriteLine("金额超过10000的订单有");
                        find = orderService.SearchByOrdMonMoreThan10000();
                        find.ShowOrder();
                    }
                    catch (CanNotFindOrder e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    Console.WriteLine();
                    break;
                }

                default:
                    n = -1;
                    break;
                }
            }
            Console.ReadKey();
        }