You are on page 1of 9

BTN ADD PRODUCT

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Inventory04
{
public partial class Form1 : Form
{
private string _ProductName, _Category, _MfgDate, _ExpDate, _Description;
private int _Quantity;
private double _SellPrice;
BindingSource showProductList;

private void frmAddProduct_Load(object sender, EventArgs e)


{
string[] ListProductCategory = new string[] {"Beverages", "Bread/Bakery", "Canned/Jarred Goods",
"Dairy", "Frozen Foods", "Meat", "Personal Care", "Other"};
foreach (string products in ListOfProductCategory)
{
cbCategory.Items.Add(products);
}
}
private void btnAddProduct_Click(object sender, EventArgs e)
{
_ProductName = Product_Name(txtProductName.Text);
_Category = cbCategory.Text;
_MfgDate = dtPickerMfgDate.Value.ToString("yyyy-MM-dd");
_ExpDate = dtPickerExpDate.Value.ToString("yyyy-MM-dd");
_Description = richTxtDescription.Text;
_Quantity = Quantity(txtQuantity.Text);
_SellPrice = SellingPrice(txtSellPrice.Text);
showProductList.Add(new ProductClass(_ProductName, _Category, _MfgDate, _ExpDate, _SellPrice, _Quantity,
_Description));
gridViewProductList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
gridViewProductList.DataSource = showProductList;
}
public frmAddProduct()
{
InitializeComponent()
showProductList = new BindingSource();
}
class NumberFormatException : Exception
{
public NumberFormatException(string quantity) : base(quantity) { }
}
class StringFormatException : Exception
{
public StringFormatException(string name) : base(name) { }
}
private void label15_Click(object sender, EventArgs e)
{

class CurrencyFormatException : Exception


{
public CurrencyFormatException(string price) : base(price) { }
}
public string Product_Name(string name)
{
try
{
if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
{
throw new StringFormatException(name);
}
}
catch (StringFormatException ex )
{
MessageBox.Show("String format input in product name." + ex.Message);
}
finally
{
Console.WriteLine("Input string only product name");
}
return name;
}
public int Quantity(string qty)
{
try
{
if (!Regex.IsMatch(qty, @"^[0-9]"))
{
throw new NumberFormatException(qty);
}
}
catch (NumberFormatException ex)
{
MessageBox.Show("Number format inpout in quantity," + ex.Message);
}
return Convert.ToInt32(qty);
}
public double SellingPrice(string price)
{
try
{
if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
{
throw new CurrencyFormatException(price);
}
}
catch (CurrencyFormatException ex)
{
MessageBox.Show("Currency format input in price" + ex.Message);
}
return Convert.ToDouble(price);
}
}
}

PRODUCT CLASS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inventory03
{
class ProductClass
{
private int _Quantity;
private double _SellingPrice;
private string _ProductName, _Category, _ManufacturingDate, _ExpirationDate, _Description;
public ProductClass(string ProductName, string Category, string MfgDate, string ExpDate, double Price, int
Quantity, string Description)
{
this._Quantity = Quantity;
this._SellingPrice = Price;
this._ProductName = ProductName;
this._Category = Category;
this._ManufacturingDate = MfgDate;
this._ExpirationDate = ExpDate;
this._Description = Description;
}
public string productName
{
get
{
return this._ProductName;
}
set
{
this._ProductName = value;
}
}
public string category
{
get
{
return this._Category;
}
set
{
this._Category = value;
}
}
public string manufacturingDate
{
get
{
return this._ManufacturingDate;
}
set
{
this._ManufacturingDate = value;
}
}
public string expirationDate
{
get
{
return this._ExpirationDate;
}
set
{
this._ExpirationDate = value;
}
}
public string description
{
get
{
return this._Description;
}
set
{
this._Description = value;
}
}
public int quantity
{
get
{
return this._Quantity;
}
set
{
this._Quantity = value;
}
}
public double sellingPrice
{
get
{
return this._SellingPrice;
}
set
{
this._SellingPrice = value;
}
}

}
}

You might also like