Skip to content

Instantly share code, notes, and snippets.

@verborghs
Last active October 30, 2019 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verborghs/7e4993f446f382ecc9fd22da2c5542b9 to your computer and use it in GitHub Desktop.
Save verborghs/7e4993f446f382ecc9fd22da2c5542b9 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 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--;
//_messageQueue.Enqueue("Selling " + product.Name);
//_messageQueue.Enqueue("Returning " + _coins + " coins");
_coins = 0;
}
else
{
//_messageQueue.Enqueue("Need " + product.Coins + " coins for " + product.Name);
}
}
else
{
//_messageQueue.Enqueue("Sorry, " + product.Name + "sold out");
}
}
else
{
//_messageQueue.Enqueue("Invalid choice");
}
}
public void Cancel()
{
_coins = 0;
//_messageQueue.Enqueue("Returning " + _coins + " 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();
foreach (var product in _vendingMachine.Products)
{
ProductView view = _productUIPrefab.Create(_productsListUI);
view.Code = product.Key;
view.Product = product.Value;
}
}
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";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment