Skip to content

Instantly share code, notes, and snippets.

@verborghs
Created October 30, 2019 12:45
Show Gist options
  • Save verborghs/683b28b953725ac6c3074f7758d3f495 to your computer and use it in GitHub Desktop.
Save verborghs/683b28b953725ac6c3074f7758d3f495 to your computer and use it in GitHub Desktop.
public class VendingMachine
{
private Dictionary<string, Product> _products = new Dictionary<string, Product>()
{
{ "01", new Product("Product 01", 3, 5)},
{ "02", new Product("Product 02", 2, 1)},
{ "03", new Product("Product 03", 1, 0)},
{ "11", new Product("Product 04", 4, 0)},
{ "12", new Product("Product 05", 1, 5)},
{ "13", new Product("Product 06", 1, 5)},
{ "21", new Product("Product 07", 2, 3)},
{ "22", new Product("Product 08", 2, 2)},
{ "23", new Product("Product 09", 3, 5)},
{ "31", new Product("Product 10", 4, 0)},
{ "32", new Product("Product 11", 2, 1)},
{ "33", new Product("Product 12", 1, 1)},
};
private int[] _numbers = new[] { 0, 0 };
private int _coins = 0;
public Dictionary<string, Product> Products => _products;
public int[] Numbers => _numbers;
public int Coins => _coins;
public event EventHandler<VendingMachineMessageArgs> VendingMachineMessage;
public event EventHandler<ReturningCoinsArgs> ReturningCoins;
public event EventHandler<SellingProductArgs> SellingProduct;
public void InsertCoin()
{
_numbers[0] = 0;
_numbers[1] = 0;
_coins++;
}
public void EnterNumber(int number)
{
_numbers[0] = _numbers[1];
_numbers[1] = number;
}
public void Select()
{
if (_products.TryGetValue(string.Join("", _numbers), out Product product))
{
if (product.Available > 0)
{
if (product.Coins <= _coins)
{
_coins -= product.Coins;
product.Available--;
SellingProduct?.Invoke(this, new SellingProductArgs(product));
int coins = _coins;
_coins = 0;
ReturningCoins?.Invoke(this, new ReturningCoinsArgs(coins));
}
else
{
VendingMachineMessage?.Invoke(this, new VendingMachineMessageArgs(VendingMachineMessages.NotEnoughCoins));
}
}
else
{
VendingMachineMessage?.Invoke(this, new VendingMachineMessageArgs(VendingMachineMessages.ProductSoldOut));
}
}
else
{
VendingMachineMessage?.Invoke(this, new VendingMachineMessageArgs(VendingMachineMessages.InvalidChoice));
}
}
public void Cancel()
{
int coins = _coins;
_coins = 0;
ReturningCoins?.Invoke(this, new ReturningCoinsArgs(coins));
}
}
public class VendingMachineView : MonoBehaviour
{
#region Serialized Fields
[SerializeField]
private Text _message;
[SerializeField]
private ProductView _productUIPrefab;
[SerializeField]
private Transform _productsListUI;
#endregion
private TimedQueue<string> _messageQueue;
private VendingMachine _vendingMachine;
void Start()
{
_messageQueue = new TimedQueue<string>(ShowMessage, ShowDefaultMessage);
_vendingMachine = new VendingMachine();
_vendingMachine.VendingMachineMessage += OnVendingMachineMessage;
_vendingMachine.ReturningCoins += (sender, e) => _messageQueue.Enqueue("Returning " + e.Coins + " coins");
_vendingMachine.SellingProduct += (sender, e) => _messageQueue.Enqueue("Selling " + e.Product.Name);
AddProducts();
}
void Update()
{
_messageQueue.Update(Time.deltaTime);
}
public void OnCoinButtonClicked()
{
_vendingMachine.InsertCoin();
_messageQueue.Enqueue("You entered " + _vendingMachine.Coins + " coins");
}
public void OnNumberButtonClicked(int number)
{
_vendingMachine.EnterNumber(number);
_messageQueue.Enqueue(string.Join("", _vendingMachine.Numbers));
}
public void OnSelectButtonClicked()
{
_vendingMachine.Select();
}
public void OnCancelButtonClicked()
{
_vendingMachine.Cancel();
}
private void ShowMessage(string message)
{
_message.text = message;
}
private void ShowDefaultMessage()
{
_message.text = "Select a product";
}
private void OnVendingMachineMessage(object sender, VendingMachineMessageArgs e)
{
switch (e.Message)
{
case VendingMachineMessages.NotEnoughCoins:
_messageQueue.Enqueue("Not enough coins");
break;
case VendingMachineMessages.ProductSoldOut:
_messageQueue.Enqueue("Product sold out");
break;
case VendingMachineMessages.InvalidChoice:
_messageQueue.Enqueue("Invalid choice");
break;
}
}
private void AddProducts()
{
foreach (var product in _vendingMachine.Products)
{
ProductView view = _productUIPrefab.Create(_productsListUI);
view.Code = product.Key;
view.Product = product.Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment