Пример #1
0
        private void process(String line)
        {
            String[] cmds = Regex.Split(line, "\\s+");
            if (cmds.Length == 0)
            {
                return;
            }

            String command = cmds[0];

            try
            {
                if (string.Compare("begin", command, true) == 0)
                {
                    if (tx != null)
                    {
                        throw new System.InvalidOperationException("transaction " + tx + " is active");
                    }
                    tx = myWC.Start();
                }
                else if (string.Compare("commit", command, true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no transaction is active");
                    }
                    myWC.Commit(tx);
                    tx = null;
                }
                else if (string.Compare("abort", command, true) == 0)
                {
                    if (tx != null)
                    {
                        myWC.Abort(tx);
                    }
                    tx = null;
                }
                else if (string.Compare("cancel", command, true) == 0)
                {
                    Customer c = cmds.Length == 2 ? new Customer(cmds[1]) : customer;
                    if (myWC.CancelItinerary(c))
                    {
                        Console.WriteLine("Itinierary for customer " + c + " has canceled");
                    }
                    else
                    {
                        Console.WriteLine("Failed to cancel itinerary for customer " + c);
                    }
                }
                else if (string.Compare("new", command, true) == 0)
                {
                    if (customer != null)
                    {
                        Console.WriteLine("Old customer = " + customer);
                    }
                    if (cmds.Length == 2)
                    {
                        customer = new Customer(cmds[1]);
                    }
                    else
                    {
                        customer = new Customer();
                    }
                    Console.WriteLine("current customer = " + customer);
                }
                else if (string.Compare("print", command, true) == 0)
                {
                    if (tx != null)
                    {
                        Console.WriteLine("Current transaction = " + tx);
                    }
                    if (customer != null)
                    {
                        Console.WriteLine("Current customer = " + customer);
                    }
                }
                else if (string.Compare("add", command, true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no active transaction");
                    }
                    String target = cmds[1];
                    String loc    = cmds[2];
                    int    num    = Int32.Parse(cmds[3]);
                    int    price  = Int32.Parse(cmds[4]);
                    if (string.Compare("car", target, true) == 0)
                    {
                        myWC.AddCars(tx, loc, num, price);
                    }
                    else if (string.Compare("seat", target, true) == 0)
                    {
                        myWC.AddSeats(tx, loc, num, price);
                    }
                    else if (string.Compare("room", target, true) == 0)
                    {
                        myWC.AddRooms(tx, loc, num, price);
                    }
                    else
                    {
                        throw new System.InvalidOperationException("usage: add (seat|car|room) loc qty price");
                    }
                }
                else if (string.Compare("del", command, true) == 0 || string.Compare("delete", command, true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no active transaction");
                    }
                    String target = cmds[1];
                    String loc    = cmds[2];
                    if (string.Compare("flight", target, true) == 0)
                    {
                        myWC.DeleteFlight(tx, loc);
                    }
                    else
                    {
                        int num = Int32.Parse(cmds[3]);
                        if (string.Compare("car", target, true) == 0)
                        {
                            myWC.DeleteCars(tx, loc, num);
                        }
                        else if (string.Compare("seat", target, true) == 0)
                        {
                            myWC.DeleteSeats(tx, loc, num);
                        }
                        else if (string.Compare("room", target, true) == 0)
                        {
                            myWC.DeleteRooms(tx, loc, num);
                        }
                        else
                        {
                            throw new System.InvalidOperationException("usage: " + command + " (seat|car|room) qty | flight");
                        }
                    }
                }
                else if (string.Compare("query", command, true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no active transaction");
                    }
                    String target = cmds[1];
                    if (string.Compare("itinerary", (target), true) == 0 || string.Compare("i", (target), true) == 0)
                    {
                        Customer c = customer;
                        if (cmds.Length == 3)
                        {
                            c = new Customer(cmds[2]);
                        }
                        else if (c == null)
                        {
                            throw new System.InvalidOperationException("usage: query (itinerary|i) [customer]");
                        }
                        String result = myWC.QueryItinerary(tx, c);
                        Console.WriteLine("Itinerary for customer " + c);
                        Console.WriteLine(result);
                    }
                    else
                    {
                        String loc   = cmds[2];
                        int    avail = 0;
                        if (string.Compare("car", (target), true) == 0 || string.Compare("c", (target), true) == 0)
                        {
                            avail = myWC.QueryCar(tx, loc);
                        }
                        else if (string.Compare("flight", (target), true) == 0 || string.Compare("f", (target), true) == 0)
                        {
                            avail = myWC.QueryFlight(tx, loc);
                        }
                        else if (string.Compare("room", (target), true) == 0 || string.Compare("r", (target), true) == 0)
                        {
                            avail = myWC.QueryRoom(tx, loc);
                        }
                        else
                        {
                            throw new System.InvalidOperationException("usage: query (car|flight|room) loc");
                        }
                        Console.WriteLine(avail + " " + target + " are available at " + loc);
                    }
                }
                else if (string.Compare("price", (command), true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no active transaction");
                    }
                    String target = cmds[1];

                    if (string.Compare("itinerary", (target), true) == 0 || string.Compare("i", (target), true) == 0)
                    {
                        Customer c = customer;
                        if (cmds.Length == 3)
                        {
                            c = new Customer(cmds[2]);
                        }
                        else if (c == null)
                        {
                            throw new System.InvalidOperationException("usage: price (itinerary|i) [customer]");
                        }
                        int total = myWC.QueryItineraryPrice(tx, c);
                        Console.WriteLine("Total price of itinerary for customer " + c + " = " + total);
                    }
                    else
                    {
                        String loc   = cmds[2];
                        int    avail = 0;
                        if (string.Compare("car", (target), true) == 0 || string.Compare("c", (target), true) == 0)
                        {
                            avail = myWC.QueryCarPrice(tx, loc);
                        }
                        else if (string.Compare("flight", (target), true) == 0 || string.Compare("f", (target), true) == 0)
                        {
                            avail = myWC.QueryFlightPrice(tx, loc);
                        }
                        else if (string.Compare("room", (target), true) == 0 || string.Compare("r", (target), true) == 0)
                        {
                            avail = myWC.QueryRoomPrice(tx, loc);
                        }
                        else
                        {
                            throw new System.InvalidOperationException("usage: price (car|flight|room) loc");
                        }
                        Console.WriteLine("price to reserve " + target + " at " + loc + " = " + avail);
                    }
                }
                else if (string.Compare("list", (command), true) == 0)
                {
                    if (tx == null)
                    {
                        throw new System.InvalidOperationException("no active transaction");
                    }
                    String target = cmds[1];
                    if (string.Compare("customer", (target), true) == 0)
                    {
                        Customer[] customers = myWC.ListCustomers(tx);
                        Console.WriteLine(customers.Length + " customers exist");
                        foreach (Customer c in customers)
                        {
                            Console.WriteLine(c);
                        }
                    }
                    else
                    {
                        String[] result = new String[0];
                        if (string.Compare("car", (target), true) == 0)
                        {
                            result = myWC.ListCars(tx);
                        }
                        else if (string.Compare("flight", (target), true) == 0)
                        {
                            result = myWC.ListFlights(tx);
                        }
                        else if (string.Compare("room", (target), true) == 0)
                        {
                            result = myWC.ListRooms(tx);
                        }
                        else
                        {
                            throw new System.InvalidOperationException("usage: list (car|flight|room|customer)");
                        }

                        Console.WriteLine(result.Length + " items exist");
                        foreach (String e in result)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }
                else if (string.Compare("reserve", (command), true) == 0)
                {
                    if (cmds.Length < 5)
                    {
                        throw new System.InvalidOperationException("usage: reserve flight1 ... flightN location bookCar bookRoom");
                    }
                    if (customer == null)
                    {
                        throw new System.InvalidOperationException("no customer was set.");
                    }

                    String[] flights = new String[cmds.Length - 3 - 1];
                    Array.Copy(cmds, 1, flights, 0, cmds.Length - 3 - 1);
                    String loc   = cmds[cmds.Length - 3];
                    bool   bcar  = parseBoolean(cmds[cmds.Length - 2]);
                    bool   broom = parseBoolean(cmds[cmds.Length - 1]);

                    if (myWC.ReserveItinerary(customer, flights, loc, bcar, broom))
                    {
                        Console.WriteLine("reserved the itinerary for customer " + customer);
                    }
                    else
                    {
                        Console.WriteLine("Failed to reserve the itinerary for customer " + customer);
                    }
                }
                else if (string.Compare("help", command, true) == 0 || command.CompareTo("?") == 0)
                {
                    PrintHelp();
                }
                else if (string.Compare("exit", (command), true) == 0 || string.Compare("quit", (command), true) == 0)
                {
                    try
                    {
                        if (tx != null)
                        {
                            myWC.Abort(tx);
                        }
                    }
                    catch (Exception) { }
                }
                else
                {
                    throw new System.InvalidOperationException("unknown command: " + command);
                }
            }
            catch (InvalidOperationException x)
            {
                Console.WriteLine(x.ToString());
            }
            catch (Exception x)
            {
                Console.WriteLine(x.ToString());
            }
        }