Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using Newtonsoft.Json;
  7. using System.IO;
  8.  
  9. namespace LAB1_A
  10. {
  11.     class Product
  12.     {
  13.         public string name { get; set; }
  14.         public int quantity { get; set; }
  15.         public double price { get; set; }
  16.  
  17.         public string Format() => String.Format("{0, -12} {1, -10} {2, -10}", name, quantity, price);
  18.     }
  19.  
  20.     class ProductContainer
  21.     {
  22.         public Product[] Products { get; set; } = new Product[50];
  23.  
  24.         public ProductContainer(string data)
  25.         {
  26.             Products = JsonConvert.DeserializeObject<Product[]>(data);
  27.         }
  28.  
  29.     }
  30.  
  31.     class MonitorClass
  32.     {
  33.         private const int bufferSize = 25;
  34.  
  35.         private Predicate<Product> predicate = null;
  36.         private object threadLock = new object();
  37.         private Product[] dataArray = new Product[bufferSize];
  38.         public Product[] resultArray = new Product[50];
  39.         private int count;
  40.         public bool dataLoaded;
  41.  
  42.         public MonitorClass(Predicate<Product> pred)
  43.         {
  44.             this.predicate = pred;
  45.             this.count = 0;
  46.             this.dataLoaded = false;
  47.         }
  48.  
  49.         public void Add(Product obj)
  50.         {
  51.             lock (threadLock)
  52.             {
  53.                 while (!(count < bufferSize))
  54.                 {
  55.                     Monitor.Wait(threadLock);
  56.                 }
  57.  
  58.                 dataArray[count] = obj;
  59.                 count++;
  60.                 Monitor.PulseAll(threadLock);
  61.             }
  62.         }
  63.  
  64.         public Product Get()
  65.         {
  66.             Product newProduct;
  67.             lock (threadLock)
  68.             {
  69.                 if (count == 0 && dataLoaded == true)
  70.                 {
  71.                     return null;
  72.                 }
  73.  
  74.                 while (count == 0)
  75.                 {
  76.                     Monitor.Wait(threadLock);
  77.                 }
  78.  
  79.                 newProduct = dataArray[0];
  80.                 dataArray = dataArray.Where(x => x != newProduct).ToArray();
  81.                 Array.Resize(ref dataArray, bufferSize);
  82.                 count -= 1;
  83.                 Monitor.PulseAll(threadLock);
  84.             }
  85.             return newProduct;
  86.         }
  87.  
  88.         public void MoveData()
  89.         {
  90.             while (count > 0 || dataLoaded == false)
  91.             {
  92.                 var obj = Get();
  93.  
  94.                 if (obj != null)
  95.                 {
  96.                     lock (threadLock)
  97.                     {
  98.                         if (predicate.Invoke(obj))
  99.                         {
  100.                             obj = CalculatePrice(obj);
  101.                             InsertResult(obj, 0);
  102.                             Monitor.PulseAll(threadLock);
  103.                         }
  104.                     }
  105.                 }
  106.  
  107.                 Thread.Sleep(500);
  108.             }
  109.         }
  110.  
  111.         public Product CalculatePrice(Product obj)
  112.         {
  113.             var temp = obj.name.ToCharArray().Select(x => (int)x).ToArray();
  114.             int sum = temp.Sum(x => x);
  115.             obj.price = obj.quantity * sum;
  116.             return obj;
  117.         }
  118.  
  119.         public void InsertResult(Product obj, int i)
  120.         {
  121.             if (i == resultArray.Length)
  122.             {
  123.                 return;
  124.             }
  125.  
  126.             if (resultArray[i] == null)
  127.             {
  128.                 resultArray[i] = obj;
  129.                 return;
  130.             }
  131.  
  132.             if (String.Compare(resultArray[i].name, obj.name) > 0)
  133.             {
  134.                 var tmp = resultArray[i];
  135.                 resultArray[i] = obj;
  136.                 InsertResult(tmp, ++i);
  137.             }
  138.             else
  139.             {
  140.                 InsertResult(obj, ++i);
  141.             }
  142.         }
  143.  
  144.         public string FormatResults()
  145.         {
  146.             var res = "No vehicles found";
  147.             if (resultArray.All(x => x == null))
  148.             {
  149.                 Console.WriteLine(res);
  150.                 return res;
  151.             }
  152.  
  153.             res = resultArray
  154.                 .Where(x => x != null)
  155.                 .Distinct()
  156.                 .Select(x => x.Format())
  157.                 .Aggregate((s, i) => s + (Environment.NewLine + i));
  158.             Console.WriteLine(res);
  159.             return res;
  160.         }
  161.     }
  162.  
  163.  
  164.     class Program
  165.     {
  166.         static void Main(string[] args)
  167.         {
  168.             var currentDir = Directory.GetCurrentDirectory();
  169.             var outputDir = currentDir + "\\results.txt";
  170.  
  171.             var test = new ProductContainer(File.ReadAllText("IFF-611_VolkeviciusT_L1A_dat_1.json"));
  172.  
  173.             var threadCount = test.Products.Length / 4;
  174.             var threads = new List<Thread>();
  175.  
  176.             var monitor = new MonitorClass(v => v.quantity < 30);
  177.  
  178.  
  179.             for (int i = 0; i < threadCount; i++)
  180.             {
  181.                 var th = new Thread(() => monitor.MoveData());
  182.                 threads.Add(th);
  183.                 th.Start();
  184.             }
  185.  
  186.             foreach (var prod in test.Products)
  187.             {
  188.                 monitor.Add(prod);
  189.             }
  190.  
  191.             monitor.dataLoaded = true;
  192.  
  193.             foreach (var thread in threads)
  194.             {
  195.                 thread.Join();
  196.             }
  197.  
  198.             var results = monitor.FormatResults();
  199.  
  200.             var resultsFile = File.Create(outputDir);
  201.  
  202.             resultsFile.Write(Encoding.UTF8.GetBytes(results), 0, Encoding.UTF8.GetByteCount(results));
  203.             resultsFile.Flush();
  204.  
  205.             Console.Read();
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement