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.
39 lines
1021 B
39 lines
1021 B
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace State_VendingMachineExample
|
|
{
|
|
internal class DispenseItemState : VMState
|
|
{
|
|
public DispenseItemState(VendingMachine context)
|
|
{
|
|
vendingMachine = context;
|
|
}
|
|
|
|
public override void AddMoney(decimal money)
|
|
{
|
|
throw new ApplicationException();
|
|
}
|
|
|
|
public override void ReturnChange(decimal money)
|
|
{
|
|
throw new ApplicationException();
|
|
}
|
|
|
|
public override void SelectItem(int itemId)
|
|
{
|
|
decimal? price = vendingMachine.GetPrice(itemId).Value;
|
|
|
|
// Dispense item here
|
|
Console.WriteLine($"Dispense Item#{itemId} ({price.Value})");
|
|
|
|
vendingMachine.Money -= price.Value;
|
|
|
|
vendingMachine.State = vendingMachine.DispenseChangeState;
|
|
vendingMachine.State.ReturnChange(vendingMachine.Money);
|
|
}
|
|
}
|
|
}
|
|
|