public ContainableItem GetItem(Position position) { int id = this.matrix.GetItemIdAtPosition(position); ContainableItem item = this.products.FirstOrDefault(prod => prod.Id == id); return(item); }
public void AddItem(ContainableItem prod) { if (this.products.Any(p => p.Id == prod.Id)) { throw new Exception($"Item already exists in list with id: {prod.Id}"); } this.products.Add(prod); }
public ContainableItem RemoveItem(int itemId) { ContainableItem itemToRemove = this.products.FirstOrDefault(p => p.Id == itemId); if (itemToRemove != null) { this.products.Remove(itemToRemove); } return(itemToRemove); }
public void Update(ContainableItem obj) { if (obj.Quantity == 0) { // Remove it from list this.RemoveItem(obj.Id); // Clear positions in matrix. this.matrix.ClearPositionsInMatrix(obj.Id); } }
public void LoadFromFile(string databaseFilePath) { using (StreamReader file = File.OpenText(databaseFilePath)) { JsonSerializer serializer = new JsonSerializer(); var productsInFile = (List <Product>)serializer.Deserialize(file, typeof(List <Product>)); foreach (Product product in productsInFile) { ContainableItem item = this.BuildContainableItem(product); this.AddItem(item); } } }
private ContainableItem BuildContainableItem(Product product) { ContainableItem item = new ContainableItem(product); Position positionInMatrix = this.matrix.GetNextPosition(item.Size, item.Id); if (positionInMatrix != null) { item.Position = positionInMatrix; } else { throw new Exception($"Cannot add product: {item.Name} in vending machine. No position found!"); } return(item); }