Ejemplo n.º 1
0
        /// <summary>
        /// Crea un objeto de tipo Producto y lo agrega a la base de datos
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCargar_Click(object sender, EventArgs e)
        {
            try
            {
                int precio;
                int stock;

                this.txtPrecio.Text = this.txtPrecio.Text.Replace(".", ",");

                if (int.TryParse(this.txtPrecio.Text, out precio) && int.TryParse(this.txtStock.Text, out stock))
                {
                    if (precio >= 0 && stock >= 0)
                    {
                        if ((Armamento.ETipo) this.cmbTipo.SelectedValue == Armamento.ETipo.fusil)
                        {
                            a = new Fusil(this.txtNombre.Text, Armamento.ETipo.fusil, this.txtOrigen.Text, precio, stock);

                            if (Inventario.Armas + a)
                            {
                                MessageBox.Show("Cargado con exito");
                                MessageBox.Show(a.ToString());
                            }
                        }
                        else if ((Armamento.ETipo) this.cmbTipo.SelectedValue == Armamento.ETipo.pistola)
                        {
                            a = new Pistola(this.txtNombre.Text, Armamento.ETipo.pistola, this.txtOrigen.Text, precio, stock);

                            if (Inventario.Armas + a)
                            {
                                MessageBox.Show("Cargado con exito");
                                MessageBox.Show(a.ToString());
                            }
                        }
                        else if ((Armamento.ETipo) this.cmbTipo.SelectedValue == Armamento.ETipo.dron)
                        {
                            a = new Dron(this.txtNombre.Text, Armamento.ETipo.dron, this.txtOrigen.Text, precio, stock);

                            if (Inventario.Armas + a)
                            {
                                MessageBox.Show("Cargado con exito");
                                MessageBox.Show(a.ToString());
                            }
                        }
                        else if ((Armamento.ETipo) this.cmbTipo.SelectedValue == Armamento.ETipo.explosivo)
                        {
                            a = new Explosivo(this.txtNombre.Text, Armamento.ETipo.explosivo, this.txtOrigen.Text, precio, stock);

                            if (Inventario.Armas + a)
                            {
                                MessageBox.Show("Cargado con exito");
                                MessageBox.Show(a.ToString());
                            }
                        }
                        this.evento.Invoke();
                    }
                    else
                    {
                        if (int.Parse(this.txtPrecio.Text) < 0)
                        {
                            MessageBox.Show("Error: El precio no puede ser negativo.");
                            this.txtPrecio.Clear();
                        }
                        else
                        {
                            MessageBox.Show("Error: El stock no puede ser negativo.");
                            this.txtStock.Clear();
                        }
                    }
                }
                else
                {
                    if (int.TryParse(this.txtPrecio.Text, out precio))
                    {
                        MessageBox.Show("Error: Verifique que el stock sea un número.");
                        this.txtStock.Clear();
                    }
                    else
                    {
                        MessageBox.Show("Error: Verifique que el precio sea un número.");
                        this.txtPrecio.Clear();
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                this.evento.Invoke();
            }
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }