Exemple #1
0
        public void AllBooks(ModelStore model)
        {
            int i = 0;

            Console.WriteLine("\tOur books for you:");
            Console.WriteLine("\t<table>");
            if (model.GetBooks().Count > 0)
            {
                Console.WriteLine("\t\t<tr>");
                foreach (Book book in model.GetBooks())
                {
                    if (i % 3 == 0 && i != 0)
                    {
                        Console.WriteLine("\t\t</tr>");
                        Console.WriteLine("\t\t<tr>");
                    }


                    Console.WriteLine("\t\t\t<td style=\"padding: 10px;\">");
                    Console.WriteLine("\t\t\t\t<a href=\"/Books/Detail/{0}\">{1}</a><br />", book.Id, book.Title);
                    Console.WriteLine("\t\t\t\tAuthor: {0}<br />", book.Author);
                    Console.WriteLine("\t\t\t\tPrice: {0} EUR &lt;<a href=\"/ShoppingCart/Add/{1}\">Buy</a>&gt;", book.Price, book.Id);
                    Console.WriteLine("\t\t\t</td>");
                    i++;
                }
                Console.WriteLine("\t\t</tr>");
            }
            Console.WriteLine("\t</table>");
        }
Exemple #2
0
 public void ShoppingCartEmpty(int CustID, ModelStore model)
 {
     Head();
     Style();
     Header(model.GetCustomer(CustID).FirstName, model.GetCustomer(CustID).ShoppingCart.Items.Count);
     Empty();
     Foot();
     End();
 }
Exemple #3
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();
 }
Exemple #4
0
        public static ModelStore LoadFrom(TextReader reader)
        {
            var store = new ModelStore();

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

                    string[] tokens = line.Split(';', StringSplitOptions.RemoveEmptyEntries);
                    switch (tokens[0])
                    {
                    case "BOOK":
                        if (tokens.Length != 5)
                        {
                            //Console.WriteLine("Data error.");
                            return(null);
                        }
                        if (Book.TryParse(tokens[1], tokens[2], tokens[3], tokens[4], out Book b, store))
                        {
                            store.books.Add(b);
                        }
                        else     //parse failed, number was expected
                        {
                            //Console.WriteLine("Data error.");
                            return(null);
                        }
                        break;

                    case "CUSTOMER":
                        if (tokens.Length != 4)
                        {
                            // Console.WriteLine("Data error.");
                            return(null);
                        }
                        if (Customer.TryParse(tokens[1], tokens[2], tokens[3], out Customer c, store))
                        {
                            store.customers.Add(c);
                        }
Exemple #5
0
        public void Detail(int bookID, ModelStore model)
        {
            Console.WriteLine("\tBook details:");

            Console.WriteLine("\t<h2>{0}</h2>", model.GetBook(bookID).Title);
            Console.WriteLine("\t<p style=\"margin-left: 20px\">");
            Console.WriteLine("\tAuthor: {0}<br />", model.GetBook(bookID).Author);
            Console.WriteLine("\tPrice: {0} EUR<br />", model.GetBook(bookID).Price);
            Console.WriteLine("\t</p>");

            Console.WriteLine("\t<h3>&lt;<a href=\"/ShoppingCart/Add/{0}\">Buy this book</a>&gt;</h3>", bookID);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            Stream       InputStrem   = Console.OpenStandardInput();
            Stream       Outputstream = Console.OpenStandardOutput();
            StreamReader Reader       = new StreamReader(InputStrem);
            StreamWriter Writer       = new StreamWriter(Outputstream);

            //Writer.AutoFlush = true;
            Console.SetOut(Writer);
            ModelStore Store = ModelStore.LoadFrom(Reader);

            if (Store == null)
            {
                Writer.WriteLine("Data error.");
                Writer.Flush();
                return;
            }
            Controler.ReadAdnDoRequests(Reader, Writer, Store);
        }
Exemple #7
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);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            ModelStore model   = new ModelStore();
            Controller prikazy = new Controller(model);

            if (ModelStore.LoadFrom(Console.In, model) == null)
            {
                Console.WriteLine("Data error.");

                return;
            }

            while (true)
            {
                string line = Console.ReadLine();
                if (line == null)
                {
                    break;
                }
                prikazy.All(line);
            }
        }
Exemple #9
0
 public Controller(ModelStore model)
 {
     this.model = model;
     view       = new View();
 }
Exemple #10
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);
        }
Exemple #11
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();
            }
        }
Exemple #12
0
        public static void GenCart(TextWriter writer, string CustFirtsName, List <ShoppingCartItem> Items, ModelStore store)
        {
            GenFirstHead(writer);
            GenStyle(writer);
            GenCommonHeader(writer, CustFirtsName, Items.Count);
            writer.WriteLine("	Your shopping cart:");
            writer.WriteLine("	<table>");
            writer.WriteLine("		<tr>");
            writer.WriteLine("			<th>Title</th>");
            writer.WriteLine("			<th>Count</th>");
            writer.WriteLine("			<th>Price</th>");
            writer.WriteLine("			<th>Actions</th>");
            writer.WriteLine("		</tr>");

            decimal TotalPrice = 0;

            for (int i = 0; i < Items.Count; i++)
            {
                int  NumOfBooks = Items[i].Count;
                Book book       = store.GetBook(Items[i].BookId);
                GenCartItem(writer, book, NumOfBooks, ref TotalPrice);
            }

            writer.WriteLine("	</table>");
            writer.WriteLine($"	Total price of all items: {TotalPrice} EUR");
            writer.WriteLine("</body>");
            writer.WriteLine("</html>");
            writer.Flush();
        }
        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;
        }