示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            string textBoxText = "";

            if (radioButton1.Checked == true)
            {
                foreach (object product in listOfProducts)
                {
                    if (product is Toy)
                    {
                        Toy toyProduct = product as Toy;
                        textBoxText += toyProduct.getDescription();
                        textBoxText += "\r\n---------------------\r\n";
                    }
                }
            }
            else if (radioButton2.Checked == true)
            {
                foreach (object product in listOfProducts)
                {
                    if (product is Book)
                    {
                        if (product is Book)
                        {
                            Book bookProduct = product as Book;
                            textBoxText += bookProduct.getDescription();
                            textBoxText += "\r\n---------------------\r\n";
                        }
                    }
                }
            }
            else if (radioButton3.Checked == true)
            {
                foreach (object product in listOfProducts)
                {
                    if (product is SportEquipment)
                    {
                        if (product is SportEquipment)
                        {
                            SportEquipment sportEquipmentProduct = product as SportEquipment;
                            textBoxText += sportEquipmentProduct.getDescription();
                            textBoxText += ("\r\n---------------------\r\n");
                        }
                    }
                }
            }
            else if (radioButton4.Checked == true)
            {
                foreach (object product in listOfProducts)
                {
                    if (product is Toy)
                    {
                        Toy toyProduct = product as Toy;
                        textBoxText += toyProduct.getDescription();
                        textBoxText += "\r\n---------------------\r\n";
                    }
                    if (product is Book)
                    {
                        Book bookProduct = product as Book;
                        textBoxText += bookProduct.getDescription();
                        textBoxText += "\r\n---------------------\r\n";
                    }
                    if (product is SportEquipment)
                    {
                        SportEquipment sportEquipmentProduct = product as SportEquipment;
                        textBoxText += sportEquipmentProduct.getDescription();
                        textBoxText += "\r\n---------------------\r\n";
                    }
                }
            }

            textBox1.Text = textBoxText;

            if (textBox1.Text == "" && listOfProducts.Count != 0)
            {
                textBox1.Text = "Nothing Found";
            }
            else if (textBox1.Text == "")
            {
                textBox1.Text = "No items in the list";
            }
        }
示例#2
0
        private void button2_Click(object sender, EventArgs e)
        {
            List <object> myList = new List <object>();

            /*
             * textBox1.Text = "";
             * Random rnd = new Random();
             *
             * for (int i = 0; i < 10; i++)
             * {
             *  int newRandomPrice = rnd.Next(10, 100);
             *  int newRandomAge = rnd.Next(2, 8);
             *  Toy newToy = new Toy(newRandomPrice, $"toy{i + 1}", newRandomAge, $"{i * newRandomAge}-Corporation", $"some-{i + 2}");
             *  myList.Add(newToy);
             * }
             * for (int i = 0; i < 10; i++)
             * {
             *  int newRandomPrice = rnd.Next(10, 100);
             *  int newRandomAge = rnd.Next(2, 8);
             *  Book newBook = new Book(newRandomPrice, $"toy{i + 1}", newRandomAge, $"{i * newRandomAge}-Corporation", $"Joh-{i + 2}");
             *  myList.Add(newBook);
             * }
             * for (int i = 0; i < 10; i++)
             * {
             *  int newRandomPrice = rnd.Next(10, 100);
             *  int newRandomAge = rnd.Next(2, 8);
             *  SportEquipment newToy = new SportEquipment(newRandomPrice, $"toy{i + 1}", newRandomAge, $"{i * newRandomAge}-Corporation");
             *  myList.Add(newToy);
             * }
             */
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load("C:\\Users\\ericg\\Developer\\Univerisy-Projects\\help-projects\\Mary\\DotNetLab04-Mary\\ProductsData.xml");

            XmlNodeList toys           = xmlDoc.GetElementsByTagName("Toy");
            XmlNodeList books          = xmlDoc.GetElementsByTagName("Book");
            XmlNodeList sportEquipment = xmlDoc.GetElementsByTagName("SportEquipment");

            for (int i = 0; i < toys.Count; i++)
            {
                String manufacturer = toys[i].ChildNodes.Item(0).InnerText;
                String material     = toys[i].ChildNodes.Item(1).InnerText;
                String price        = toys[i].ChildNodes.Item(2).InnerText;
                String name         = toys[i].ChildNodes.Item(3).InnerText;
                String age          = toys[i].ChildNodes.Item(4).InnerText;
                Toy    newToy       = new Toy(Convert.ToDouble(price), $"{name}-{i}", Convert.ToInt32(age), manufacturer, material);
                myList.Add(newToy);
            }

            for (int i = 0; i < books.Count; i++)
            {
                String author    = toys[i].ChildNodes.Item(0).InnerText;
                String publisher = toys[i].ChildNodes.Item(1).InnerText;
                String price     = toys[i].ChildNodes.Item(2).InnerText;
                String name      = toys[i].ChildNodes.Item(3).InnerText;
                String age       = toys[i].ChildNodes.Item(4).InnerText;
                Book   newBook   = new Book(Convert.ToDouble(price), $"{name}-{i}", Convert.ToInt32(age), author, publisher);
                myList.Add(newBook);
            }

            for (int i = 0; i < sportEquipment.Count; i++)
            {
                String         manufacturer      = toys[i].ChildNodes.Item(0).InnerText;
                String         price             = toys[i].ChildNodes.Item(2).InnerText;
                String         name              = toys[i].ChildNodes.Item(3).InnerText;
                String         age               = toys[i].ChildNodes.Item(4).InnerText;
                SportEquipment newSportEquipment = new SportEquipment(Convert.ToDouble(price), $"{name}-{i}", Convert.ToInt32(age), manufacturer);
                myList.Add(newSportEquipment);
            }

            listOfProducts = myList.OrderBy(x => Guid.NewGuid()).ToList();

            string textBoxText = "";

            foreach (object product in listOfProducts)
            {
                if (product is Toy)
                {
                    Toy toyProduct = product as Toy;
                    textBoxText += toyProduct.getDescription();
                    textBoxText += "\r\n---------------------\r\n";
                }
                if (product is Book)
                {
                    Book bookProduct = product as Book;
                    textBoxText += bookProduct.getDescription();
                    textBoxText += "\r\n---------------------\r\n";
                }
                if (product is SportEquipment)
                {
                    SportEquipment sportEquipmentProduct = product as SportEquipment;
                    textBoxText += sportEquipmentProduct.getDescription();
                    textBoxText += "\r\n---------------------\r\n";
                }
            }
            textBox1.Text = textBoxText;
        }