Advertisement
KAMEN1973

InStock

Nov 13th, 2020
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Wintellect.PowerCollections;
  5. using System.Linq;
  6.  
  7.  
  8. public class Instock : IProductStock
  9. {
  10.     private Dictionary<string, Product> productsByLabel;
  11.     private SortedSet<Product> productsSortedByLabel;
  12.     private List<Product> productsByIndex;
  13.     private SortedDictionary<double, HashSet<Product>> productsByPrice;
  14.  
  15.     public Instock()
  16.     {
  17.         this.productsByLabel = new Dictionary<string, Product>();
  18.         this.productsSortedByLabel = new SortedSet<Product>();
  19.         this.productsByIndex = new List<Product>();
  20.         this.productsByPrice = new SortedDictionary<double, HashSet<Product>>();
  21.     }
  22.  
  23.     public int Count => this.productsByLabel.Count;
  24.  
  25.     public void Add(Product product)
  26.     {
  27.         if (!this.Contains(product))
  28.         {
  29.             this.productsByLabel[product.Label] = product;
  30.             this.productsSortedByLabel.Add(product);
  31.             this.productsByIndex.Add(product);
  32.  
  33.             if (!this.productsByPrice.ContainsKey(product.Price))
  34.             {
  35.                 this.productsByPrice[product.Price] = new HashSet<Product>();
  36.             }
  37.             this.productsByPrice[product.Price].Add(product);
  38.         }
  39.     }
  40.  
  41.     public bool Contains(Product product)
  42.     {
  43.         return this.productsByLabel.ContainsKey(product.Label);
  44.     }
  45.  
  46.     public Product Find(int index)
  47.     {
  48.         if (index < 0 || index >= this.Count)
  49.         {
  50.             throw new IndexOutOfRangeException();
  51.         }
  52.  
  53.         return this.productsByIndex[index];
  54.     }
  55.  
  56.     public void ChangeQuantity(string product, int quantity)
  57.     {
  58.         if (!this.productsByLabel.ContainsKey(product))
  59.         {
  60.             throw new ArgumentException();
  61.         }
  62.  
  63.         Product item = this.productsByLabel[product];
  64.  
  65.         item.Quantity = quantity;
  66.  
  67.         int lastIndex = this.productsByIndex.Count - 1;
  68.  
  69.         if (this.productsByIndex[lastIndex].Label == item.Label)
  70.         {
  71.             this.productsByIndex[lastIndex].Quantity = quantity;
  72.         }
  73.         else
  74.         {
  75.             this.productsByIndex.Remove(item);
  76.             this.productsByIndex.Add(item);
  77.         }
  78.  
  79.     }
  80.  
  81.     public IEnumerable<Product> FindAllByQuantity(int quantity)
  82.     {
  83.         return this.productsByIndex.Where(p => p.Quantity == quantity);
  84.     }
  85.  
  86.     public IEnumerable<Product> FindAllByPrice(double price)
  87.     {
  88.         if (this.productsByPrice.ContainsKey(price))
  89.         {
  90.             return this.productsByPrice[price];
  91.         }
  92.  
  93.         return new Product[0];
  94.     }
  95.  
  96.     public IEnumerable<Product> FindAllInRange(double lo, double hi)
  97.     {
  98.         return this.productsByPrice.Keys
  99.             .Where(k => k > lo && k <= hi)
  100.             .Reverse()
  101.             .SelectMany(k => this.productsByPrice[k])
  102.             .ToArray();
  103.  
  104.     }
  105.  
  106.     public IEnumerable<Product> FindFirstMostExpensiveProducts(int count)
  107.     {
  108.         if (count > this.Count)
  109.         {
  110.             throw new ArgumentException();
  111.         }
  112.  
  113.         return this.productsByPrice.Keys
  114.             .Reverse()
  115.             .Take(count)
  116.             .SelectMany(k => this.productsByPrice[k])
  117.             .ToArray();
  118.     }
  119.  
  120.     public Product FindByLabel(string label)
  121.     {
  122.         if (!this.productsByLabel.ContainsKey(label))
  123.         {
  124.             throw new ArgumentException();
  125.         }
  126.  
  127.         return this.productsByLabel[label];
  128.     }
  129.  
  130.     public IEnumerable<Product> FindFirstByAlphabeticalOrder(int count)
  131.     {
  132.         if (count < 0 || count > this.Count)
  133.         {
  134.             throw new ArgumentException();
  135.         }
  136.  
  137.         return this.productsSortedByLabel
  138.             .Take(count);
  139.  
  140.     }
  141.  
  142.     public IEnumerator<Product> GetEnumerator()
  143.     {
  144.         return this.productsByIndex.GetEnumerator();
  145.     }
  146.  
  147.     IEnumerator IEnumerable.GetEnumerator()
  148.     {
  149.         return GetEnumerator();
  150.     }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement