namespace Vending2 { public interface IVendingMachine { string Manufacturer { get; }…
- User
namespace Vending2 { public interface IVendingMachine { string Manufacturer { get; } bool HasProducts { get; } Money Amount { get; } Product[] Products { 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 List<Product> _products = new List<Product>(); public string Manufacturer { get; } public bool HasProducts => _products.Count > 0; public Money Amount => _Amount; private Money _Amount; public Product[] Products => _products.ToArray(); 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; } } 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 updatedProduct = new Product { Name = name, Price = price ?? product.Price, Available = amount }; _products[productNumber - 1] = updatedProduct; 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 == 0) || 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 }; vendingMachine.InsertCoin(coin1); vendingMachine.InsertCoin(coin2); vendingMachine.InsertCoin(coin3); Console.WriteLine($"Amount in vending machine: {vendingMachine.Amount.Euros}.{vendingMachine.Amount.Cents:D2}"); vendingMachine.UpdateProduct(2, "Updated product", new Money { Euros = 5, Cents = 30 }, 8); vendingMachine.AddProduct("New Product 4", new Money { Euros = 2, Cents = 20 }, 3); foreach (var product in vendingMachine.GetProducts()) { Console.WriteLine($"Product: {product.Name}," + $" Price: {product.Price.Euros}.{product.Price.Cents:D2}, Available: {product.Available}"); } Money moneyInVendingMachine = vendingMachine.ReturnMoney(); Console.WriteLine($"Money to return: {moneyInVendingMachine.Euros}.{moneyInVendingMachine.Cents:D2}"); } } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.