namespace Vending2 { public interface IVendingMachine { string Manufacturer { get; }…
- User
namespace Vending2 { public interface IVendingMachine { string Manufacturer { get; } bool HasProducts { get; } Money Amount { get; } Money InsertCoin(Money amount); Money ReturnMoney(); bool AddProduct(string name, Money price, int count); bool UpdateProduct(int productNumber, string name, Money? price, int amount); }
public struct Money { public int Euros { get; set; } public int Cents { get; set; } } public struct Product { public int Available { get; set; } public Money Price { get; set; } public string Name { get; set; } } public class VendingMachine : IVendingMachine { public string Manufacturer { get; } public bool HasProducts => _products.Count > 0; public Money Amount => _Amount; private Money _Amount; private List<Product> _products = new List<Product>(); public VendingMachine(string manufacturer) { Manufacturer = manufacturer; _Amount = new Money(); _products = new List<Product>(); } public Money InsertCoin(Money amount) { if (IsValidCoin(amount)) { _Amount.Euros += amount.Euros; _Amount.Cents += amount.Cents; if (_Amount.Cents >= 100) { _Amount.Euros += _Amount.Cents / 100; _Amount.Cents = _Amount.Cents % 100; } return new Money(); } else { return amount; } //Console.WriteLine($"{_Amount}"); } public Money ReturnMoney() { Money returnAmount = _Amount; _Amount = new Money(); return returnAmount; } public bool AddProduct(string name, Money price, int count) { if (string.IsNullOrEmpty(name) || price.Euros < 0 || price.Cents < 0 || count < 0) { return false; } Product existingProduct = _products.Find(product => product.Name == name); if (existingProduct.Name != null) { existingProduct.Available += count; } else { _products.Add(new Product { Name = name, Price = price, Available = count }); } return true; } public bool UpdateProduct(int productNumber, string name, Money? price, int amount) { if (productNumber < 1 || productNumber > _products.Count || string.IsNullOrEmpty(name) || price.HasValue && (price.Value.Euros < 0 || price.Value.Cents < 0) || amount < 0) { return false; } Product product = _products[productNumber - 1]; product.Name = name; if (price.HasValue) { product.Price = price.Value; } product.Available = amount; return true; } public IEnumerable<Product> GetProducts() { return _products; } private bool IsValidCoin(Money money) { return money.Euros == 0 && (money.Cents == 10 || money.Cents == 20 || money.Cents == 50) || money.Euros == 1 && (money.Cents == 0 || money.Cents == 2) || money.Euros == 2 && (money.Cents == 0); } } internal class Program { static void Main(string[] args) { VendingMachine vendingMachine = new VendingMachine("Company"); vendingMachine.AddProduct("Product 1", new Money { Euros = 2, Cents = 0 }, 5); vendingMachine.AddProduct("Product 2", new Money { Euros = 1, Cents = 50 }, 3); vendingMachine.AddProduct("Product 3", new Money { Euros = 0, Cents = 20 }, 10); Money coin1 = new Money { Euros = 0, Cents = 20 }; Money coin2 = new Money { Euros = 1, Cents = 0 }; Money coin3 = new Money { Euros = 2, Cents = 0 }; Money returnedAmount1 = vendingMachine.InsertCoin(coin1); Money returnedAmount2 = vendingMachine.InsertCoin(coin2); Money returnedAmount3 = vendingMachine.InsertCoin(coin3); Money moneyInVendingMachine = vendingMachine.ReturnMoney(); vendingMachine.UpdateProduct(2, "Uptated product", new Money { Euros = 0, Cents = 30 }, 8); vendingMachine.AddProduct("Product 4", new Money { Euros = 2, Cents = 20 }, 3); Console.WriteLine($"Amount in vending machine: {vendingMachine.Amount.Euros}.{vendingMachine.Amount.Cents:D2}"); foreach (var product in vendingMachine.GetProducts()) { Console.WriteLine($"Product: {product.Name}," + $" Price: {product.Price.Euros}.{product.Price.Cents:D2}, Available: {product.Available}"); } Console.WriteLine($"Money to return: {moneyInVendingMachine.Euros}.{moneyInVendingMachine.Cents:D2}"); } } - Assistant
The issue with the code is that the UpdateProduct method doesn't update the product in the _products list. To fix it, you can modify the method as follows:
public bool UpdateProduct(int productNumber, string name, Money? price, int amount) { if (productNumber < 1 || productNumber > _products.Count || string.IsNullOrEmpty(name) || price.HasValue && (price.Value.Euros < 0 || price.Value.Cents < 0) || amount < 0) { return false; } Product product = _products[productNumber - 1]; product.Name = name; if (price.HasValue) { product.Price = price.Value; } product.Available = amount; _products[productNumber - 1] = product; // Update the product in the list return true; }With this modification, the UpdateProduct method will correctly update the product in the _products list.