Create a console application to accept Product Details like ProductNo, Name, Rate and Stock.
[Use Array List Collection]
Display the Menu to perform the following:
================
a. Adding New Product
b. Deleting Currently Searched Product
c. Searching Product
Searching will work as shown below:
• User will enter ProductNo.
• If the product with that productno exists in Collection, then the details should be shown, otherwise show appropriate message.
d. Save the New Product – The products should get saved in the sorted order of ProductNo.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
class Product
{
public int ProductNo{get;set;}
public string Name{get;set;}
public double Rate{get;set;}
public int Stock{get;set;}
public override string ToString()
{
return string.Format("{0,-10}{1,-10}{2,-10}{3,-10}", ProductNo, Name, Rate, Stock);
}
}
static void Main(string[] args)
{
List<Product> products = new List<Product>();
int choice = 0;
while (choice != 4)
{
Console.WriteLine("1. Adding New Product");
Console.WriteLine("2. Deleting Currently Searched Product");
Console.WriteLine("3. Searching Product");
Console.WriteLine("4. Exit");
Console.Write("Your choice: ");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
{
Product newProduct = new Product();
Console.Write("Enter the Product No: ");
newProduct.ProductNo = int.Parse(Console.ReadLine());
Console.Write("Enter the Product Name: ");
newProduct.Name = Console.ReadLine();
Console.Write("Enter the Product Rate: ");
newProduct.Rate = double.Parse(Console.ReadLine());
Console.Write("Enter the Product Stock: ");
newProduct.Stock = int.Parse(Console.ReadLine());
products.Add(newProduct);
//sort products
products.Sort(delegate(Product x, Product y)
{
return x.ProductNo.CompareTo(y.ProductNo);
});
}
break;
case 2:
{
Console.Write("Enter the Product No to delete: ");
int productNo = int.Parse(Console.ReadLine());
bool exist = false;
foreach (Product p in products)
{
if (p.ProductNo == productNo)
{
products.Remove(p);
exist = true;
break;
}
}
if (!exist)
{
Console.WriteLine("\nThe product does not exist!\n");
}
}
break;
case 3:
{
Console.Write("Enter the Product No to search: ");
int productNo = int.Parse(Console.ReadLine());
bool exist = false;
foreach (Product p in products)
{
if (p.ProductNo == productNo)
{
Console.WriteLine(p.ToString());
exist = true;
}
}
if (!exist)
{
Console.WriteLine("\nThe product does not exist!\n");
}
}
break;
case 4:
break;
default:
Console.WriteLine("Wrong menu item. Try again.");
break;
}
}
Console.ReadKey();
}
}
}
Comments
Leave a comment