Advertisement
Artem_Chepurov

Ex 3

Jun 15th, 2022
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.Json;
  5.  
  6. namespace SandBox
  7. {
  8.     public class Product
  9.     {
  10.         public int id { get; set; }
  11.         public string name { get; set; }
  12.         public int price { get; set; }
  13.         public string date { get; set; }
  14.     }
  15.     public class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             string inputJSON = Console.ReadLine();
  20.            
  21.             List<string> filtersList = new List<string>(5);
  22.             for (int i = 0; i < 5; i++)
  23.             {
  24.                 filtersList.Add(Console.ReadLine());
  25.             }
  26.  
  27.             Dictionary<string, string> filtersDict = new Dictionary<string, string>(5);
  28.  
  29.             foreach (var filter in filtersList)
  30.             {
  31.                 filtersDict.Add(filter.Split(' ')[0], filter.Split(' ')[1]);
  32.             }
  33.  
  34.             List <Product> products= new List<Product>();
  35.             products = JsonSerializer.Deserialize<List<Product>>(inputJSON);
  36.  
  37.             DateTime dateAfter = new DateTime(Convert.ToInt32(filtersDict["DATE_AFTER"].Split('.')[2]),
  38.                 Convert.ToInt32(filtersDict["DATE_AFTER"].Split('.')[1]),
  39.                 Convert.ToInt32(filtersDict["DATE_AFTER"].Split('.')[0]));
  40.             DateTime dateBefore = new DateTime(Convert.ToInt32(filtersDict["DATE_BEFORE"].Split('.')[2]),
  41.                 Convert.ToInt32(filtersDict["DATE_BEFORE"].Split('.')[1]),
  42.                 Convert.ToInt32(filtersDict["DATE_BEFORE"].Split('.')[0]));
  43.  
  44.             List<Product> selectedProducts = new List<Product>();
  45.  
  46.             foreach (var product in products)
  47.             {
  48.                 DateTime date = new DateTime(Convert.ToInt32(product.date.Split('.')[2]),
  49.                     Convert.ToInt32(product.date.Split('.')[1]),
  50.                     Convert.ToInt32(product.date.Split('.')[0]));
  51.                 if (product.name.ToLower().Contains(filtersDict["NAME_CONTAINS"].ToLower()) &&
  52.                     product.price >= Convert.ToInt32(filtersDict["PRICE_GREATER_THAN"]) &&
  53.                     product.price <= Convert.ToInt32(filtersDict["PRICE_LESS_THAN"]) &&
  54.                     date >= dateAfter && date <= dateBefore)
  55.                 {
  56.                     selectedProducts.Add(product);
  57.                 }
  58.             }
  59.  
  60.             var sortedSelectedProducts = from p in selectedProducts orderby p.id select p;
  61.             Console.WriteLine(JsonSerializer.Serialize(sortedSelectedProducts));
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement