コード例 #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
 public void createWebPage(string subsectionType, int bookId, int custId, ModelStore store)
 {
     createHtml();
     createHead();
     createBody(subsectionType, bookId, custId, store);
     endHTML();
 }
コード例 #3
0
        static void Main(string[] args)
        {
            using (var sw = new StreamWriter("NezarkaTest.html"))
            {
                TextReader textReader = Console.In;
                TextWriter textWriter = Console.Out;

                //textWriter = sw; //for debug

                //load store data
                ModelStore store = ModelStore.LoadFrom(textReader);
                if (store == null)
                {
                    Console.WriteLine("Data error.");
                    Environment.Exit(0);
                    return;
                }
                HTMLBuilder htmlBuilder = new HTMLBuilder(textWriter);
                GetParser   getParser   = new GetParser(htmlBuilder, store);

                //commands execution
                while (true)
                {
                    string getCommand = textReader.ReadLine();
                    if (getCommand == null)
                    {
                        break;
                    }
                    getParser.parse(getCommand);
                }
            }
        }
コード例 #4
0
        private void showShoppingCart(ShoppingCart shoppingCart, ModelStore store)
        {
            if (shoppingCart.Items.Count == 0)
            {
                writer.WriteLine(ind(1) + "Your shopping cart is EMPTY.");
                return;
            }



            int price = 0;

            writer.WriteLine(ind(1) + "Your shopping cart:");
            writer.WriteLine(ind(1) + "<table>");
            writer.WriteLine(ind(2) + "<tr>");
            writer.WriteLine(ind(3) + "<th>Title</th>");
            writer.WriteLine(ind(3) + "<th>Count</th>");
            writer.WriteLine(ind(3) + "<th>Price</th>");
            writer.WriteLine(ind(3) + "<th>Actions</th>");
            writer.WriteLine(ind(2) + "</tr>");

            for (int i = 0; i < shoppingCart.Items.Count; i++)
            {
                int  bookId     = shoppingCart.Items[i].BookId;
                int  count      = shoppingCart.Items[i].Count;
                Book book       = store.GetBook(bookId);
                int  totalPrice = count * book.Price;

                writer.WriteLine(ind(2) + "<tr>");
                writer.WriteLine(ind(3) + "<td><a href=\"/Books/Detail/{0}\">{1}</a></td>", book.Id, book.Title);
                writer.WriteLine(ind(3) + "<td>{0}</td>", count);
                if (count == 1)
                {
                    writer.WriteLine(ind(3) + "<td>{0} EUR</td>", book.Price);
                }
                else
                {
                    writer.WriteLine(ind(3) + "<td>{0} * {1} = {2} EUR</td>", count, book.Price, totalPrice);
                }
                writer.WriteLine(ind(3) + "<td>&lt;<a href=\"/ShoppingCart/Remove/{0}\">Remove</a>&gt;</td>", book.Id);
                writer.WriteLine(ind(2) + "</tr>");

                price += totalPrice;
            }



            writer.WriteLine(ind(1) + "</table>");
            writer.WriteLine(ind(1) + "Total price of all items: {0} EUR", price);
        }
コード例 #5
0
        /// <summary>
        /// add item to cart
        /// </summary>
        /// <param name="store"></param>
        /// <param name="bookId"></param>
        /// <param name="shoppingCart"></param>
        /// <returns>true on succues</returns>
        private bool addItemToCart(ModelStore store, int bookId, ShoppingCart shoppingCart)
        {
            ShoppingCartItem shoppingCartItem = shoppingCart.Items.Find(r => r.BookId == bookId);

            if (shoppingCartItem == null)
            {
                shoppingCart.Items.Add(new ShoppingCartItem
                {
                    BookId = bookId,
                    Count  = 1
                });
            }
            else
            {
                shoppingCartItem.Count++;
            }
            return(true);
        }
コード例 #6
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;
            }
        }
コード例 #7
0
        /// <summary>
        /// remove one piece from cart or remove it if there left only one
        /// </summary>
        /// <param name="store"></param>
        /// <param name="bookId"></param>
        /// <param name="shoppingCart"></param>
        /// <returns>true on succues</returns>
        private bool removeItemFromCart(ModelStore store, int bookId, ShoppingCart shoppingCart)
        {
            ShoppingCartItem shoppingCartItem = shoppingCart.Items.Find(r => r.BookId == bookId);

            if (shoppingCartItem == null)
            {
                return(false);
            }
            if (shoppingCartItem.Count == 1)
            {
                shoppingCart.Items.Remove(shoppingCartItem);
            }
            if (shoppingCartItem.Count < 1)
            {
                return(false);
            }
            else
            {
                shoppingCartItem.Count--;
            }
            return(true);
        }
コード例 #8
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);
        }
コード例 #9
0
 public GetParser(HTMLBuilder htmlBuilder, ModelStore store)
 {
     this.htmlBuilder = htmlBuilder;
     this.store       = store;
 }