ADD PRODUCT/INVENTORY

 avatar
unknown
csharp
4 years ago
6.3 kB
9
Indexable
using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using FireSharp.Config;
using FireSharp.Interfaces;

namespace CamposanoCRUDFirebaseFinalTask.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(MessageBox.Show("Quantity field is empty").ToString());
                }
            }
            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)
        {
            try
            {
                client = new FireSharp.FirebaseClient(conf);
                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);
                }
            }
            catch
            {
                MessageBox.Show("Firebase Connection Problem");
            }
            
        }

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

        IFirebaseConfig conf = new FirebaseConfig
        {
            AuthSecret = "QJ3yfSs4flmlH1iB9FyIJvu9DjfJ2t4cKjQwovck",
            BasePath = "https://crudfirebase-2b60a-default-rtdb.firebaseio.com/"
        }; IFirebaseClient client;

        private void ClearFields()
        {
            txtProductName.Text = "";
            cbCategory.SelectedItem = null;
            dtPickerMfgDate.Value = new DateTime(2021, 11, 12);
            dtPickerExpDate.Value = new DateTime(2021, 11, 12);
            txtQuantity.Text = "";
            txtSellPrice.Text = "";
            richTextDescription.Text = "";
        }

        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show(this, "Do you want to add this product to the database?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == DialogResult.Yes)
            {
                StudentInformationClass addprdct = new StudentInformationClass
                {
                    Product = txtProductName.Text,
                    Category = cbCategory.Text,
                    MfgDate = dtPickerMfgDate.Value.ToString("yyyy-MM-dd"),
                    ExpDate = dtPickerExpDate.Value.ToString("yyyy-MM-dd"),
                    Qty = txtQuantity.Text,
                    SellDate = txtSellPrice.Text
                };
                var inputData = client.Set("Products/" + txtProductName.Text, addprdct);
                MessageBox.Show("Product successfully Added", "System", MessageBoxButtons.OK, MessageBoxIcon.Information);

                _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;
                ClearFields();
            }
        }
    }
}
Editor is loading...