Created
October 30, 2019 13:37
-
-
Save verborghs/69caa06d24a5e0c0b468edb4d8950519 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IVendingMachineView | |
{ | |
event EventHandler CoinInserted; | |
event EventHandler Cancel; | |
event EventHandler Select; | |
event EventHandler<NumberEnteredArgs> NumberEntered; | |
void ShowMessage(string message); | |
void AddProduct(string code, Product p); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NumberEnteredArgs : EventArgs | |
{ | |
public int Number { get; } | |
public NumberEnteredArgs(int number) | |
{ | |
Number = number; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VendingMachinePresenter | |
{ | |
private VendingMachine _vendingMachine; | |
private IVendingMachineView _view; | |
public VendingMachinePresenter(IVendingMachineView view) | |
{ | |
_view = view; | |
_view.Cancel += (sender, e) => _vendingMachine.Cancel(); ; | |
_view.Select += (sender, e) => _vendingMachine.Select(); ; | |
_view.NumberEntered += OnNumberEntered; | |
_view.CoinInserted += OnCoinInserted; | |
_vendingMachine = new VendingMachine(); | |
_vendingMachine.VendingMachineMessage += OnVendingMachineMessage; | |
_vendingMachine.ReturningCoins += (sender, e) =>_view.ShowMessage("Returning " + e.Coins + " coins"); | |
_vendingMachine.SellingProduct += (sender, e) => _view.ShowMessage("Selling " + e.Product.Name); | |
AddProducts(); | |
} | |
private void AddProducts() | |
{ | |
foreach (var product in _vendingMachine.Products) | |
{ | |
_view.AddProduct(product.Key, product.Value); | |
} | |
} | |
private void OnNumberEntered(object sender, NumberEnteredArgs e) | |
{ | |
_vendingMachine.EnterNumber(e.Number); | |
_view.ShowMessage(string.Join("", _vendingMachine.Numbers)); | |
} | |
private void OnCoinInserted(object sender, EventArgs e) | |
{ | |
_vendingMachine.InsertCoin(); | |
_view.ShowMessage("You entered " + _vendingMachine.Coins + " coins"); | |
} | |
private void OnVendingMachineMessage(object sender, VendingMachineMessageArgs e) | |
{ | |
switch (e.Message) | |
{ | |
case VendingMachineMessages.NotEnoughCoins: | |
_view.ShowMessage("Not enough coins"); | |
break; | |
case VendingMachineMessages.ProductSoldOut: | |
_view.ShowMessage("Product sold out"); | |
break; | |
case VendingMachineMessages.InvalidChoice: | |
_view.ShowMessage("Invalid choice"); | |
break; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class VendingMachineView : MonoBehaviour, IVendingMachineView | |
{ | |
[SerializeField] | |
private Text _message; | |
[SerializeField] | |
private ProductView _productUIPrefab; | |
[SerializeField] | |
private Transform _productsListUI; | |
public event EventHandler CoinInserted; | |
public event EventHandler Cancel; | |
public event EventHandler Select; | |
public event EventHandler<NumberEnteredArgs> NumberEntered; | |
private TimedQueue<string> _messageQueue; | |
private VendingMachinePresenter _presenter; | |
void Start() | |
{ | |
_messageQueue = new TimedQueue<string>(OnMessageDequeued, OnQueueEmpty); | |
_presenter= new VendingMachinePresenter(this); | |
} | |
void Update() | |
=> _messageQueue.Update(Time.deltaTime); | |
public void AddProduct(string code, Product product) | |
=> _productUIPrefab.Create(_productsListUI, code, product); | |
public void ShowMessage(string message) | |
=> _messageQueue.Enqueue(message); | |
public void OnCoinButtonClicked() | |
=> CoinInserted?.Invoke(this, EventArgs.Empty); | |
public void OnNumberButtonClicked(int number) | |
=> NumberEntered?.Invoke(this, new NumberEnteredArgs(number)); | |
public void OnSelectButtonClicked() | |
=> Select?.Invoke(this,EventArgs.Empty); | |
public void OnCancelButtonClicked() | |
=> Cancel?.Invoke(this, EventArgs.Empty); | |
private void OnMessageDequeued(string message) | |
=> _message.text = message; | |
private void OnQueueEmpty() | |
=> OnMessageDequeued("Select a product"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment