Advertisement
Guest User

Untitled

a guest
May 25th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5.  
  6. public class Producto
  7. {
  8.     public int Mes { get; set; }
  9.     public string Nombre { get; set; }
  10.     public string Codigo { get; set; }
  11.     public decimal Precio { get; set; }
  12.     public DateTime FechaIngreso { get; set; }
  13.     public DateTime FechaVencimiento { get; set; }
  14.  
  15.     public bool Vencido => DateTime.Now > FechaVencimiento;
  16. }
  17.  
  18. public static class GestionProductos
  19. {
  20.     private static readonly List<Producto> Productos = new List<Producto>();
  21.  
  22.     public static Producto RegistrarProducto(int mes, string nombre, string codigo, decimal precio, string fechaVencimiento, string fechaIngreso = null)
  23.     {
  24.         fechaIngreso = fechaIngreso ?? DateTime.Now.ToString("yyyy/MM/dd");
  25.  
  26.         if (mes < 1 || mes > 12)
  27.             throw new Exception($"Mes Invalido: '{mes}'. Ingrese un valor del 1 al 12.");
  28.  
  29.         if (!DateTime.TryParseExact(fechaVencimiento, "yyyy/MM/dd", CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.None, out var fechaVenc))
  30.             throw new Exception($"La fecha de Vencimiento: '{fechaVencimiento}' tiene un formato incorrecto. Utilice el formato 'yyyy/MM/dd'.");
  31.  
  32.         if (!DateTime.TryParseExact(fechaIngreso, "yyyy/MM/dd", CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.None, out var fechaIngr))
  33.             throw new Exception($"La fecha de Ingreso: '{fechaIngreso}' tiene un formato incorrecto. Utilice el formato 'yyyy/MM/dd'.");
  34.  
  35.         var producto = new Producto
  36.         {
  37.             Mes = mes,
  38.             Nombre = nombre,
  39.             Codigo = codigo,
  40.             Precio = precio,
  41.             FechaIngreso = fechaIngr,
  42.             FechaVencimiento = fechaVenc
  43.         };
  44.  
  45.         Productos.Add(producto);
  46.  
  47.         return producto;
  48.     }
  49.  
  50.     // Estas dos funciones se pueden obviar por completo, ya que en C# se utiliza LINQ.
  51.     public static Producto GetPrimerProducto => Productos.FirstOrDefault();
  52.     public static Producto GetUltimoProducto => Productos.LastOrDefault();
  53.  
  54.     public static Producto GetProductoMasCostoso =>
  55.         Productos.OrderByDescending(x => x.Precio).FirstOrDefault();
  56.  
  57.     public static int GetProductosCount() => Productos.Count;
  58.  
  59.     public static decimal GetCostoTotal() =>
  60.         Productos.Where(x => !x.Vencido)
  61.                  .Aggregate(0m, (p, producto) => p + producto.Precio);
  62.  
  63.     public static decimal GetPrecioPromedio() =>
  64.         Productos.Any(x => !x.Vencido)
  65.             ? GetCostoTotal() / Productos.Count(x => !x.Vencido)
  66.             : 0m;
  67.  
  68.     public static void ImprimirProductosPorMes()
  69.     {
  70.         var porMes =
  71.             Productos.GroupBy(x => x.Mes)
  72.                      .OrderBy(x => x.Key);
  73.  
  74.         foreach (var g in porMes)
  75.         {
  76.             var mes = new DateTimeFormatInfo().GetMonthName(g.Key);
  77.             Console.WriteLine($"Mes {mes}:");
  78.  
  79.             foreach (var p in g)
  80.             {
  81.                 Console.WriteLine($"Codigo: {p.Codigo}");
  82.                 Console.WriteLine($"Nombre: {p.Nombre}");
  83.                 Console.WriteLine($"Precio: {p.Precio}");
  84.                 Console.WriteLine($"Fecha de Ingreso: {p.FechaIngreso:yyyy/MM/dd}");
  85.                 Console.WriteLine($"Fecha de Vencimiento: {p.FechaVencimiento:yyyy/MM/dd}");
  86.                 Console.WriteLine($"Vencido: {(p.Vencido ? "Si" : "No")}");
  87.             }
  88.  
  89.             Console.WriteLine("------------------------------------------");
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement