Control 4 .Net II

Excepciones en el desarrollo de aplicaciones Javier Serrano Programación .NET II Instituto IACC 07-07-2019 Desarrollo

Views 55 Downloads 0 File size 183KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

Excepciones en el desarrollo de aplicaciones Javier Serrano Programación .NET II Instituto IACC 07-07-2019

Desarrollo Ejecución.

Validación de UF.

Validación de Codigo (ejemplo de algunas de las validaciones solicitadas.)

Validación Ubicación

Validación Dormitorios.

Validación Baños

Cargar Foto

Resumen final.

Codigo Fuente Form principal. using using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Threading.Tasks; System.Windows.Forms;

namespace Excepciones { public partial class Inmobiliaria : Form { public List ubicaciones = new List { "N", "S", "E", "O", "NE", "NO", "SE", "SO" }; public List vocales = new List { "A", "E", "I", "O", "U" }; public Dictionary consonantes; public Inmobiliaria() { InitializeComponent(); cargarConsonantes(); } private void cargarConsonantes() { consonantes = new Dictionary(); consonantes.Add(1, "B"); consonantes.Add(2, "C"); consonantes.Add(3, "D"); consonantes.Add(4, "F"); consonantes.Add(5, "G"); consonantes.Add(6, "H"); consonantes.Add(7, "J"); consonantes.Add(8, "K"); consonantes.Add(9, "L"); consonantes.Add(10, "M"); consonantes.Add(11, "N"); consonantes.Add(12, "P"); consonantes.Add(13, "Q"); consonantes.Add(14, "R"); consonantes.Add(15, "S"); consonantes.Add(16, "T"); consonantes.Add(17, "V"); consonantes.Add(18, "W"); consonantes.Add(19, "X"); consonantes.Add(20, "Y"); consonantes.Add(21, "Z"); } private void BtnCargarImg_Click(object sender, EventArgs e) {

OpenFileDialog op = new OpenFileDialog(); op.Filter = "Archivo JPG |*.jpg;*.jpeg" + "|Archivo PNG|*.png"; if (op.ShowDialog() == DialogResult.OK) { string imagen = op.FileName; pbFoto.Image = Image.FromFile(imagen); } } private void BtnValidar_Click(object sender, EventArgs e) { int count = 0; try { validarUf(txtValorUf.Text, "El valor de la uf"); if (!string.IsNullOrEmpty(txtCodigo.Text)) { int largo = txtCodigo.Text.Length; if (largo == 5) { validaLetras(txtCodigo.Text.Substring(0, 2)); validaNumeros(txtCodigo.Text.Substring(2, 3)); } else throw new Exception("El codigo debe ser de largo 5, 2 letras y 3 numeros."); } else throw new Exception("Debe ingresar Codigo"); if (!string.IsNullOrEmpty(txtUbicacion.Text)) { string ubicacion = txtUbicacion.Text.Trim().ToUpper(); //if (ubicaciones.FirstOrDefault(f => f.Contains(ubicacion)) == null) if (!ubicaciones.Contains(ubicacion)) throw new Exception("En ubicacion se permiten los siguientes valores: N, S, E, O, NE, NO, SE, SO."); } else throw new Exception("Debe ingresar Ubicacion");

if (!string.IsNullOrEmpty(txtDormitorios.Text)) { int dormitorios = Convert.ToInt32(txtDormitorios.Text); if (dormitorios < 1 || dormitorios >= 4) throw new Exception("El numero de dormitorios debe estar dentro del rango 1-4"); } else throw new Exception("Debe ingresar numero de dormitorios"); if (!string.IsNullOrEmpty(txtBanos.Text)) { int banos = Convert.ToInt32(txtBanos.Text);

if (banos < 1 || banos > 3) throw new Exception("El numero de baños debe estar dentro del rango 1-2"); } else throw new Exception("Debe ingresar numero de dormitorios");

validarUf(txtValorDepa.Text, "El valor en uf");

} catch (FormatException ex) { count++; MessageBox.Show($"ERROR:{ex.Message}", "FormatException"); } catch (Exception ex) { count++; MessageBox.Show($"ERROR:{ex.Message}", "Exception"); } finally { if (count == 0) { MessageBox.Show("Sin Errores"); MostrarResumen(); } else MessageBox.Show("Se presentaron errores de validacion"); } } private void validaNumeros(string numeros) { try { int pos1 = int.Parse(numeros.Substring(0, 1)); int pos2 = int.Parse(numeros.Substring(1, 1)); int pos3 = int.Parse(numeros.Substring(2, 1)); if (pos1 == pos2 || pos2 == pos3 || pos3 == pos1) throw new Exception("no pueden haber 2 o más números repetidos"); int sumatoria = pos1 + pos2 + pos3; if (sumatoria < 6 && sumatoria > 22) throw new Exception("La suma de los 3 debe ser mayor a 6 y menor a 23"); if (pos1 == 0) throw new Exception("El primero no puede ser cero"); } catch (Exception ex) { throw new Exception($"Valida codigo numeros {ex.Message}");

}

} private void validaLetras(string letras) { try { string let1 = letras.Substring(0, 1).ToUpper(); string let2 = letras.Substring(1, 1).ToUpper(); if (vocales.Contains(let1) || vocales.Contains(let2)) throw new Exception("No pueden haber vocales"); if (let1 == let2) throw new Exception("No puede haber 2 consonantes iguales");

int ord1 = consonantes.FirstOrDefault(w => w.Value == let1).Key; int ord2 = consonantes.FirstOrDefault(w => w.Value == let2).Key; if (ord1 > ord2) throw new Exception("Deben respetar el orden alfabético"); } catch (Exception ex) { throw new Exception($"Valida codigo letras {ex.Message}"); } } private void validarUf(string valor, string origen) { if (!string.IsNullOrEmpty(valor)) { decimal valorUf = Convert.ToDecimal(valor); if (valorUf