예제 #1
0
        /*public void StoreToMemory(List<Product> products)
         * {
         *  foreach (Product product in products)
         *  {
         *      StoreProduct(product);
         *  }
         * }*/
        void SaveToCSV(Product product)
        {
            StreamWriter streamWriter = new StreamWriter("Store.csv", true);

            if (product is BookProduct)
            {
                BookProduct b      = (BookProduct)product;
                string      record = "Book;";
                record += b.name + ";";
                record += b.price.ToString() + ";";
                record += b.numOfPages.ToString();
                streamWriter.WriteLine(record);
            }
            else
            {
                CDProduct c      = (CDProduct)product;
                string    record = "CD;";
                record += c.name + ";";
                record += c.price.ToString() + ";";
                record += c.numOfTracks.ToString();
                streamWriter.WriteLine(record);
            }
            streamWriter.Close();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("The product has saved to the Store.csv file.");
            Console.ForegroundColor = ConsoleColor.White;
            Thread.Sleep(2000);
        }
예제 #2
0
 protected Product CreateProduct(string type, string name, int price, int size)
 {
     if (type == "Book")
     {
         Product newProduct = new BookProduct(name, price, size);
         return(newProduct);
     }
     else
     {
         Product newProduct = new CDProduct(name, price, size);
         return(newProduct);
     }
 }
예제 #3
0
        public override List <Product> LoadProducts()
        {
            List <Product> products = new List <Product>();
            string         type;
            string         name;
            int            price;
            int            size;
            StreamReader   streamReader = new StreamReader("Store.csv");

            while (!streamReader.EndOfStream)
            {
                string[] line = streamReader.ReadLine().Split(";");
                type  = line[0];
                name  = line[1];
                price = int.Parse(line[2]);
                size  = int.Parse(line[3]);
                {
                    if (type == "Book")
                    {
                        BookProduct bookProduct = new BookProduct();
                        bookProduct.name       = name;
                        bookProduct.price      = price;
                        bookProduct.numOfPages = size;
                        products.Add(bookProduct);
                    }
                    else
                    {
                        CDProduct cdProduct = new CDProduct();
                        cdProduct.name        = name;
                        cdProduct.price       = price;
                        cdProduct.numOfTracks = size;
                        products.Add(cdProduct);
                    }
                }
            }
            streamReader.Close();
            return(products);
        }
예제 #4
0
        public override List <Product> LoadProducts()
        {
            List <Product> products = new List <Product>();
            string         type;
            string         name;
            int            price;
            int            size;
            XmlDocument    doc = new XmlDocument();

            doc.Load("Store.xml");
            XmlNode root = doc.DocumentElement;

            foreach (XmlNode xmlNode in root)
            {
                type  = xmlNode.Attributes["type"].Value;
                name  = xmlNode.ChildNodes[0].ChildNodes[0].Attributes[0].Value;
                price = int.Parse(xmlNode.ChildNodes[0].ChildNodes[1].Attributes[0].Value);
                size  = int.Parse(xmlNode.ChildNodes[0].ChildNodes[2].Attributes[0].Value);
                if (type == "Book")
                {
                    BookProduct bookProduct = new BookProduct();
                    bookProduct.name       = name;
                    bookProduct.price      = price;
                    bookProduct.numOfPages = size;
                    products.Add(bookProduct);
                }
                else
                {
                    CDProduct cdProduct = new CDProduct();
                    cdProduct.name        = name;
                    cdProduct.price       = price;
                    cdProduct.numOfTracks = size;
                    products.Add(cdProduct);
                }
            }
            return(products);
        }
예제 #5
0
        void SaveToXml(Product product)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode     root;

            if (File.Exists("Store.xml"))
            {
                doc.Load("Store.xml");
                root = doc.DocumentElement;
                if (product is BookProduct)
                {
                    BookProduct  b          = (BookProduct)product;
                    XmlNode      node2      = doc.CreateElement("product");
                    XmlAttribute attribute2 = doc.CreateAttribute("type");
                    attribute2.Value = "Book";
                    node2.Attributes.Append(attribute2);
                    root.AppendChild(node2);
                    XmlNode node3 = doc.CreateElement("Properties");
                    node2.AppendChild(node3);

                    XmlNode      node41      = doc.CreateElement("Property");
                    XmlAttribute attribute41 = doc.CreateAttribute("Title");
                    attribute41.Value = b.name;
                    node41.Attributes.Append(attribute41);
                    node3.AppendChild(node41);

                    XmlNode      node42      = doc.CreateElement("Property");
                    XmlAttribute attribute42 = doc.CreateAttribute("Price");
                    attribute42.Value = b.price.ToString();
                    node42.Attributes.Append(attribute42);
                    node3.AppendChild(node42);

                    XmlNode      node43      = doc.CreateElement("Property");
                    XmlAttribute attribute43 = doc.CreateAttribute("Pages");
                    attribute43.Value = b.numOfPages.ToString();
                    node43.Attributes.Append(attribute43);
                    node3.AppendChild(node43);

                    doc.Save("Store.xml");
                }
                else
                {
                    CDProduct    c          = (CDProduct)product;
                    XmlNode      node2      = doc.CreateElement("product");
                    XmlAttribute attribute2 = doc.CreateAttribute("type");
                    attribute2.Value = "CD";
                    node2.Attributes.Append(attribute2);
                    root.AppendChild(node2);
                    XmlNode node3 = doc.CreateElement("Properties");
                    node2.AppendChild(node3);

                    XmlNode      node41      = doc.CreateElement("Property");
                    XmlAttribute attribute41 = doc.CreateAttribute("Title");
                    attribute41.Value = c.name;
                    node41.Attributes.Append(attribute41);
                    node3.AppendChild(node41);

                    XmlNode      node42      = doc.CreateElement("Property");
                    XmlAttribute attribute42 = doc.CreateAttribute("Price");
                    attribute42.Value = c.price.ToString();
                    node42.Attributes.Append(attribute42);
                    node3.AppendChild(node42);

                    XmlNode      node43      = doc.CreateElement("Property");
                    XmlAttribute attribute43 = doc.CreateAttribute("Tracks");
                    attribute43.Value = c.numOfTracks.ToString();
                    node43.Attributes.Append(attribute43);
                    node3.AppendChild(node43);

                    doc.Save("Store.xml");
                }
            }
            else
            {
                XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                root = doc.CreateElement("products");
                doc.AppendChild(root);
                doc.InsertBefore(xmlDeclaration, root);
                if (product is BookProduct)
                {
                    BookProduct b = (BookProduct)product;

                    XmlNode      node2      = doc.CreateElement("product");
                    XmlAttribute attribute2 = doc.CreateAttribute("type");
                    attribute2.Value = "Book";
                    node2.Attributes.Append(attribute2);
                    root.AppendChild(node2);
                    XmlNode node3 = doc.CreateElement("Properties");
                    node2.AppendChild(node3);

                    XmlNode      node41      = doc.CreateElement("Property");
                    XmlAttribute attribute41 = doc.CreateAttribute("Title");
                    attribute41.Value = b.name;
                    node41.Attributes.Append(attribute41);
                    node3.AppendChild(node41);

                    XmlNode      node42      = doc.CreateElement("Property");
                    XmlAttribute attribute42 = doc.CreateAttribute("Price");
                    attribute42.Value = b.price.ToString();
                    node42.Attributes.Append(attribute42);
                    node3.AppendChild(node42);

                    XmlNode      node43      = doc.CreateElement("Property");
                    XmlAttribute attribute43 = doc.CreateAttribute("Pages");
                    attribute43.Value = b.numOfPages.ToString();
                    node43.Attributes.Append(attribute43);
                    node3.AppendChild(node43);

                    doc.Save("Store.xml");
                }
                else
                {
                    CDProduct    c          = (CDProduct)product;
                    XmlNode      node2      = doc.CreateElement("product");
                    XmlAttribute attribute2 = doc.CreateAttribute("type");
                    attribute2.Value = "CD";
                    node2.Attributes.Append(attribute2);
                    root.AppendChild(node2);
                    XmlNode node3 = doc.CreateElement("Properties");
                    node2.AppendChild(node3);

                    XmlNode      node41      = doc.CreateElement("Property");
                    XmlAttribute attribute41 = doc.CreateAttribute("Title");
                    attribute41.Value = c.name;
                    node41.Attributes.Append(attribute41);
                    node3.AppendChild(node41);

                    XmlNode      node42      = doc.CreateElement("Property");
                    XmlAttribute attribute42 = doc.CreateAttribute("Price");
                    attribute42.Value = c.price.ToString();
                    node42.Attributes.Append(attribute42);
                    node3.AppendChild(node42);

                    XmlNode      node43      = doc.CreateElement("Property");
                    XmlAttribute attribute43 = doc.CreateAttribute("Tracks");
                    attribute43.Value = c.numOfTracks.ToString();
                    node43.Attributes.Append(attribute43);
                    node3.AppendChild(node43);

                    doc.Save("Store.xml");
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("The product has saved to the Store.xml file.");
            Console.ForegroundColor = ConsoleColor.White;
            Thread.Sleep(2000);
        }
예제 #6
0
        static bool Choose()
        {
            string input = Console.ReadLine();

            switch (input)
            {
            case "1":
                Console.WriteLine();
                storeManager.AddBookProduct(AnyInput("The name of the book: "), JustNumber("The price of the book: "), JustNumber("Number of pages: "));
                return(true);

            case "2":
                Console.WriteLine();
                storeManager.AddCDProduct(AnyInput("The name of the CD: "), JustNumber("The price of the CD: "), JustNumber("Number of tracks: "));
                return(true);

            case "3":
                Console.Clear();
                Console.WriteLine("Store inventory list");
                Console.WriteLine();
                Console.WriteLine("Type          Title                           Size           Price");
                Console.WriteLine("------------------------------------------------------------------");

                foreach (Product product in storeManager.ListProducts())
                {
                    if (product is BookProduct)
                    {
                        Console.Write("Book  ");
                        BookProduct book = (BookProduct)product;
                        Console.Write(book.name.PadRight(40));
                        string pages = book.numOfPages.ToString() + " pages";
                        Console.Write(pages.PadRight(15));
                        string price = book.price.ToString() + " $";
                        Console.WriteLine(price.PadLeft(5));
                    }
                    else
                    {
                        Console.Write("CD    ");
                        CDProduct cd = (CDProduct)product;
                        Console.Write(cd.name.PadRight(40));
                        string tracks = cd.numOfTracks.ToString() + " tracks";
                        Console.Write(tracks.PadRight(15));
                        string price = cd.price.ToString() + " $";
                        Console.WriteLine(price.PadLeft(5));
                    }
                }
                Console.WriteLine();
                AnyInput("Press any key to continue...");
                return(true);

            case "4":
                Console.WriteLine();
                Console.WriteLine("The value of all products in the store: " + Convert.ToString(storeManager.GetTotalProductPrice()) + " $.");
                AnyInput("Press any key to continue...");
                return(true);

            case "0":
                return(false);

            default:
                return(true);
            }
        }