Skip to content

Instantly share code, notes, and snippets.

@abhishekluv
Created April 3, 2020 17:42
Show Gist options
  • Save abhishekluv/5a24b5b5a9f51a8ec448bf53f9616b75 to your computer and use it in GitHub Desktop.
Save abhishekluv/5a24b5b5a9f51a8ec448bf53f9616b75 to your computer and use it in GitHub Desktop.
Doubt
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
internal class Program
{
private static List<Product> list = new List<Product>();
private static void Main(string[] args)
{
while (true)
{
Console.WriteLine("menu:");
Console.WriteLine("1. add");
Console.WriteLine("2. addchange units and price by id");
Console.WriteLine("3. delete");
Console.WriteLine("4. view");
Console.WriteLine("5.search products");
Console.WriteLine("--------------------");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Add();
break;
case 2:
change(choice);
break;
case 3:
delete();
break;
case 4:
view();
break;
case 5:
Product product = search();
if (product != null)
{
Console.WriteLine($"Id for Product: {product.name} is {product.id}");
}
else
{
Console.WriteLine("Invalid Product Name");
}
break;
}
}
}
public static void Add()
{
Console.WriteLine("enter name,id,units,price");
string name = Console.ReadLine();
int id = Convert.ToInt32(Console.ReadLine());
int units = Convert.ToInt32(Console.ReadLine());
double price = Convert.ToDouble(Console.ReadLine());
Product p = new Product(id, name, units, price);
list.Add(p);
}
public static void change(int id)
{
Console.WriteLine("enter id");
int id1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter new unit and price");
int u = Convert.ToInt32(Console.ReadLine());
double price = Convert.ToDouble(Console.ReadLine());
Product productToUpdate = list.Where(x => x.id == id1).FirstOrDefault();
productToUpdate.price = price;
productToUpdate.unitinstock = u;
}
public static void delete()
{
Console.WriteLine("enter id to be deleted:");
int id = Convert.ToInt32(Console.ReadLine());
Product productToDelete = list.Where(x => x.id == id).FirstOrDefault();
list.Remove(productToDelete);
Console.WriteLine($"{productToDelete} removed");
}
public static void view()
{
if (list.Any())
{
foreach (Product p in list)
{
Console.WriteLine($"Showing Product Details: {p.name},{p.id},{p.price}, {p.unitinstock}");
}
}
else
{
Console.WriteLine("--No data in list--");
}
}
public static Product search()
{
Console.WriteLine("enter the name of product to be searched");
string name = Console.ReadLine();
Product searchProduct = list.Where(x => x.name == name).FirstOrDefault();
return searchProduct;
}
}
public class Product
{
public int id;
public string name;
public int unitinstock;
public double price;
public Product(int id, string name, int unit, double price)
{
this.id = id;
this.name = name;
unitinstock = unit;
this.price = price;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment