Aseguradora finalizado alvarinho
C sharp
11.763 visualizaciones desde el 10 de Julio del 2018
código basado en una aseguradora que puede ser de vida o de vehiculos donde capturara y mostrara la información usando herencias y linq.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Biblioteca
{
public enum TipoSeguro
{
NoSeleccionado, Vida, Vehiculo
}
public enum TipoSexo
{
NoSeleccionado, Hombre, Mujer, Otro
}
}
/////////////////////////7
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Biblioteca
{
interface ISeguro
{
float CalcularPrimaMensual();
bool EsRiesgoso { get; }
}
}
///////////////////////77
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Biblioteca;
namespace Biblioteca
{
public class SeguroColleccion: List<SeguroGeneral>
{
public int ObtenerCantidadClientesMayoresDe50Years()
{
int cantidad = this.Where(r => r is SeguroVida).Count(r => (r as SeguroVida).Edad > 50);
return cantidad;
}
public int ObtenerCantidadClientesRiesgosos()
{
int cantidad =
this.Where(r => r is SeguroVida).Count(r => (r as SeguroVida).CalcularPrimaMensual() > 50000)
+ this.Where(r => r is SeguroVehiculo).Count(r => (r as SeguroVehiculo).CalcularPrimaMensual() > 50000);
return cantidad;
}
public List<string> ObtenerClientesConMenorPrima()
{
float minVida = 0;
float minVehiculo = 0;
List<SeguroGeneral> listVida = this.Where(r => r is SeguroVida).ToList();
List<SeguroGeneral> listVehiculo = this.Where(r => r is SeguroVehiculo).ToList();
if (listVida.Count > 0)
minVida = this.Where(r => r is SeguroVida).Min(r => (r as SeguroVida).CalcularPrimaMensual());
if (listVehiculo.Count > 0)
minVehiculo = this.Where(r => r is SeguroVehiculo).Min(r => (r as SeguroVehiculo).CalcularPrimaMensual());
else
minVehiculo = minVida;
float min = minVida < minVehiculo ? minVida : minVehiculo;
List<string> listNomVida = new List<string>();
if (listVida.Count > 0)
listNomVida =
this.Where(r => r is SeguroVida)
.Where(r => (r as SeguroVida).CalcularPrimaMensual() == min)
.Select(r => r.NombreCliente).ToList();
List<string> listNomVehiculo = new List<string>();
if (listVehiculo.Count > 0)
listNomVehiculo =
this.Where(r => r is SeguroVehiculo)
.Where(r => (r as SeguroVehiculo).CalcularPrimaMensual() == min)
.Select(r => r.NombreCliente).ToList();
return listNomVida.Union(listNomVehiculo).ToList();
}
public List<string> ObtenerClientesConMayorPrima()
{
float maxVida = 0;
float maxVehiculo = 0;
List<SeguroGeneral> listVida = this.Where(r => r is SeguroVida).ToList();
List<SeguroGeneral> listVehiculo = this.Where(r => r is SeguroVehiculo).ToList();
if (listVida.Count > 0)
maxVida = this.Where(r => r is SeguroVida).Max(r => (r as SeguroVida).CalcularPrimaMensual());
if (listVehiculo.Count > 0)
maxVehiculo = this.Where(r => r is SeguroVehiculo).Max(r => (r as SeguroVehiculo).CalcularPrimaMensual());
float max = maxVida > maxVehiculo ? maxVida : maxVehiculo;
List<string> listNomVida = new List<string>();
if (listVida.Count > 0)
listNomVida =
this.Where(r => r is SeguroVida)
.Where(r => (r as SeguroVida).CalcularPrimaMensual() == max)
.Select(r => r.NombreCliente).ToList();
List<string> listNomVehiculo = new List<string>();
if (listVehiculo.Count > 0)
listNomVehiculo =
this.Where(r => r is SeguroVehiculo)
.Where(r => (r as SeguroVehiculo).CalcularPrimaMensual() == max)
.Select(r => r.NombreCliente).ToList();
return listNomVida.Union(listNomVehiculo).ToList();
}
}
}
////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Biblioteca
{
public class SeguroGeneral
{
private static int _correlativo = 0;
private int _id;
private string _nombreSeguro;
private TipoSeguro _tipo;
private DateTime _fechaInicio;
private string _nombreCliente;
protected string NombreSeguro
{
get
{
return _nombreSeguro;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("El nombre del seguro no puede ser vacío");
}
_nombreSeguro = value;
}
}
protected TipoSeguro Tipo
{
get
{
return _tipo;
}
set
{
if (value == TipoSeguro.NoSeleccionado)
{
throw new ArgumentOutOfRangeException("El tipo de seguro debe ser de vida o de vehículo");
}
_tipo = value;
}
}
public DateTime FechaInicio
{
get
{
return _fechaInicio;
}
set
{
if (value <= DateTime.Today)
{
throw new ArgumentOutOfRangeException("Los seguros deben iniciarse desde una fecha posterior a la actual.");
}
_fechaInicio = value;
}
}
public int Id
{
get
{
return _id;
}
}
public string NombreCliente
{
get
{
return _nombreCliente;
}
set
{
if (string.IsNullOrEmpty(_nombreCliente))
{
throw new ArgumentOutOfRangeException("El nombre de cliente no puede estar vacío");
}
_nombreCliente = value;
}
}
public SeguroGeneral()
{
this.Init();
}
private void Init()
{
_correlativo++;
this._id = _correlativo;
this._nombreSeguro = string.Empty;
this._tipo = TipoSeguro.NoSeleccionado;
this.FechaInicio = DateTime.Today.AddDays(1);
this._nombreCliente = string.Empty;
}
public SeguroGeneral(DateTime fechaInicio, string nombreCliente)
{
_correlativo++;
this._id = _correlativo;
this._nombreSeguro = "";
this._tipo = TipoSeguro.NoSeleccionado;
this.FechaInicio = fechaInicio;
this._nombreCliente = nombreCliente;
}
public string ObtenerDatos()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("ID: {0}", Id);
sb.AppendLine();
sb.AppendFormat("NombreSeguro: {0}", NombreSeguro);
sb.AppendLine();
sb.AppendFormat("Tipo: {0}", Tipo);
sb.AppendLine();
sb.AppendFormat("FechaInicio: {0}", FechaInicio);
sb.AppendLine();
sb.AppendFormat("NombreCliente: {0}", NombreCliente);
sb.AppendLine();
return sb.ToString();
}
}
}/////////////////////
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Biblioteca
{
public class SeguroVehiculo : SeguroGeneral, ISeguro
{
private int _yearFabricacion;
private int _kilometraje;
public int YearFabricacion
{
get
{
return _yearFabricacion;
}
set
{
int maxYear = DateTime.Today.Year + 1;
if (value < 1800 && value > maxYear)
{
string mensaje = string.Format("No se puede asegurar un vehículo, anterior a 1800 ni posterior a {0}", maxYear);
throw new ArgumentOutOfRangeException(mensaje);
}
_yearFabricacion = value;
}
}
public int Kilometraje
{
get
{
return _kilometraje;
}
set
{
if (value < 0 && value > 100000000)
{
string mensaje = string.Format("El kilometraje no puede ser negativo ni, superar 1 millón de kilómetros");
throw new ArgumentOutOfRangeException(mensaje);
}
_kilometraje = value;
}
}
public bool EsRiesgoso
{
get
{
return (CalcularPrimaMensual() > 50000);
}
}
public SeguroVehiculo(): base()
{
Init();
}
private void Init()
{
base.NombreSeguro = "Seguro de Vehículo";
base.Tipo = TipoSeguro.Vehiculo;
_yearFabricacion = DateTime.Today.Year;
_kilometraje = 0;
}
public SeguroVehiculo(int yearFabricacion, int kilometraje, DateTime fechaInicio, string nombreCliente)
: base(fechaInicio, nombreCliente)
{
base.NombreSeguro = "Seguro de Vehículo";
base.Tipo = TipoSeguro.Vehiculo;
_yearFabricacion = yearFabricacion;
_kilometraje = kilometraje;
}
public new string ObtenerDatos()
{
StringBuilder sb = new StringBuilder();
sb.Append(base.ObtenerDatos());
sb.AppendFormat("Año fabricación: {0}", YearFabricacion);
sb.AppendLine();
sb.AppendFormat("Kilometraje: {0}", Kilometraje);
sb.AppendLine();
sb.AppendFormat("Prima mensual: {0}", CalcularPrimaMensual());
return sb.ToString();
}
public float CalcularPrimaMensual()
{
int monto = 20000; //Prima base
if (YearFabricacion < 2010)
{
monto += 25000;
}
if (Kilometraje > 200000)
{
monto += 15000;
}
return monto;
}
}
}
//////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Biblioteca
{
public class SeguroVida : SeguroGeneral, ISeguro
{
private int _edad;
private TipoSexo _sexo;
public int Edad
{
get
{
return _edad;
}
set
{
if (value < 18)
{
throw new ArgumentOutOfRangeException("La edad del cliente debe ser 18 años o más");
}
_edad = value;
}
}
public TipoSexo Sexo
{
get
{
return _sexo;
}
set
{
if (value == TipoSexo.NoSeleccionado)
{
throw new ArgumentOutOfRangeException("El sexo debe ser uno de estos tres valores: Hombre, Mujer, Otro");
}
_sexo = value;
}
}
public bool EsRiesgoso
{
get
{
return (CalcularPrimaMensual() > 50000);
}
}
public SeguroVida(): base()
{
Init();
}
private void Init()
{
base.NombreSeguro = "Seguro de Vida";
base.Tipo = TipoSeguro.Vida;
_edad = 18;
_sexo = TipoSexo.NoSeleccionado;
}
public SeguroVida(int edad, TipoSexo sexo, DateTime fechaInicio, string nombreCliente)
: base(fechaInicio, nombreCliente)
{
base.NombreSeguro = "Seguro de Vida";
base.Tipo = TipoSeguro.Vida;
_edad = edad;
_sexo = sexo;
}
public new string ObtenerDatos()
{
StringBuilder sb = new StringBuilder();
sb.Append(base.ObtenerDatos());
sb.AppendFormat("Edad: {0}", Edad);
sb.AppendLine();
sb.AppendFormat("Sexo: {0}", Sexo);
sb.AppendLine();
sb.AppendFormat("Prima mensual: {0}", CalcularPrimaMensual());
return sb.ToString();
}
public float CalcularPrimaMensual()
{
int monto = 30000; //Prima base
if (Edad > 50)
{
monto += 30000;
}
if (Sexo == TipoSexo.Hombre)
{
monto += 20000;
}
return monto;
}
}
}
///////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Biblioteca;
namespace AseguraMeWPF
{
/// <summary>
/// Lógica de interacción para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SeguroColleccion coleccion = new SeguroColleccion();
public MainWindow()
{
InitializeComponent();
Limpiar();
}
private void Limpiar()
{
txtNomCliente.Text = string.Empty;
rdVida.IsChecked = false;
lblVida.IsEnabled = false;
rctVida.IsEnabled = false;
lblSexo.IsEnabled = false;
rdMasc.IsEnabled = false;
rdFem.IsEnabled = false;
lblEdad.IsEnabled = false;
txtEdad.IsEnabled = false;
txtEdad.Text = string.Empty;
rdVehiculo.IsChecked = false;
lblVehiculo.IsEnabled = false;
rctVehiculo.IsEnabled = false;
lblAnhoFab.IsEnabled = false;
txtAnhoFab.IsEnabled = false;
txtAnhoFab.Text = string.Empty;
lblKilo.IsEnabled = false;
txtKilo.IsEnabled = false;
txtKilo.Text = string.Empty;
dtFecInicio.SelectedDate = DateTime.Now;
txtMayorDe50.Text = string.Empty;
txtRiesgosos.Text = string.Empty;
txtMenor.Text = string.Empty;
txtMayor.Text = string.Empty;
}
private void btnLimpiar_Click(object sender, RoutedEventArgs e)
{
Limpiar();
}
private void rdVida_Checked(object sender, RoutedEventArgs e)
{
txtAnhoFab.IsEnabled = false;
txtAnhoFab.Text = string.Empty;
txtKilo.IsEnabled = false;
txtKilo.Text = string.Empty;
rdMasc.IsEnabled = true;
rdFem.IsEnabled = true;
txtEdad.IsEnabled = true;
}
private void rdVehiculo_Checked(object sender, RoutedEventArgs e)
{
txtAnhoFab.IsEnabled = true;
txtEdad.Text = string.Empty;
txtKilo.IsEnabled = true;
rdMasc.IsEnabled = false;
rdFem.IsEnabled = false;
txtEdad.IsEnabled = false;
}
private void btnAgregar_Click(object sender, RoutedEventArgs e)
{
if (rdVida.IsChecked.Value)
{
if (ValidarSeguroVida())
{
TipoSexo sexo = rdMasc.IsChecked.Value ? TipoSexo.Hombre : TipoSexo.Mujer;
coleccion.Add(new SeguroVida(int.Parse(txtEdad.Text), sexo, dtFecInicio.SelectedDate.Value, txtNomCliente.Text));
}
}
else
{
if (ValidarSeguroVehiculo())
{
coleccion.Add(new SeguroVehiculo(int.Parse(txtAnhoFab.Text), int.Parse(txtKilo.Text), dtFecInicio.SelectedDate.Value, txtNomCliente.Text));
}
}
var list = (from r in coleccion
select new
{
Id = r.Id,
Nombre = r.NombreCliente,
Fechainicio = r.FechaInicio,
TipoSeguro = (r is SeguroVida)? "Vida" : "Vehículo",
Edad = (r is SeguroVida) ? (r as SeguroVida).Edad.ToString() : "",
Sexo = (r is SeguroVida) ? (r as SeguroVida).Sexo.ToString() : "",
AnioFabricacion = (r is SeguroVehiculo) ? (r as SeguroVehiculo).YearFabricacion.ToString() : "",
Kilometraje = (r is SeguroVehiculo) ? (r as SeguroVehiculo).Kilometraje.ToString() : "",
PrimaMensual =
(r is SeguroVida)? (r as SeguroVida).CalcularPrimaMensual()
: (r as SeguroVehiculo).CalcularPrimaMensual(),
esRiesgoso =
(r is SeguroVida) ? (r as SeguroVida).EsRiesgoso
: (r as SeguroVehiculo).EsRiesgoso,
}
).ToList();
dgRegistro.ItemsSource = list;
dgRegistro.Items.Refresh();
txtMayorDe50.Text = coleccion.ObtenerCantidadClientesMayoresDe50Years().ToString();
txtRiesgosos.Text = coleccion.ObtenerCantidadClientesRiesgosos().ToString();
string nom = "";
coleccion.ObtenerClientesConMenorPrima().ForEach(r => { nom += r + " "; });
txtMenor.Text = nom;
nom = "";
coleccion.ObtenerClientesConMayorPrima().ForEach(r => { nom += r + " "; });
txtMayor.Text = nom;
}
private bool ValidarSeguroVida()
{
if (string.IsNullOrEmpty(txtNomCliente.Text.Trim()))
{
MessageBox.Show("Debe ingresar el nombre del cliente");
return false;
}
int edad = 0;
if (!int.TryParse(txtEdad.Text, out edad))
{
MessageBox.Show("La edad debe ser un entero");
return false;
}
if (!rdMasc.IsChecked.Value && !rdFem.IsChecked.Value)
{
MessageBox.Show("Debe seleccionar un sexo");
return false;
}
if (dtFecInicio.SelectedDate == null)
{
MessageBox.Show("Debe ingresar una fecha de inicio del seguro");
return false;
}
return true;
}
private bool ValidarSeguroVehiculo()
{
if (string.IsNullOrEmpty(txtNomCliente.Text.Trim()))
{
MessageBox.Show("Debe ingresar el nombre del cliente");
return false;
}
int year = 0;
if (!int.TryParse(txtAnhoFab.Text, out year))
{
MessageBox.Show("El año de fabricación debe ser un entero mayor que 1800 y menor o igual al próximo año");
return false;
}
int kilo = 0;
if (!int.TryParse(txtKilo.Text, out kilo))
{
MessageBox.Show("El kilometraje debe ser un entero mayor o igual 0 y menor que 100.000.000");
return false;
}
if (dtFecInicio.SelectedDate == null)
{
MessageBox.Show("Debe ingresar una fecha de inicio del seguro");
return false;
}
return true;
}
private void dgRegistro_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (dgRegistro.SelectedIndex != -1)
//{
// ISeguro seguro = (ISeguro)seguros[dgRegistro.SelectedIndex];
// txtMayorDe50.Text =
//}
}
}
}
Comentarios sobre la versión: 1.0 (1)