0% found this document useful (0 votes)
71 views9 pages

Add Product to Inventory System

The document describes a form for adding products to an inventory system. It includes a ProductClass that stores product details. The form loads available categories, collects product data on button click, validates the input, creates a new ProductClass instance, and adds it to a BindingSource to display in a data grid. Input is validated for name, quantity, and price formats before being added to the class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views9 pages

Add Product to Inventory System

The document describes a form for adding products to an inventory system. It includes a ProductClass that stores product details. The form loads available categories, collects product data on button click, validates the input, creates a new ProductClass instance, and adds it to a BindingSource to display in a data grid. Input is validated for name, quantity, and price formats before being added to the class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

BTN ADD PRODUCT

using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

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)
{
[Link](products);
}
}
private void btnAddProduct_Click(object sender, EventArgs e)
{
_ProductName = Product_Name([Link]);
_Category = [Link];
_MfgDate = [Link]("yyyy-MM-dd");
_ExpDate = [Link]("yyyy-MM-dd");
_Description = [Link];
_Quantity = Quantity([Link]);
_SellPrice = SellingPrice([Link]);
[Link](new ProductClass(_ProductName, _Category, _MfgDate, _ExpDate, _SellPrice, _Quantity,
_Description));
[Link] = [Link];
[Link] = 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 (![Link](name, @"^[a-zA-Z]+$"))
{
throw new StringFormatException(name);
}
}
catch (StringFormatException ex )
{
[Link]("String format input in product name." + [Link]);
}
finally
{
[Link]("Input string only product name");
}
return name;
}
public int Quantity(string qty)
{
try
{
if (![Link](qty, @"^[0-9]"))
{
throw new NumberFormatException(qty);
}
}
catch (NumberFormatException ex)
{
[Link]("Number format inpout in quantity," + [Link]);
}
return Convert.ToInt32(qty);
}
public double SellingPrice(string price)
{
try
{
if (![Link]([Link](), @"^(\d*\.)?\d+$"))
{
throw new CurrencyFormatException(price);
}
}
catch (CurrencyFormatException ex)
{
[Link]("Currency format input in price" + [Link]);
}
return [Link](price);
}
}
}

PRODUCT CLASS

using System;
using [Link];
using [Link];
using [Link];
using [Link];

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;
}
}

}
}

Common questions

Powered by AI

Exception handling for incorrect selling price inputs is implemented through the `SellingPrice` method, which checks inputs against a regex pattern appropriate for currency values. If the input doesn't match, a `CurrencyFormatException` is thrown, prompting an error message to the user. This safeguards against incorrect pricing formats and ensures data integrity .

Setting `gridViewProductList.AutoSizeColumnsMode` to `DataGridViewAutoSizeColumnsMode.Fill` ensures that all columns in the DataGridView control adjust their widths proportionally so that they completely fill the control. This enhances user interface design by making use of available space efficiently, improving readability and user experience .

The `showProductList` variable is a `BindingSource` object used to store and manage the collection of `ProductClass` objects. It acts as a bridge between the data source (products list) and data-bound controls (like DataGridView), allowing for dynamic display and updates of product information without manual data management .

The `ProductClass` design supports encapsulation by declaring its fields as private and providing public properties with getters and setters. This ensures the internal state of product objects is hidden from outside direct access while allowing controlled modifications via properties, maintaining the integrity and consistency of the data encapsulated within .

The handling of regular expressions plays a crucial role in ensuring data validity and robustness. By enforcing format rules on product names, quantities, and prices, the application prevents incorrect data inputs that could lead to runtime errors or database inconsistencies. This ultimately enhances system robustness by reducing vulnerabilities related to invalid data entries .

The application uses a regular expression to validate that the product name contains only alphabetic characters. If the name is invalid, a `StringFormatException` is thrown and a message is displayed to inform the user. This is enforced in the `Product_Name` method where a regex check ensures the name adheres to the specified format .

A missing `InitializeComponent` call in the `frmAddProduct` constructor would prevent the form control components from initializing, leading to a malfunction of the form UI. Controls would not be correctly instantiated or positioned, resulting in display errors or application crashes as the visual and interactive elements rely on this setup to function correctly .

The program uses a regular expression within the `Quantity` method to validate that the quantity consists only of numeric characters. If the input fails this pattern match, a `NumberFormatException` is thrown, prompting a message box alerting the user of a 'Number format input in quantity' issue .

The constructor, `frmAddProduct`, initializes the form component and sets up the `showProductList` as a new `BindingSource`. This enables the application to manage and bind lists and other collections to data-bound controls, such as a `DataGridView`, for displaying products within the form .

The application categorizes products using a predefined list of categories available in the `ListProductCategory` array. These categories include "Beverages," "Bread/Bakery," "Canned/Jarred Goods," "Dairy," "Frozen Foods," "Meat," "Personal Care," and "Other," which are added to a ComboBox for selection during product entry .

You might also like