Question #44781

Develop a program that adds a set of product details. Each product has id, name, unitsInStock, and price. The program has to implement the following functionality, Implement using generic list
Add product
Change units in stock and price of a product by id
Delete product by id
View all the products
Search products whose price above a specified value(if the user enters 100 then display all the products where price is above 100)
1

Expert's answer

2014-08-11T02:39:21-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Set_of_product_details
{
    class Program
    {
        static void Main(string[] args)
        {
            //list of Products
            List<Product> listProducts = new List<Product>();
            //user choice
            int ch = -1;
            while (ch != 6)
            {
                ch = -1;
                while (ch < 1 || ch > 6)
                {
                    //show menu
                    Console.WriteLine("1 - Add product");
                    Console.WriteLine("2 - Change units in stock and price of a product by id");
                    Console.WriteLine("3 - Delete product by id");
                    Console.WriteLine("4 - View all the products");
                    Console.WriteLine("5 - Search products whose price above a specified value");
                    Console.WriteLine("6 - Exit");
                    Console.Write("Your choice: ");
                    //read data from file
                    ch = int.Parse(Console.ReadLine());
                    switch (ch)
                    {
                        case 1:
                            //create new product
                            Product newProduct = new Product();
                            Console.Write("Enter product ID: ");
                            //read Id from keyboard
                            newProduct.Id = Console.ReadLine();
                            Console.Write("Enter product Name: ");
                            //read Name from keyboard
                            newProduct.Name = Console.ReadLine();
                            Console.Write("Enter product Units In Stock: ");
                            //read UnitsInStock from keyboard
                            newProduct.UnitsInStock = int.Parse(Console.ReadLine());
                            Console.Write("Enter product Price: ");
                            //read Price from keyboard
                            newProduct.Price = double.Parse(Console.ReadLine());
                            //add new product to list
                            listProducts.Add(newProduct);
                            //show message that New product has been added to list
                            Console.WriteLine("\nNew product has been added to list!!!\n");
                            break;
                        case 2:
                            //search by id
                            Console.Write("Enter product ID: ");
                            //read product id
                            string productId = Console.ReadLine();
                            int index = -1;
                            //search products with such id
                            for (int i = 0; i < listProducts.Count; i++)
                            {
                                if (productId.Equals(listProducts[i].Id))
                                {
                                    index = i;
                                    break;
                                }
                            }
                            // if no products show message No such product
                            if (index == -1)
                            {
                                Console.WriteLine("\nNo such product!!!\n");
                            }
                            else
                            {
                                //Change units in stock and price of a product by id
                                newProduct = new Product();
                                // set old id and Name of product
                                newProduct.Id = listProducts[index].Id;
                                newProduct.Name = listProducts[index].Name;
                                Console.Write("Enter product Units In Stock: ");
                                // read Units In Stock from keyboard
                                newProduct.UnitsInStock = int.Parse(Console.ReadLine());
                                Console.Write("Enter product Price: ");
                                // read price from keyboard
                                newProduct.Price = double.Parse(Console.ReadLine());
                                // remove old information about product
                                listProducts.RemoveAt(index);
                                // add new information about product
                                listProducts.Insert(index, newProduct);
                            }
                            break;
                        case 3:
                            Console.Write("Enter product ID: ");
                            // read product id
                            productId = Console.ReadLine();
                            index = -1;
                            // search product by id
                            for (int i = 0; i < listProducts.Count; i++)
                            {
                                if (productId.Equals(listProducts[i].Id))
                                {
                                    index = i;
                                    break;
                                }
                            }
                            // if no products show message No such product
                            if (index == -1)
                            {
                                Console.WriteLine("\nNo such product!!!\n");
                            }
                            else
                            {
                                // Remove product from list
                                listProducts.RemoveAt(index);
                                Console.WriteLine("\nThis product has been removed from list!!!\n");
                            }
                            break;
                        case 4:
                            // show name of column
                            Console.WriteLine("ID\tName\tUnits In Stock\tPrice");
                            Console.WriteLine("---");
                            //show all products in list
                            for (int i = 0; i < listProducts.Count; i++)
                            {
                                Console.WriteLine(listProducts[i].ToString());
                            }
                            break;
                        case 5:
                            //Search products whose price above a specified value(if the user enters 100 then display all the products where price is above 100)
                            Console.Write("Enter product Price: ");
                            //read price from keyboard
                            double ProductPrice = double.Parse(Console.ReadLine());
                            index = -1;
                            //show name of column
                            Console.WriteLine("ID\tName\tUnits In Stock\tPrice");
                            Console.WriteLine("-----------------------------------");
                            //show list of products
                            for (int i = 0; i < listProducts.Count; i++)
                            {
                                if (ProductPrice <= listProducts[i].Price)
                                {
                                    Console.WriteLine(listProducts[i].ToString());
                                    index++;
                                }
                            }
                            //if no products show message No such product
                            if (index == -1)
                            {
                                Console.WriteLine("\nNo such product!!!\n");
                            }
                            break;
                    }
                }
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Set_of_product_details
{
    public class Product
    {
        //fields
        private string id;
        private string name;
        private int unitsInStock;
        private double price;
        //Encapsulate fields
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int UnitsInStock
        {
            get { return unitsInStock; }
            set { unitsInStock = value; }
        }
        public double Price
        {
            get { return price; }
            set { price = value; }
        }
        // <summary>
        /// Return information about product
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return id + "\t" + name + "\t\t" + unitsInStock.ToString() + "\t" + price.ToString();
        }
    }
}


file:///E:/Assignment Expert/Visual Studio 2010/Set of product details/Set of pr...

1 - Add product

2 - Change units in stock and price of a product by id

3 - Delete product by id

4 - View all the products

5 - Search products whose price above a specified value

6 - Exit

Your choice: 1

Enter product ID: as

Enter product Name: 5ads

Enter product Units In Stock: 3

Enter product Price: 600

New product has been added to list???

1 - Add product

2 - Change units in stock and price of a product by id

3 - Delete product by id

4 - View all the products

5 - Search products whose price above a specified value

6 - Exit

Your choice: 1

Enter product ID: asdas

Enter product Name: sads

Enter product Units In Stock: 6

Enter product Price: 90

New product has been added to list???

1 - Add product

2 - Change units in stock and price of a product by id

3 - Delete product by id

4 - View all the products

5 - Search products whose price above a specified value

6 - Exit

Your choice: 1

Enter product ID: asd

Enter product Name: asdas

Enter product Units In Stock: 6

Enter product Price: 900

New product has been added to list???

1 - Add product

2 - Change units in stock and price of a product by id

3 - Delete product by id

4 - View all the products

5 - Search products whose price above a specified value

6 - Exit

Your choice: 5

Enter product Price: 100



1 - Add product

2 - Change units in stock and price of a product by id

3 - Delete product by id

http://www.AssignmentExpert.com/

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!
LATEST TUTORIALS
APPROVED BY CLIENTS