LabExer2

 avatar
unknown
csharp
4 years ago
4.2 kB
7
Indexable
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Laboratory.Forms
{
    public partial class frmAddProduct : Form
    {
        BindingSource showProductList;
        private string _ProductName, _Category, _MfgDate, _ExpDate, _Description;
        private int _Quantity;
        private double _SellPrice;

        public string Product_Name(string name)
        {
            try
            {
                if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
                {
                    throw new StringFormatExeption(name);
                }

            }
            catch (StringFormatExeption e)
            {
                MessageBox.Show("Invalid input in Product Name" + e.Message);
            }
            finally
            {
                Console.WriteLine("Please input string in Product Name");
            }
            return name;
        }
        public int Quantity(string qty)
        {
            try
            {
                if (!Regex.IsMatch(qty, @"^[0-9]"))
                {
                    throw new NumberFormatExeption(qty);
                }
                else if (qty == "")
                {
                    throw new FormatException(qty);
                }
            }
            catch (NumberFormatExeption e)
            {
                Console.WriteLine("Invalid input in Quantity" + e.Message);
            }
            catch (FormatException c)
            {
                Console.WriteLine("Invalid input in Quantity" + c.Message);
            }
            return Convert.ToInt32(qty);
        }

        private void btnAddProduct_Click_1(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;
        }

        private void frmAddProduct_Load_1(object sender, EventArgs e)
        {
            string[] ListOfProductCategory = new string[]
            {
                "Beverages",
                "Bread/Bakery",
                "Canned/Jarred Goods",
                "Dairy",
                "Frozen Goods",
                "Meat",
                "Personal Care",
                "Other"
            };
            foreach (var n in ListOfProductCategory)
            {
                cbCategory.Items.Add(n);
            }
        }

        public double SellingPrice(string price)
        {
            try
            {
                if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
                {
                    throw new CurrencyFormatExeption(price);
                }
            }
            catch (CurrencyFormatExeption e)
            {
                MessageBox.Show("Invalid input in Price" + e.Message);
            }
            return Convert.ToDouble(price);
        }

        public frmAddProduct()
        {
            InitializeComponent();
            showProductList = new BindingSource();
        }
    }

    class NumberFormatExeption : Exception
    {
        public NumberFormatExeption(string quantity) : base(quantity) { }
    }
    class StringFormatExeption : Exception
    {
        public StringFormatExeption(string name) : base(name) { }
    }
    class CurrencyFormatExeption : Exception
    {
        public CurrencyFormatExeption(string price) : base(price) { }
    }
    class FormatExeption : Exception
    {
        public FormatExeption(string quantity) : base(quantity) { }
    }

}
Editor is loading...