Exemple #1
0
        private void saveButton_Click(object sender, EventArgs e)
        {
            //if (HasSpecialChars(priceInput.Text) is true)
            //{
            //    buttonSave.Enabled = false;
            //    priceInput.BackColor = Color.Coral;
            //    MessageBox.Show("The price input field must be in USD Currency Format and cannot contain any alpha or special characters");
            //    return;
            //}
            if (inventoryInput.Text == "")
            {
                // If the value in the numeric updown is an empty string, replace with 0.
                inventoryInput.Text = "0";
            }

            bool IV = InputValidation();

            if (IV is false)
            {
                MessageBox.Show("Please check that all fields are completed correctly and try again.");
                return;
            }
            this.Hide();

            // if its blank prompt user
            if (String.IsNullOrWhiteSpace(nameInput.Text) ||
                String.IsNullOrWhiteSpace(Convert.ToString(inventoryInput.Value)) ||
                String.IsNullOrWhiteSpace(priceInput.Text) ||
                String.IsNullOrWhiteSpace(minInput.Text) ||
                String.IsNullOrWhiteSpace(maxInput.Text) ||
                String.IsNullOrEmpty(nameInput.Text) ||
                String.IsNullOrEmpty(Convert.ToString(inventoryInput.Value)) ||
                String.IsNullOrEmpty(priceInput.Text) ||
                String.IsNullOrEmpty(minInput.Text) ||
                String.IsNullOrEmpty(maxInput.Text))
            {
                MessageBox.Show("Please fill out all fields. May not be empty.");
                InputValidation();
                return;
            }

            if (Convert.ToInt32(maxInput.Text) < Convert.ToInt32(minInput.Text))
            {
                MessageBox.Show("Max cannot be less than Min");
                this.Show();
                return;
            }
            else if (Enumerable.Range(Convert.ToInt32(minInput.Text), Convert.ToInt32(maxInput.Text)).Contains(Convert.ToInt32(inventoryInput.Text)) == false)
            {
                MessageBox.Show("Inventory must be between Min and Max values.");
                this.Show();
                return;
            }



            if (associatedPartsDGV.RowCount == 0 && modifyOrNew == 0) // and product is new
            {
                MessageBox.Show("Product must contain atleast one associated part.");
                this.Show();

                return;
            }

            else if (Convert.ToInt32(maxInput.Text) < Convert.ToInt32(minInput.Text))
            {
                MessageBox.Show("Max cannot be less than Min");
                this.Show();

                return;
            }


            else
            {
                Product updatedProduct = new Product(Convert.ToInt32(IDinput.Text), nameInput.Text, Convert.ToDouble(priceInput.Text.Replace("$", "").Replace("$", "")), Convert.ToInt32(inventoryInput.Text), Convert.ToInt32(minInput.Text), Convert.ToInt32(maxInput.Text));

                //foreach (Classes.Part part in DGVAssoParts)
                //{
                //    updatedProduct.AssociatedParts.Add(part);
                //}
                //   Classes.Product productObject = Classes.Inventory.lookupProduct(Convert.ToInt32(IDinput.Text));
                foreach (Classes.Part part in DGVAssoParts)
                {
                    Debug.WriteLine(part.Name);
                    updatedProduct.AddAssociatedPart(part);
                    Debug.WriteLine("Product name" + updatedProduct.Name);
                    foreach (Classes.Part x in updatedProduct.AssociatedParts)
                    {
                        Debug.WriteLine(x.Name);
                    }

                    // updatedProduct.AssociatedParts.Add(part);
                }
                Inventory.updateProduct(Convert.ToInt32(IDinput.Text), updatedProduct);

                if (Convert.ToInt32(maxInput.Text) < Convert.ToInt32(minInput.Text))
                {
                    MessageBox.Show("Your minimum exceeds your maximum.");
                }
                else if (modifyOrNew == 1) //modify
                {
                    //lookup the newly made product make a product object
                    Product thatNewProd = Inventory.lookupProduct(Convert.ToInt32(IDinput.Text));
                    //update that objects properties

                    //{
                    //    if (each.ProductID == Convert.ToInt32(IDinput.Text)) //was productID?
                    //    {
                    thatNewProd.Name    = nameInput.Text;
                    thatNewProd.Price   = Convert.ToDouble(priceInput.Text.Replace("$", ""));
                    thatNewProd.InStock = Convert.ToInt32(inventoryInput.Text);
                    thatNewProd.Min     = Convert.ToInt32(minInput.Text);
                    thatNewProd.Max     = Convert.ToInt32(maxInput.Text);

                    foreach (Classes.Part part in holdingAreaParts)
                    {
                        Classes.Product workingProduct = Inventory.lookupProduct(Convert.ToInt32(IDinput.Text));
                        workingProduct.AddAssociatedPart(part);
                    }
                    //    }
                    //}
                    //this.Hide();

                    Main main = new Main();
                    main.ShowDialog();
                    return;
                }


                else
                {
                    Inventory.addProduct(new Product(
                                             nameInput.Text,
                                             Convert.ToDouble(priceInput.Text.Replace("$", "")),
                                             Convert.ToInt32(inventoryInput.Text),
                                             Convert.ToInt32(minInput.Text),
                                             Convert.ToInt32(maxInput.Text)));
                    //commit parts from holding area
                    foreach (Classes.Part part in holdingAreaParts)
                    {
                        Classes.Product workingProduct = Inventory.lookupProduct(Convert.ToInt32(IDinput.Text));
                        workingProduct.AddAssociatedPart(part);
                    }

                    this.Close();

                    Main main = new Main();
                    main.Show();
                }
            }
        }