The code you provided is missing the using System.Collections.Generic; namespace at the top. Here is the corrected and completed code:
using System;
using System.Collections.Generic;
namespace Vending_machine
{
public interface IVendingMachine
{
string Manufacturer { get; }
bool HasProducts { get; }
Money Amount { get; }
List<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 string Manufacturer { get; }
public bool HasProducts => Products.Count > 0;
public Money Amount { get; private set; }
public List<Product> Products { get; }
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 != 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;
}
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)
{
// Create a new vending machine
IVendingMachine vendingMachine = new VendingMachine("Company A");
// Add products to the vending machine
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);
// Insert coins into the vending machine
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);
// Return the accumulated amount of money
Money moneyInVendingMachine = vendingMachine.ReturnMoney();
// Update a product's information
vendingMachine.UpdateProduct(1, "Product 4", new Money { Euros = 1, Cents = 0 }, 8);
// Check if the vending machine has any products available
bool hasProducts = vendingMachine.HasProducts;
}
}
}
Now the code is complete and should run without any errors.