Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- public class Producto
- {
- public int Mes { get; set; }
- public string Nombre { get; set; }
- public string Codigo { get; set; }
- public decimal Precio { get; set; }
- public DateTime FechaIngreso { get; set; }
- public DateTime FechaVencimiento { get; set; }
- public bool Vencido => DateTime.Now > FechaVencimiento;
- }
- public static class GestionProductos
- {
- private static readonly List<Producto> Productos = new List<Producto>();
- public static Producto RegistrarProducto(int mes, string nombre, string codigo, decimal precio, string fechaVencimiento, string fechaIngreso = null)
- {
- fechaIngreso = fechaIngreso ?? DateTime.Now.ToString("yyyy/MM/dd");
- if (mes < 1 || mes > 12)
- throw new Exception($"Mes Invalido: '{mes}'. Ingrese un valor del 1 al 12.");
- if (!DateTime.TryParseExact(fechaVencimiento, "yyyy/MM/dd", CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.None, out var fechaVenc))
- throw new Exception($"La fecha de Vencimiento: '{fechaVencimiento}' tiene un formato incorrecto. Utilice el formato 'yyyy/MM/dd'.");
- if (!DateTime.TryParseExact(fechaIngreso, "yyyy/MM/dd", CultureInfo.CurrentCulture.DateTimeFormat, DateTimeStyles.None, out var fechaIngr))
- throw new Exception($"La fecha de Ingreso: '{fechaIngreso}' tiene un formato incorrecto. Utilice el formato 'yyyy/MM/dd'.");
- var producto = new Producto
- {
- Mes = mes,
- Nombre = nombre,
- Codigo = codigo,
- Precio = precio,
- FechaIngreso = fechaIngr,
- FechaVencimiento = fechaVenc
- };
- Productos.Add(producto);
- return producto;
- }
- // Estas dos funciones se pueden obviar por completo, ya que en C# se utiliza LINQ.
- public static Producto GetPrimerProducto => Productos.FirstOrDefault();
- public static Producto GetUltimoProducto => Productos.LastOrDefault();
- public static Producto GetProductoMasCostoso =>
- Productos.OrderByDescending(x => x.Precio).FirstOrDefault();
- public static int GetProductosCount() => Productos.Count;
- public static decimal GetCostoTotal() =>
- Productos.Where(x => !x.Vencido)
- .Aggregate(0m, (p, producto) => p + producto.Precio);
- public static decimal GetPrecioPromedio() =>
- Productos.Any(x => !x.Vencido)
- ? GetCostoTotal() / Productos.Count(x => !x.Vencido)
- : 0m;
- public static void ImprimirProductosPorMes()
- {
- var porMes =
- Productos.GroupBy(x => x.Mes)
- .OrderBy(x => x.Key);
- foreach (var g in porMes)
- {
- var mes = new DateTimeFormatInfo().GetMonthName(g.Key);
- Console.WriteLine($"Mes {mes}:");
- foreach (var p in g)
- {
- Console.WriteLine($"Codigo: {p.Codigo}");
- Console.WriteLine($"Nombre: {p.Nombre}");
- Console.WriteLine($"Precio: {p.Precio}");
- Console.WriteLine($"Fecha de Ingreso: {p.FechaIngreso:yyyy/MM/dd}");
- Console.WriteLine($"Fecha de Vencimiento: {p.FechaVencimiento:yyyy/MM/dd}");
- Console.WriteLine($"Vencido: {(p.Vencido ? "Si" : "No")}");
- }
- Console.WriteLine("------------------------------------------");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement