コード例 #1
0
        private void createBody(string subsectionType, int bookId, int custId, ModelStore store)
        {
            var name = store.GetCustomer(custId).FirstName;

            writer.WriteLine("<body>");

            writer.WriteLine(ind(1) + "<style type=\"text/css\">");
            writer.WriteLine(ind(2) + "table, th, td {");
            writer.WriteLine(ind(3) + "border: 1px solid black;");
            writer.WriteLine(ind(3) + "border-collapse: collapse;");
            writer.WriteLine(ind(2) + "}");
            writer.WriteLine(ind(2) + "table {");
            writer.WriteLine(ind(3) + "margin-bottom: 10px;");
            writer.WriteLine(ind(2) + "}");
            writer.WriteLine(ind(2) + "pre {");
            writer.WriteLine(ind(3) + "line-height: 70%;");
            writer.WriteLine(ind(2) + "}");
            writer.WriteLine(ind(1) + "</style>");

            writer.WriteLine(ind(1) + "<h1><pre>  v,<br />Nezarka.NET: Online Shopping for Books</pre></h1>");

            writer.WriteLine(ind(1) + "{0}, here is your menu:", name);
            writer.WriteLine(ind(1) + "<table>");
            writer.WriteLine(ind(2) + "<tr>");
            writer.WriteLine(ind(3) + "<td><a href=\"/Books\">Books</a></td>");
            writer.WriteLine(ind(3) + "<td><a href=\"/ShoppingCart\">Cart ({0})</a></td>", store.GetCustomer(custId).ShoppingCart.Items.Count);
            writer.WriteLine(ind(2) + "</tr>");
            writer.WriteLine(ind(1) + "</table>");

            createSubsection(subsectionType, bookId, custId, store);

            writer.WriteLine("</body>");
        }
コード例 #2
0
        void createSubsection(string subsectionType, int bookId, int custId, ModelStore store)
        {
            switch (subsectionType)
            {
            case "Books":
                showBooks(store.GetBooks());
                break;

            case "Detail":
                showDetail(store.GetBook(bookId));
                break;

            case "Add":
            case "Remove":
            case "ShoppingCart":
                showShoppingCart(store.GetCustomer(custId).ShoppingCart, store);
                break;
            }
        }
コード例 #3
0
        public void parse(string request)
        {
            string[] get = request.Split(' ');
            if (!validateGet(get))
            {
                htmlBuilder.createDump();
                return;
            }
            string[] url = get[2].Split('/');
            if (!validateUrl(url))
            {
                htmlBuilder.createDump();
                return;
            }
            string subsectionType = "";
            string custId         = get[1];
            int    bookIdInt      = 0;
            int    custIdInt      = 0;

            subsectionType = getSubsectionType(url, out string bookId);
            if (!validateSubsectionType(subsectionType, url))
            {
                htmlBuilder.createDump();
                return;
            }

            if (!(int.TryParse(bookId, out bookIdInt) && int.TryParse(custId, out custIdInt)))
            {
                htmlBuilder.createDump();
                return;
            }
            Customer customer = store.GetCustomer(custIdInt);
            Book     book     = store.GetBook(bookIdInt);

            if (customer == null || (book == null && bookIdInt != -5769765))
            {
                htmlBuilder.createDump();
                return;
            }

            switch (subsectionType)
            {
            case "Add":
                if (!addItemToCart(store, bookIdInt, store.GetCustomer(custIdInt).ShoppingCart))
                {
                    htmlBuilder.createDump();
                    return;
                }
                break;

            case "Remove":
                if (!removeItemFromCart(store, bookIdInt, store.GetCustomer(custIdInt).ShoppingCart))
                {
                    htmlBuilder.createDump();
                    return;
                }
                break;

            case "":
                htmlBuilder.createDump();
                return;
            }

            htmlBuilder.createWebPage(subsectionType, bookIdInt, int.Parse(custId), store);
        }
コード例 #4
0
        public static ModelStore LoadFrom(TextReader reader)
        {
            var store = new ModelStore();

            try
            {
                if (reader.ReadLine() != "DATA-BEGIN")
                {
                    return(null);
                }
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        return(null);
                    }
                    else if (line == "DATA-END")
                    {
                        break;
                    }

                    string[] tokens = line.Split(';');
                    switch (tokens[0])
                    {
                    case "BOOK":
                        store.books.Add(new Book
                        {
                            Id     = int.Parse(tokens[1]),
                            Title  = tokens[2],
                            Author = tokens[3],
                            Price  = int.Parse(tokens[4])
                        });
                        break;

                    case "CUSTOMER":
                        store.customers.Add(new Customer
                        {
                            Id        = int.Parse(tokens[1]),
                            FirstName = tokens[2],
                            LastName  = tokens[3]
                        });
                        break;

                    case "CART-ITEM":
                        var customer = store.GetCustomer(int.Parse(tokens[1]));
                        if (customer == null)
                        {
                            return(null);
                        }
                        customer.ShoppingCart.Items.Add(new ShoppingCartItem
                        {
                            BookId = int.Parse(tokens[2]),
                            Count  = int.Parse(tokens[3])
                        });
                        break;

                    default:
                        return(null);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException || ex is IndexOutOfRangeException)
                {
                    return(null);
                }
                throw;
            }

            return(store);
        }