Está en la página 1de 9

Excepciones en el desarrollo de aplicaciones

Eduardo Martínez V.

Programación .Net 2

Instituto IACC

17-10-2021
Desarrollo

1. De acuerdo a lo solicitado, se adjuntan imágenes de desarrollo realizado


Diseño de formulario
Codigo

Resto de manejo de excepciones

namespace Excepciones
{
public partial class Inmobiliaria : Form
{

public List<string> ubicaciones = new List<string> { "N", "S", "E", "O",


"NE", "NO", "SE", "SO" };
public List<string> vocales = new List<string> { "A", "E", "I", "O", "U" };
public Dictionary<int, string> consonantes;
public Inmobiliaria()
{
InitializeComponent();
cargarConsonantes();
}

private void cargarConsonantes()


{
consonantes = new Dictionary<int, string>();
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 <= 0)
throw new Exception($"{origen} debe ser positivo");
}
else
throw new Exception("Debe ingresar valor uf");

private void MostrarResumen()


{

try
{
decimal valorUF = decimal.Parse(txtValorUf.Text);
decimal valorDepaUF = decimal.Parse(txtValorDepa.Text);
decimal valorPesos = valorDepaUF * valorUF;

string resumen = "\r\n"


+ $"Valor UF: {txtValorUf.Text} \r\n"
+ $"Codigo: {txtCodigo.Text.ToUpper()} \r\n"
+ $"Ubicacion: {txtUbicacion.Text} \r\n"
+ $"N° Dormitorios: {txtDormitorios.Text} \r\n"
+ $"N° Baños: {txtBanos.Text} \r\n"
+ $"Valor en UF :{txtValorDepa.Text} \r\n"
+ "********************* \r\n"
+ $"Valor Departamento en PESOS: {valorPesos}";

Info informe = new Info(resumen);


informe.Show();

}
catch (Exception ex)
{

MessageBox.Show($"ERROR EN CALCULO REPORTE FINAL {ex.Message}",


"REPORTE");
}

}
}
}

Salida

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

namespace Excepciones
{
public partial class Info : Form
{
public Info(string resumen)
{
InitializeComponent();
txtResumen.Text = resumen;
}
}
}

También podría gustarte