Created
June 22, 2016 09:21
-
-
Save mimukit/24c3519bbd4908275d1db1183da16e58 to your computer and use it in GitHub Desktop.
Basic Product Management Program by M MUKIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
namespace BasicProductManagement | |
{ | |
class Product | |
{ | |
private static ArrayList arrItems = new ArrayList(); | |
private string ProductID; | |
private double ProductStock; | |
private static int count = 1; | |
private string ProductIDGenerator() | |
{ | |
DateTime d = DateTime.Now; | |
string id = d.Day.ToString() + "." + d.Month.ToString("D2") + "." + count; | |
count++; | |
return id; | |
} | |
public Product() | |
{ | |
this.ProductID = ProductIDGenerator(); | |
this.ProductStock = 0; | |
arrItems.Add(this); | |
} | |
public Product(double quantity) | |
{ | |
this.ProductID = ProductIDGenerator(); | |
this.ProductStock = quantity; | |
arrItems.Add(this); | |
} | |
public bool Stock(string ProductID, double quantity) | |
{ | |
foreach (Product item in arrItems) | |
{ | |
if (item.ProductID == ProductID) | |
{ | |
item.ProductStock += quantity; | |
return true; | |
} | |
} | |
return false; | |
} | |
public bool Sell(string ProductID, double quantity) | |
{ | |
foreach (Product item in arrItems) | |
{ | |
if (item.ProductID == ProductID && quantity <= item.ProductStock) | |
{ | |
item.ProductStock -= quantity; | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void showProductInformation(string ProductID) | |
{ | |
bool flag = false; | |
foreach (Product item in arrItems) | |
{ | |
if (item.ProductID == ProductID) | |
{ | |
Console.WriteLine("\nProduct Details:"); | |
Console.WriteLine("================"); | |
Console.WriteLine("\nProduct ID\tStock"); | |
Console.WriteLine("==========\t\t====="); | |
Console.WriteLine(item.ProductID + "\t\t\t" + item.ProductStock); | |
flag = true; | |
} | |
} | |
if (!flag) | |
{ | |
Console.WriteLine("No product found with id: " + ProductID); | |
} | |
} | |
public static void showAllProductInfo() | |
{ | |
Console.WriteLine("\nProduct Details:"); | |
Console.WriteLine("================"); | |
Console.WriteLine("\nProduct ID\t\tStock"); | |
Console.WriteLine("==========\t\t====="); | |
if (arrItems.Count > 0) | |
{ | |
foreach (Product item in arrItems) | |
Console.WriteLine(item.ProductID + "\t\t\t" + item.ProductStock); | |
} | |
else | |
{ | |
Console.WriteLine("No product found...!"); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// No product in list | |
Product.showAllProductInfo(); | |
// Create products | |
Product p1 = new Product(); | |
Product p2 = new Product(100); | |
// Show all info | |
Product.showAllProductInfo(); | |
// Add stock | |
if (p1.Stock("22.06.1", 50)) | |
{ | |
Console.WriteLine("Stock added successfully...!"); | |
} | |
else | |
{ | |
Console.WriteLine("Error: stock cann't be added. Check product ID or quantity."); | |
} | |
// Single product information | |
Product.showProductInformation("22.06.1"); | |
// Sell product | |
if (p1.Sell("22.06.2", 15)) | |
{ | |
Console.WriteLine("Product sold successfully...!"); | |
} | |
else | |
{ | |
Console.WriteLine("Error: product cann't be sold. Check product ID or quantity."); | |
} | |
// Show all info | |
Product.showAllProductInfo(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment