Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Wintellect.PowerCollections;
- using System.Linq;
- public class Instock : IProductStock
- {
- private Dictionary<string, Product> productsByLabel;
- private SortedSet<Product> productsSortedByLabel;
- private List<Product> productsByIndex;
- private SortedDictionary<double, HashSet<Product>> productsByPrice;
- public Instock()
- {
- this.productsByLabel = new Dictionary<string, Product>();
- this.productsSortedByLabel = new SortedSet<Product>();
- this.productsByIndex = new List<Product>();
- this.productsByPrice = new SortedDictionary<double, HashSet<Product>>();
- }
- public int Count => this.productsByLabel.Count;
- public void Add(Product product)
- {
- if (!this.Contains(product))
- {
- this.productsByLabel[product.Label] = product;
- this.productsSortedByLabel.Add(product);
- this.productsByIndex.Add(product);
- if (!this.productsByPrice.ContainsKey(product.Price))
- {
- this.productsByPrice[product.Price] = new HashSet<Product>();
- }
- this.productsByPrice[product.Price].Add(product);
- }
- }
- public bool Contains(Product product)
- {
- return this.productsByLabel.ContainsKey(product.Label);
- }
- public Product Find(int index)
- {
- if (index < 0 || index >= this.Count)
- {
- throw new IndexOutOfRangeException();
- }
- return this.productsByIndex[index];
- }
- public void ChangeQuantity(string product, int quantity)
- {
- if (!this.productsByLabel.ContainsKey(product))
- {
- throw new ArgumentException();
- }
- Product item = this.productsByLabel[product];
- item.Quantity = quantity;
- int lastIndex = this.productsByIndex.Count - 1;
- if (this.productsByIndex[lastIndex].Label == item.Label)
- {
- this.productsByIndex[lastIndex].Quantity = quantity;
- }
- else
- {
- this.productsByIndex.Remove(item);
- this.productsByIndex.Add(item);
- }
- }
- public IEnumerable<Product> FindAllByQuantity(int quantity)
- {
- return this.productsByIndex.Where(p => p.Quantity == quantity);
- }
- public IEnumerable<Product> FindAllByPrice(double price)
- {
- if (this.productsByPrice.ContainsKey(price))
- {
- return this.productsByPrice[price];
- }
- return new Product[0];
- }
- public IEnumerable<Product> FindAllInRange(double lo, double hi)
- {
- return this.productsByPrice.Keys
- .Where(k => k > lo && k <= hi)
- .Reverse()
- .SelectMany(k => this.productsByPrice[k])
- .ToArray();
- }
- public IEnumerable<Product> FindFirstMostExpensiveProducts(int count)
- {
- if (count > this.Count)
- {
- throw new ArgumentException();
- }
- return this.productsByPrice.Keys
- .Reverse()
- .Take(count)
- .SelectMany(k => this.productsByPrice[k])
- .ToArray();
- }
- public Product FindByLabel(string label)
- {
- if (!this.productsByLabel.ContainsKey(label))
- {
- throw new ArgumentException();
- }
- return this.productsByLabel[label];
- }
- public IEnumerable<Product> FindFirstByAlphabeticalOrder(int count)
- {
- if (count < 0 || count > this.Count)
- {
- throw new ArgumentException();
- }
- return this.productsSortedByLabel
- .Take(count);
- }
- public IEnumerator<Product> GetEnumerator()
- {
- return this.productsByIndex.GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement