/// <summary> /// User presses "buy now" /// </summary> public void ConfirmSell() { Error = null; if (_state != ControlUnitState.Transaction) // if not in payment mode - do nothing { if (_state == ControlUnitState.Ready) { Error = "CHOOSE DRINK FIRST!"; RaiseDisplayChanged(); } return; } if (TransactionAmount100 < ChosenProduct.Price100) // if there's not enough money - do nothing { Error = "NOT ENOUGH MONEY!"; RaiseDisplayChanged(); return; } var change = _cashBox.GiveChange(TransactionAmount - ChosenProduct.Price); // get change if (change == null) //there no change { change = _cashBox.GiveChange(TransactionAmount); // return transaction amount back if (change == null) { Error = "NO CASH FOR CHANGE"; RaiseDisplayChanged(); } else { foreach (var coin in change) { RaiseCashReturn(coin); } } _state = ControlUnitState.Ready; } else { foreach (var coin in change) // return change { RaiseCashReturn(coin); } ChosenProduct.TotalSold++; // update product statistics ChosenProduct.Count--; if (ChosenProduct.Count == 0) { ChosenProduct.OutOfStock = true; RaiseOutOfStock(ProductSlot); } StartProductRelease(); // start releasing product _state = ControlUnitState.Release; } RaiseDisplayChanged(); }
private ControlUnit() { _cashBox = new CashBox(); _vendingProcessor = new VendingProcessor(); _state = ControlUnitState.Ready; }
/// <summary> /// Moves machine to operational mode /// </summary> public void ToOperational() { Error = null; _state = ControlUnitState.Ready; RaiseOnRestart(); RaiseDisplayChanged(); }
/// <summary> /// Moves machine to maintenance mode /// </summary> public void ToMaintenance() { Refund(); _state = ControlUnitState.Maintenance; RaiseDisplayChanged(); }
/// <summary> /// User chooses drink /// </summary> /// <param name="slot"></param> public void ChooseDrink(int slot) { Error = null; if (_state == ControlUnitState.Ready || (_state == ControlUnitState.Transaction && TransactionAmount100 == 0)) // User can choose drink in appropriate mode or if he didn't start paying { if (slot >= _vendingProcessor.ProductSlots.Length) { return; } var p = _vendingProcessor.ProductSlots[slot]; if (p.OutOfStock || p.Count == 0) { Error = "OUT OF STOCK"; RaiseDisplayChanged(); return; } ProductSlot = slot; _state = ControlUnitState.Transaction; // Move to payment mode TransactionAmount100 = 0; NoChange = false; RaiseDisplayChanged(); } else if (_state == ControlUnitState.Transaction && TransactionAmount100 > 0) { Error = "PRESS REFUND \n FOR ANOTHER DRINK"; RaiseDisplayChanged(); } }
/// <summary> /// Starts product release procedure to feel realistic process (takes 3 seconds) /// </summary> private void StartProductRelease() { _state = ControlUnitState.Release; var timer = new Timer(3000); timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); timer.Start(); }
private void timer_Elapsed(object sender, ElapsedEventArgs e) { ((Timer)sender).Enabled = false; _state = ControlUnitState.Finalize; Dispatcher.Invoke(new Action(delegate() { RaiseProductRelease(ChosenProduct); RaiseDisplayChanged(); })); }
/// <summary> /// User presses refund button /// </summary> public void Refund() { Error = null; if (_state == ControlUnitState.Transaction) // if was in payment mode { var coins = _cashBox.GiveChange(TransactionAmount); // calculate minimum number of coins to return as if it was a change for full transaction amount if (coins != null) { foreach (var coin in coins) { RaiseCashReturn(coin); // return each coin } } else { //error! } _state = ControlUnitState.Ready; RaiseDisplayChanged(); } }
/// <summary> /// User took the product /// </summary> public void SellFinished() { Error = null; _state = ControlUnitState.Ready; RaiseDisplayChanged(); }