You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.3 KiB
47 lines
1.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace State_VendingMachineExample
|
|
{
|
|
// Context
|
|
internal class VendingMachine
|
|
{
|
|
private Product product = new Product();
|
|
internal decimal Money { get; set; }
|
|
|
|
internal VMState State { get; set; }
|
|
|
|
internal VMState ReadyState { get; private set; }
|
|
internal VMState HasMoneyState { get; private set; }
|
|
internal VMState DispenseItemState { get; private set; }
|
|
internal VMState DispenseChangeState { get; private set; }
|
|
|
|
public VendingMachine()
|
|
{
|
|
this.Money = 0;
|
|
this.ReadyState = new ReadyState(this);
|
|
this.HasMoneyState = new HasMoneyState(this);
|
|
this.DispenseItemState = new DispenseItemState(this);
|
|
this.DispenseChangeState = new DispenseChangeState(this);
|
|
this.State = this.ReadyState;
|
|
}
|
|
|
|
public void AddMoney(decimal money)
|
|
{
|
|
this.State.AddMoney(money);
|
|
}
|
|
|
|
public void SelectItem(int itemId)
|
|
{
|
|
this.State.SelectItem(itemId);
|
|
}
|
|
|
|
internal decimal? GetPrice(int itemId)
|
|
{
|
|
return product.GetPrice(itemId);
|
|
}
|
|
}
|
|
}
|
|
|