示例#1
0
 private bool PriceIsValid()
 {
     try
     {
         double.Parse(ProductPriceTextBox.Text);
         return(true);
     }
     catch (Exception)
     {
         MessageBox.Show("Price must be a valid decimal number.");
         ProductPriceTextBox.Focus();
         return(false);
     }
 }
示例#2
0
        // ======================  END OF SEARCH FUNCTIONALITY METHODS  =========================


        /*
         * ======================  START OF INVENTORY MANAGEMENT METHODS  =========================
         * 1.AddProductButton_Click
         * 2.CategoryButton_Click
         * 3.ModifyButton_Click
         */

        /*
         * Event Handler for Adding New Product to Inventory
         */
        private void AddProductButton_Click(object sender, EventArgs e)
        {
            Product      Prod             = new Product();
            bool         DuplicateProduct = false;
            StreamWriter FileWriter;

            FileWriter = File.AppendText(PRODUCTDATABASEFILENAME);

            if (ProductNameTextBox.Text == "")
            {
                MessageBox.Show("Please enter New Product Name");
                ProductNameTextBox.Focus();
                ProductNameTextBox.SelectAll();
            }
            else
            {
                if (ProductCategoryListBox.SelectedIndex == -1)
                {
                    MessageBox.Show("Please select Product Category from the list");
                    ProductCategoryListBox.Focus();
                }
                else
                {
                    if (ProductPriceTextBox.Text == "")
                    {
                        MessageBox.Show("Please enter Product Price");
                        ProductPriceTextBox.Focus();
                        ProductPriceTextBox.SelectAll();
                    }
                    else
                    {
                        try
                        {
                            decimal.TryParse(ProductPriceTextBox.Text, out decimal ProductPrice);
                            if (ProductPrice <= 0)
                            {
                                MessageBox.Show("Product Price should be greater than Zero");
                                ProductPriceTextBox.Focus();
                                ProductPriceTextBox.SelectAll();
                            }
                            else
                            {
                                if (ProductQuantityTextBox.Text == "")
                                {
                                    MessageBox.Show("Please enter Product Quantity");
                                    ProductQuantityTextBox.Focus();
                                    ProductQuantityTextBox.SelectAll();
                                }
                                else
                                {
                                    try
                                    {
                                        int.TryParse(ProductQuantityTextBox.Text, out int ProductQuantity);
                                        if (ProductQuantity <= 0)
                                        {
                                            MessageBox.Show("Product Quantity should be greater than Zero");
                                            ProductQuantityTextBox.Focus();
                                            ProductQuantityTextBox.SelectAll();
                                        }
                                        else
                                        {
                                            foreach (Product p in ProductList)
                                            {
                                                if (p.ProductCategory == ProductCategoryListBox.SelectedItem.ToString() &&
                                                    p.ProductName == ProductNameTextBox.Text)
                                                {
                                                    MessageBox.Show("Same Product Already Exist in the same Category");
                                                    ProductNameTextBox.Focus();
                                                    ProductNameTextBox.SelectAll();
                                                    DuplicateProduct = true;
                                                    break;
                                                }
                                            }

                                            if (DuplicateProduct == false)
                                            {
                                                Random RandomNumberGenerator = new Random();

                                                int    ProductNumber  = RandomNumberGenerator.Next(1, 999999);
                                                string CheckProductID = ProductCategoryListBox.SelectedItem.ToString().Substring(0, 4) + ProductNumber.ToString("D6");
                                                while (ExistProductID_Check(CheckProductID))
                                                {
                                                    ProductNumber  = RandomNumberGenerator.Next(1, 999999);
                                                    CheckProductID = ProductCategoryListBox.SelectedItem.ToString().Substring(0, 4) + ProductNumber.ToString("D6");
                                                }
                                                Prod.ProductID = ProductCategoryListBox.SelectedItem.ToString().Substring(0, 4) + ProductNumber.ToString("D6");
                                                FileWriter.WriteLine(Prod.ProductID);
                                                Prod.ProductCategory = ProductCategoryListBox.SelectedItem.ToString();
                                                FileWriter.WriteLine(Prod.ProductCategory);
                                                Prod.ProductName = ProductNameTextBox.Text;
                                                FileWriter.WriteLine(Prod.ProductName);
                                                Prod.ProductPrice = decimal.Parse(ProductPriceTextBox.Text);
                                                FileWriter.WriteLine(Prod.ProductPrice);
                                                Prod.ProductQuantity = int.Parse(ProductQuantityTextBox.Text);
                                                FileWriter.WriteLine(Prod.ProductQuantity);
                                                ProductList.Add(Prod);
                                                FileWriter.Close();
                                                MessageBox.Show("Product '" + Prod.ProductName + "' Added Successfully to the Category '" + Prod.ProductCategory + "'");

                                                ProductCategoryListBox.SelectedIndex = -1;
                                                ProductNameTextBox.Text = ProductPriceTextBox.Text = ProductQuantityTextBox.Text = CheckProductID = "";
                                            }
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }