Código de C sharp - Aseguradora finalizado alvarinho

1.0
estrellaestrellaestrellaestrellaestrella(1)

Publicado el 10 de Julio del 2018gráfica de visualizaciones de la versión: 1.0
9.922 visualizaciones desde el 10 de Julio del 2018
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
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)

10 de Julio del 2018
estrellaestrellaestrellaestrellaestrella
Muy buen código me sirvió mucho para un ejercicio parecido
Responder

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s4695