Inventory Form

Manually Throwing Custom Exception Laboratory Exercise 2
 avatar
JohnDiaz
csharp
4 years ago
4.3 kB
8
Indexable
//Inventory Form or Add Product Source Code

using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;

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

        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) { }
        }

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

        private void frmAddProduct_Load(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 frmAddProduct()
        {
            InitializeComponent();
            showProductList = new BindingSource();
        }

        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 = richTextDescription.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;
        }
    }
}
Editor is loading...