Answer to Question #266146 in C# for ivy

Question #266146

Make a console app with menu:

1. Add product

2. Search product

3. Add category

4. Add product to category

5.Display list of Cateory

6.Delete product

7. Exit

**************************

List of Category

*****************************

Name                      Price

------------------------------------------------------------------

T-Shirts (2)                 

  Black T-Shirt           199

  White T-Shirt           249

Shoes (1)                   

  Nike Sneakers           249

one class for  ProductCategory.
shall keep necessary information about a category, including associated products (List <T>).
  • Should have the Public Void Addproduct (Product Product) method to add product to category.
  • Should have constructor to create new category.
  • should use Properties (not fields) to keep the information in the category
  • use Dictionary for make list of Product
  • use List for make list of Category

For each category, the name and number of products are shown in this (to the right of the name). During the category, all products are listed to this, with the name and price.


1
Expert's answer
2021-11-15T12:36:31-0500
using System;
using System.Collections.Generic;


namespace App
{


    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }




        public Product() { }


        public Product(string Name, double Price)
        {
            this.Name = Name;
            this.Price = Price;
        }


        public override string ToString()
        {
            return String.Format("{0,-20}{1,-10}", Name, Price);
        }


    }
    class ProductCategory
    {
        //use Dictionary for make list of Product
        public Dictionary<string, Product> products { get; set; }
        public string Name { get; set; }
        /// <summary>
        ///  constructor to create new category. 
        /// </summary>
        public ProductCategory(string Name)
        {
            this.products = new Dictionary<string, Product>();
            this.Name = Name;
        }


        /// <summary>
        /// method to add product to category.
        /// </summary>
        /// <param name="Product"></param>
        public void Addproduct(Product Product)
        {
            if (!products.ContainsKey(Product.Name))
            {
                products.Add(Product.Name, Product);
            }
            else {
                Console.WriteLine("This product has been already added to the category." + Environment.NewLine);
            }
            
        }
        public void display()
        {
            Console.WriteLine(this.Name + " (" + products.Count.ToString() + ")");
            foreach (KeyValuePair<string, Product> keyValue in products)
            {
                Console.WriteLine("{0,-20}{1,-10}", keyValue.Value.Name, keyValue.Value.Price);
            }
        }
    }


    class Program
    {




        static void Main(string[] args)
        {
            //use List for make list of Category
            List<ProductCategory> categories = new List<ProductCategory>();
            List<Product> products = new List<Product>();




            //test data
            products.Add(new Product("Black T-Shirt",199));
            products.Add(new Product("White T-Shirt", 249));
            products.Add(new Product("Nike Sneakers", 249));


            categories.Add(new ProductCategory("T-Shirts"));
            categories.Add(new ProductCategory("Shoes"));


            categories[0].Addproduct(products[0]);
            categories[0].Addproduct(products[1]);


            categories[1].Addproduct(products[2]);




            int ch = -1;


            while (ch != 7)
            {
                Console.WriteLine("1. Add product");
                Console.WriteLine("2. Search product");
                Console.WriteLine("3. Add category");
                Console.WriteLine("4. Add product to category");
                Console.WriteLine("5. Display list of Cateory");
                Console.WriteLine("6. Delete product");
                Console.WriteLine("7. Exit");
                Console.Write("You5 choice: ");
                ch = int.Parse(Console.ReadLine());
                switch (ch)
                {
                    case 1:
                        {
                            if (categories.Count > 0)
                            {
                                Console.Write("Enter a product name: ");
                                string name = Console.ReadLine();
                                Console.Write("Enter a product price: ");
                                double price = double.Parse(Console.ReadLine());
                                Product newProduct = new Product(name, price);
                                products.Add(newProduct);
                                Console.WriteLine("A new product has been added." + Environment.NewLine);
                            }
                            else {
                                Console.WriteLine("Add a new product category." + Environment.NewLine);
                            }
                        }
                        break;
                    case 2:
                        {
                            Console.Write("Enter a product name to search: ");
                            string name = Console.ReadLine();
                            Console.WriteLine("*****************************");
                            Console.WriteLine("List of Products");
                            Console.WriteLine("*****************************");
                            Console.WriteLine("{0,-20}{1,-10}", "Name", "Price");
                            Console.WriteLine("-----------------------------");
                            for (int i = 0; i < categories.Count; i++)
                            {
                                foreach (KeyValuePair<string, Product> keyValue in categories[i].products)
                                {
                                    if (keyValue.Value.Name.CompareTo(name) == 0) {
                                        Console.WriteLine("{0,-20}{1,-10}", keyValue.Value.Name, keyValue.Value.Price);
                                    }
                                }
                            }


                        }
                        break;
                    case 3:
                        {
                            Console.Write("Enter a category name: ");
                            string name = Console.ReadLine();
                            ProductCategory newProductCategory = new ProductCategory(name);
                            categories.Add(newProductCategory);
                            Console.WriteLine("A new product category has been added." + Environment.NewLine);
                        }
                        break;
                    case 4:
                        {
                            for (int i = 0; i < products.Count; i++)
                            {
                                Console.WriteLine((i + 1).ToString() + ". " + products[i].Name + "  $" + products[i].Price.ToString());
                            }
                            Console.Write("Select product: ");
                            int selectedProduct = int.Parse(Console.ReadLine());


                            for (int i = 0; i < categories.Count; i++)
                            {
                                Console.WriteLine((i + 1).ToString() + ". " + categories[i].Name);
                            }
                            Console.Write("Select category: ");
                            int selectedCategory = int.Parse(Console.ReadLine());
                            categories[selectedCategory - 1].Addproduct(products[selectedProduct-1]);
                            Console.WriteLine("A new product has been added to category." + Environment.NewLine);
                        }
                        break;
                    case 5:
                        {
                            Console.WriteLine();
                            Console.WriteLine("*****************************");
                            Console.WriteLine("List of Category ");
                            Console.WriteLine("*****************************");
                            Console.WriteLine("{0,-20}{1,-10}", "Name", "Price");
                            Console.WriteLine("-----------------------------");
                            for (int i = 0; i < categories.Count; i++)
                            {
                                categories[i].display();
                            }
                            Console.WriteLine();
                            Console.WriteLine();
                        }
                        break;
                    case 6:
                        {
                            Console.Write("Enter a product name to delete: ");
                            string name = Console.ReadLine();
                            for (int i = 0; i < categories.Count; i++)
                            {
                                foreach (KeyValuePair<string, Product> keyValue in categories[i].products)
                                {
                                    if (keyValue.Value.Name.CompareTo(name) == 0)
                                    {
                                        categories[i].products.Remove(keyValue.Key);
                                        Console.WriteLine("A new product has been deleted from the category." + Environment.NewLine);
                                        break;
                                    }
                                }
                            }
                        }
                        break;
                    case 7:
                        //exit
                        break;
                    default:
                        Console.WriteLine("Wrong menu item");
                        break;
                }
            }


            Console.ReadLine();
        }




    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS