Exemplo n.º 1
0
 public void ShoppingCartEmpty(int CustID, ModelStore model)
 {
     Head();
     Style();
     Header(model.GetCustomer(CustID).FirstName, model.GetCustomer(CustID).ShoppingCart.Items.Count);
     Empty();
     Foot();
     End();
 }
Exemplo n.º 2
0
 public void BooksDetail(int CustID, int bookID, ModelStore model)
 {
     Head();
     Style();
     Header(model.GetCustomer(CustID).FirstName, model.GetCustomer(CustID).ShoppingCart.Items.Count);
     Detail(bookID, model);
     Foot();
     End();
 }
Exemplo n.º 3
0
        public void ShopingCart(int CustID, ModelStore model)
        {
            decimal price = 0;

            Console.WriteLine("\tYour shopping cart:");
            Console.WriteLine("\t<table>");


            Console.WriteLine("\t\t<tr>");
            Console.WriteLine("\t\t\t<th>Title</th>");
            Console.WriteLine("\t\t\t<th>Count</th>");
            Console.WriteLine("\t\t\t<th>Price</th>");
            Console.WriteLine("\t\t\t<th>Actions</th>");
            Console.WriteLine("\t\t</tr>");

            foreach (ShoppingCartItem item in model.GetCustomer(CustID).ShoppingCart.Items)
            {
                Console.WriteLine("\t\t<tr>");

                Console.WriteLine("\t\t\t<td><a href=\"/Books/Detail/{0}\">{1}</a></td>", item.BookId, model.GetBook(item.BookId).Title);
                Console.WriteLine("\t\t\t<td>{0}</td>", item.Count);


                if (item.Count > 1)
                {
                    Console.WriteLine("\t\t\t<td>{0} * {1} = {2} EUR</td>", item.Count, model.GetBook(item.BookId).Price, (model.GetBook(item.BookId).Price *item.Count));
                }
                else
                {
                    Console.WriteLine("\t\t\t<td>{0} EUR</td>", model.GetBook(item.BookId).Price);
                }

                Console.WriteLine("\t\t\t<td>&lt;<a href=\"/ShoppingCart/Remove/{0}\">Remove</a>&gt;</td>", item.BookId);
                Console.WriteLine("\t\t</tr>");
                price += (item.Count * model.GetBook(item.BookId).Price);
            }


            Console.WriteLine("\t</table>");
            Console.WriteLine("\tTotal price of all items: {0} EUR", price);
        }
Exemplo n.º 4
0
 public bool Split(string line)
 {
     prikaz = line.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
     try {
         if (prikaz[0] == "GET")
         {
             CustomerId = Int32.Parse(prikaz[1]);
             string name = model.GetCustomer(CustomerId).FirstName;
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch
     {
         return(false);
     }
 }
Exemplo n.º 5
0
        public static ModelStore LoadFrom(TextReader reader, ModelStore store)
        {
            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  = decimal.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);
                        }
                        var BookID = int.Parse(tokens[2]);
                        var CounT  = int.Parse(tokens[3]);
                        customer.ShoppingCart.Items.Add(new ShoppingCartItem
                        {
                            BookId = BookID,
                            Count  = CounT
                        });
                        break;

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

            return(store);
        }
Exemplo n.º 6
0
        public static void ReadAdnDoRequests(TextReader reader, TextWriter writer, ModelStore store)
        {
            Regex BookList              = new Regex(@"^GET \d+ http://www.nezarka.net/Books$");
            Regex BookDetail            = new Regex(@"^GET \d+ http://www.nezarka.net/Books/Detail/\d+$");
            Regex ShopingCart           = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart$");
            Regex ShopingCartAddItem    = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Add/\d+$");
            Regex ShopingCartRemoveItem = new Regex(@"^GET \d+ http://www.nezarka.net/ShoppingCart/Remove/\d+$");

            char[] delims = new char[] { ' ', '/' };

            // bool first = true;

            string line = reader.ReadLine();

            while (line != null && line != "")
            {
                string[] Records = line.Split(delims, StringSplitOptions.RemoveEmptyEntries);

                /*if (!first)
                 * {
                 *  writer.WriteLine("====");
                 * }
                 * first = false; */
                if (BookList.Match(line).Length > 0)
                {
                    int      cusID = Int32.Parse(Records[1]);
                    Customer cust  = store.GetCustomer(cusID);

                    if (cust == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    string      firstName = cust.FirstName;
                    List <Book> Books     = store.GetBooks();
                    int         numItems  = cust.CountItemsInCart();
                    View.GenBookList(writer, firstName, numItems, Books);
                }
                else if (BookDetail.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    string firstNsame = cust.FirstName;
                    int    numItems   = cust.CountItemsInCart();

                    View.GenBookDetail(writer, firstNsame, numItems, b.Title, b.Author, b.Price, b.Id);
                }
                else if (ShopingCart.Match(line).Length > 0)
                {
                    int      cusID = Int32.Parse(Records[1]);
                    Customer cust  = store.GetCustomer(cusID);
                    if (cust == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }

                    string firstName = cust.FirstName;
                    if (cust.CountItemsInCart() == 0)
                    {
                        View.GenCartEmpty(writer, firstName);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    List <ShoppingCartItem> cart = cust.GetCart();

                    View.GenCart(writer, firstName, cart, store);
                }
                else if (ShopingCartAddItem.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    //TODO: create shopping cart?
                    cust.ShoppingCart.AddItem(bookID);

                    string firstName             = cust.FirstName;
                    List <ShoppingCartItem> cart = cust.GetCart();

                    View.GenCart(writer, firstName, cart, store);
                }
                else if (ShopingCartRemoveItem.Match(line).Length > 0)
                {
                    int      cusID  = Int32.Parse(Records[1]);
                    int      bookID = Int32.Parse(Records[Records.Length - 1]);
                    Customer cust   = store.GetCustomer(cusID);
                    Book     b      = store.GetBook(bookID);
                    if (cust == null || b == null)
                    {
                        View.GenInvalidRequest(writer);
                        writer.WriteLine("====");
                        writer.Flush();
                        line = reader.ReadLine();
                        continue;
                    }
                    bool RemoveSuccesful = cust.ShoppingCart.RemoveItem(bookID);
                    if (RemoveSuccesful)
                    {
                        string firstName             = cust.FirstName;
                        List <ShoppingCartItem> cart = cust.GetCart();
                        if (cust.CountItemsInCart() == 0)
                        {
                            View.GenCartEmpty(writer, firstName);
                            writer.WriteLine("====");
                            writer.Flush();
                            line = reader.ReadLine();
                            continue;
                        }
                        View.GenCart(writer, firstName, cart, store);
                    }
                    else
                    {
                        View.GenInvalidRequest(writer);
                    }
                }
                else
                {
                    View.GenInvalidRequest(writer);
                }
                writer.WriteLine("====");
                writer.Flush();
                line = reader.ReadLine();
            }
        }
Exemplo n.º 7
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 = decimal.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;
        }