private void CancelOrder() { VendingMachineState = VendingMachineState.Order; // Display cancel message DisplayMessageByCode(MessageCode.OrderCancel); // Return paid money foreach (var pm in _paymentWalletRepository.WalletList) { var msg = String.Format(_vendingMessageRepository.GetMessage(MessageCode.ReturnPayment), pm.Item1.MoneyValue, pm.Item2); _displayPanel.DisplayMessage(msg); } _paymentWalletRepository.ClearMoney(); // Return reserved stock of product foreach (var p in _orderedProducts) { _productRepository.AddToStock(p.Code, 1); } _orderedProducts.Clear(); _coinReceiver.Off(); TryToStartSelling(); }
private static void LoadProduct(VendingMachineState machine, string productName, double productPrice, int quantity) { machine.ProductStorage.Add(new Product() { Name = productName, Price = productPrice, Quantity = quantity }); }
/// <summary> /// Process the order actions /// </summary> /// <param name="cmd">The order command.</param> /// <param name="obj">The selected type of product.</param> void OrderAction(OrderCmdEvent cmd, Product obj) { if (VendingMachineState != VendingMachineState.Order) { return; } if (cmd == OrderCmdEvent.Select) { if (_productRepository.CountProduct(obj.Code) > 0) { _orderedProducts.Push(obj); string msg = String.Format(_vendingMessageRepository.GetMessage(MessageCode.Checkout), obj); _displayPanel.DisplayMessage(msg); _productRepository.RemoveSellFromStock(obj.Code); VendingMachineState = VendingMachineState.Payment; _orderPurchasePanel.Off(); _coinReceiver.On(); } } else if (cmd == OrderCmdEvent.OutOfStock) { DisplayMessageByCode(MessageCode.OutOfStock); _orderPurchasePanel.Off(); VendingMachineState = VendingMachineState.OutOfStock; } }
public VendingMachineService(IVendingMachineLoader vendingMachineLoader, IVendingMachineSaver vendingMachineSaver, ICoinValidator coinValidator) { this.vendingMachineLoader = vendingMachineLoader; this.vendingMachineSaver = vendingMachineSaver; this.coinValidator = coinValidator; this.machineState = new VendingMachineState(); }
void FailtException(Exception ex) { VendingMachineState = VendingMachineState.Fault; _orderPurchasePanel.Off(); _coinReceiver.Off(); _complete.TrySetException(ex); }
public void Off() { VendingMachineState = VendingMachineState.TurnedOff; DisplayMessageByCode(MessageCode.OutOfServise); _orderPurchasePanel.Off(); _coinReceiver.Off(); _complete.TrySetResult(0); }
public void VendingMachineState_ProcessString_Successful(string input) { var vendingMachineState = new VendingMachineState(new VendingCash()); var inputCoins = vendingMachineState.ProcessInputCoins(input); inputCoins.Count.Should().Be(22); }
public Task LoadVendingMachine(VendingMachineState machine) { LoadProduct(machine, productName: "Tea", productPrice: 1.30, quantity: 10); LoadProduct(machine, productName: "Espresso", productPrice: 1.80, quantity: 20); LoadProduct(machine, productName: "Juice", productPrice: 1.80, quantity: 20); LoadProduct(machine, productName: "Chicken soup", productPrice: 1.80, quantity: 15); LoadCoin(machine, quantity: 100, coinDenomination: 0.1); LoadCoin(machine, quantity: 100, coinDenomination: 0.2); LoadCoin(machine, quantity: 100, coinDenomination: 0.5); LoadCoin(machine, quantity: 100, coinDenomination: 1); return(Task.FromResult(0)); }
public Task LoadVendingMachine(VendingMachineState machine) { foreach (var coin in this.StorageCoins.Keys) { machine.CoinStorage.Add(coin, this.StorageCoins[coin]); } foreach (var coin in this.UserAcceptedCoins.Keys) { machine.UserCoinsAccepted.Add(coin, this.UserAcceptedCoins[coin]); } this.StorageProducts.ForEach(product => machine.ProductStorage.Add(product)); return(Task.FromResult(0)); }
private void TryToStartSelling() { if (_productRepository.ProductList.All((p) => _productRepository.CountProduct(p.Code) == 0)) { VendingMachineState = VendingMachineState.OutOfStock; DisplayMessageByCode(MessageCode.OutOfStock); } else { VendingMachineState = VendingMachineState.Order; DisplayMessageByCode(MessageCode.ReadyToService); _orderPurchasePanel.On(); } }
public void VendingMachineState_ProductExist() { var vendingMachineState = new VendingMachineState(new VendingCash()) { Products = new Dictionary <Product, int> { { new Product("Mars", 0.85m), 10 }, { new Product("Snickers", 0.85m), 10 } } }; vendingMachineState.ProductExists("Mars").Should().NotBeNull(); vendingMachineState.ProductExists("Snickers").Price.Should().Be(0.85m); vendingMachineState.ProductExists("Snickes").Should().BeNull(); }
public async Task LoadVendingMachine(VendingMachineState machine) { if (File.Exists("machineState.json")) { var json = await this.ReadTextAsync("machineState.json"); var storedMachine = JsonConvert.DeserializeObject <VendingMachineState>(json); machine.CoinStorage = storedMachine.CoinStorage; machine.ProductStorage = storedMachine.ProductStorage; machine.UserCoinsAccepted = storedMachine.UserCoinsAccepted; } else { var loader = new HardcodedVendingMachineLoader(); await loader.LoadVendingMachine(machine); } }
// change the state of the machine private void ChangeState(VendingMachineState stateIn) { // if any of the states had exit actions, we'd deal with them here... // change the state CurrentState = stateIn; // perform the entry action of each state switch (CurrentState) { case VendingMachineState.IDLE: // display a greeting Display("Welcome to the ICT2106 vending machine!"); break; case VendingMachineState.ACCUMULATE: // display the amount of money inserted Display("$" + Amount); break; case VendingMachineState.VEND: // move to the next state right away if (Amount > 0.0M) { // need to give change ChangeState(VendingMachineState.MAKE_CHANGE); } else { // no change required; go back to idle ChangeState(VendingMachineState.IDLE); } break; case VendingMachineState.MAKE_CHANGE: // display a message, reset the amount of money in the machine, then return to idle Display("Returning change of $" + Amount); Amount = 0.0M; ChangeState(VendingMachineState.IDLE); break; } }
public void VendingMachineState_ProcessString_Failed(string input) { var vendingMachineState = new VendingMachineState(new VendingCash()); Assert.Throws <InvalidInputException>(() => vendingMachineState.ProcessInputCoins(input)); }
public SoldOutState(VendingMachineState state) : base(state.Context, state.ReturnTray, state.CoinSlot, state.ProductInfoRepository, state.Output, state.Vault) { }
private static void LoadCoin(VendingMachineState machine, int quantity, double coinDenomination) { machine.CoinStorage.Add(coinDenomination, quantity); }
public async Task SaveMachine(VendingMachineState machine) { var json = JsonConvert.SerializeObject(machine); await this.WriteTextAsync("machineState.json", json); }
protected VendingMachine(IAccountVerificationService accountVerificationService, IOrderManager orderManager) { _accountVerificationService = accountVerificationService; _orderManager = orderManager; State = VendingMachineState.EnoughCan; }