Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using Newtonsoft.Json;
- using System.IO;
- namespace LAB1_A
- {
- class Product
- {
- public string name { get; set; }
- public int quantity { get; set; }
- public double price { get; set; }
- public string Format() => String.Format("{0, -12} {1, -10} {2, -10}", name, quantity, price);
- }
- class ProductContainer
- {
- public Product[] Products { get; set; } = new Product[50];
- public ProductContainer(string data)
- {
- Products = JsonConvert.DeserializeObject<Product[]>(data);
- }
- }
- class MonitorClass
- {
- private const int bufferSize = 25;
- private Predicate<Product> predicate = null;
- private object threadLock = new object();
- private Product[] dataArray = new Product[bufferSize];
- public Product[] resultArray = new Product[50];
- private int count;
- public bool dataLoaded;
- public MonitorClass(Predicate<Product> pred)
- {
- this.predicate = pred;
- this.count = 0;
- this.dataLoaded = false;
- }
- public void Add(Product obj)
- {
- lock (threadLock)
- {
- while (!(count < bufferSize))
- {
- Monitor.Wait(threadLock);
- }
- dataArray[count] = obj;
- count++;
- Monitor.PulseAll(threadLock);
- }
- }
- public Product Get()
- {
- Product newProduct;
- lock (threadLock)
- {
- if (count == 0 && dataLoaded == true)
- {
- return null;
- }
- while (count == 0)
- {
- Monitor.Wait(threadLock);
- }
- newProduct = dataArray[0];
- dataArray = dataArray.Where(x => x != newProduct).ToArray();
- Array.Resize(ref dataArray, bufferSize);
- count -= 1;
- Monitor.PulseAll(threadLock);
- }
- return newProduct;
- }
- public void MoveData()
- {
- while (count > 0 || dataLoaded == false)
- {
- var obj = Get();
- if (obj != null)
- {
- lock (threadLock)
- {
- if (predicate.Invoke(obj))
- {
- obj = CalculatePrice(obj);
- InsertResult(obj, 0);
- Monitor.PulseAll(threadLock);
- }
- }
- }
- Thread.Sleep(500);
- }
- }
- public Product CalculatePrice(Product obj)
- {
- var temp = obj.name.ToCharArray().Select(x => (int)x).ToArray();
- int sum = temp.Sum(x => x);
- obj.price = obj.quantity * sum;
- return obj;
- }
- public void InsertResult(Product obj, int i)
- {
- if (i == resultArray.Length)
- {
- return;
- }
- if (resultArray[i] == null)
- {
- resultArray[i] = obj;
- return;
- }
- if (String.Compare(resultArray[i].name, obj.name) > 0)
- {
- var tmp = resultArray[i];
- resultArray[i] = obj;
- InsertResult(tmp, ++i);
- }
- else
- {
- InsertResult(obj, ++i);
- }
- }
- public string FormatResults()
- {
- var res = "No vehicles found";
- if (resultArray.All(x => x == null))
- {
- Console.WriteLine(res);
- return res;
- }
- res = resultArray
- .Where(x => x != null)
- .Distinct()
- .Select(x => x.Format())
- .Aggregate((s, i) => s + (Environment.NewLine + i));
- Console.WriteLine(res);
- return res;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var currentDir = Directory.GetCurrentDirectory();
- var outputDir = currentDir + "\\results.txt";
- var test = new ProductContainer(File.ReadAllText("IFF-611_VolkeviciusT_L1A_dat_1.json"));
- var threadCount = test.Products.Length / 4;
- var threads = new List<Thread>();
- var monitor = new MonitorClass(v => v.quantity < 30);
- for (int i = 0; i < threadCount; i++)
- {
- var th = new Thread(() => monitor.MoveData());
- threads.Add(th);
- th.Start();
- }
- foreach (var prod in test.Products)
- {
- monitor.Add(prod);
- }
- monitor.dataLoaded = true;
- foreach (var thread in threads)
- {
- thread.Join();
- }
- var results = monitor.FormatResults();
- var resultsFile = File.Create(outputDir);
- resultsFile.Write(Encoding.UTF8.GetBytes(results), 0, Encoding.UTF8.GetByteCount(results));
- resultsFile.Flush();
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement