//Method that process the request or pass is to the next handler public virtual bool DispenseRequest(int amount) { if (amount >= Denomination) { var num = Quantity; var remainder = amount; while (remainder >= Denomination && num > 0) { remainder -= Denomination; num--; } if (remainder != 0) { return(next.DispenseRequest(remainder)); } return(true); } else { return(next.DispenseRequest(amount)); } }
static void Main(string[] args) { //create handlers var bills50s = new CurrencyBill(50, 1); var bills20s = new CurrencyBill(20, 2); var bills10s = new CurrencyBill(10, 5); //set handlers pipeline bills50s.RegisterNext(bills20s) .RegisterNext(bills10s); //client code that uses handler while (true) { Console.WriteLine("Please enter amount to dispense:"); var isParsed = int.TryParse(Console.ReadLine(), out var amount); if (isParsed) { //sender pass the request to first handler in the pipeline var isDepensible = bills50s.DispenseRequest(amount); if (isDepensible) { Console.WriteLine($"Your amount ${amount} is dispensable!"); } else { Console.WriteLine($"Failed to dispense ${amount}!"); } } else { Console.WriteLine("Please enter a valid amount to dispense"); } } }