public void UseInventory(Ingredient ingredient) { var totalIndex = TotalInv.IndexOf(TotalInv.Where(i => i.Name == ingredient.Name).FirstOrDefault()); if (totalIndex == -1 || TotalInv[totalIndex].Weight < ingredient.Weight) { Console.WriteLine($"There is not enough {ingredient.Name} in your inventory"); return; } var usedWeight = ingredient.Weight; while (usedWeight > 0) { var listIndex = InventoryList.IndexOf(InventoryList.Where(i => i.Name == ingredient.Name).FirstOrDefault()); if (InventoryList[listIndex].Weight < usedWeight) { usedWeight -= InventoryList[listIndex].Weight; InventoryList.RemoveAt(listIndex); } else { InventoryList[listIndex].Weight -= usedWeight; usedWeight = 0; } } AdjustTotalInventory(ingredient); }
private void AdjustTotalInventory(Ingredient ingredient) { var index = TotalInv.FindIndex(i => i.Name == ingredient.Name); var totalWeight = InventoryList.Where(i => i.Name == ingredient.Name).Sum(i => i.Weight); if (totalWeight > 0) { decimal avgCost = Math.Round(InventoryList.Where(i => i.Name == ingredient.Name).Select(i => i.getCurrentTotalValue()).Sum(), 2) / (decimal)(totalWeight); TotalInv[index].Weight = totalWeight; TotalInv[index].Value = avgCost; } else { TotalInv.RemoveAt(TotalInv.IndexOf(TotalInv.Where(i => i.Name == ingredient.Name).First())); } foreach (var ing in InventoryList) { Console.WriteLine($"{ing.Name} - {ing.Weight}"); } }