public void ApproveExpenseWithInstanceExpenseApprover() { ExpenseHandler mary = new ExpenseHandler(new Employee("Mary Manager", new Decimal(1000))); ApprovalResponse response = mary.Approve(new ExpenseReport(500)); Assert.Equal(ApprovalResponse.Approved, response); }
public void SetUp() { _initialApprover = new ExpenseHandler(new Employee("Donald", decimal.Zero)); var mickey = new ExpenseHandler(new Employee("Mickey", new decimal(1000))); var goofy = new ExpenseHandler(new Employee("Goofy", new decimal(5000))); var daisy = new ExpenseHandler(new Employee("Daisy", new decimal(10000))); _initialApprover.RegisterNext(mickey); mickey.RegisterNext(goofy); goofy.RegisterNext(daisy); }
public void DeclineExpenseForAmountThatNoExpenseHandlerCanApprove() { ExpenseHandler mary = new ExpenseHandler(new Employee("Mary Manager", new Decimal(1000))); ExpenseHandler victor = new ExpenseHandler(new Employee("Victor Vicepres", new Decimal(5000))); mary.RegisterNext(victor); ApprovalResponse response = mary.Approve(new ExpenseReport(15000)); Assert.Equal(ApprovalResponse.Denied, response); }
public void ApproveExpenseWithInstanceNextExpenseHandler() { ExpenseHandler mary = new ExpenseHandler(new Employee("Mary Manager", new Decimal(1000))); ExpenseHandler victor = new ExpenseHandler(new Employee("Victor Vicepres", new Decimal(5000))); mary.RegisterNext(victor); ApprovalResponse response = mary.Approve(new ExpenseReport(1500)); Assert.Equal(ApprovalResponse.Approved, response); }
public IHttpActionResult GetExpensesByExpenseID(ExpenseDetailsRequest request) { if (!ModelState.IsValid) { return(BadRequest()); } var response = ExpenseHandler.GetExpenseDetails(request); return(Ok(response)); }
public void ApproveCorrect(int valueToApprove, ApprovalResponse expected) { var william = new ExpenseHandler(new Employee("William Worker", new decimal(0))); var mary = new ExpenseHandler(new Employee("Mary Manager", new decimal(1000))); var victor = new ExpenseHandler(new Employee("Victor Vicepres", new decimal(5000))); var paula = new ExpenseHandler(new Employee("Paula President", new decimal(20000))); william.RegisterNext(mary); mary.RegisterNext(victor); victor.RegisterNext(paula); var expense = new ExpenseReport(valueToApprove); var actual = william.Approve(expense); Assert.Equal(expected, actual); }
public void Should_Deny_An_Expense() { //Arrange ExpenseHandler gwen = new ExpenseHandler(new Employee("Gwen Stacy", 45000)); ExpenseHandler harry = new ExpenseHandler(new Employee("Harry Osborn", 50000)); ExpenseHandler norman = new ExpenseHandler(new Employee("Norman Osborn", 8000000)); gwen.SetNext(harry); harry.SetNext(norman); var expenseAmount = 9000000; IExpenseReport expenseReport = new ExpenseReport(expenseAmount); //Act var response = harry.Approve(expenseReport); //Assert Assert.Equal(ApprovalResponse.Denied, response); }
public static void Execute() { IExpenseHandler YigitAli = new ExpenseHandler(new Employee("Yiğit Ali", 500)); IExpenseHandler Aysun = new ExpenseHandler(new Employee("Aysun", 1000)); IExpenseHandler Halit = new ExpenseHandler(new Employee("Halit", 10000)); YigitAli.RegisterNext(Aysun); Aysun.RegisterNext(Halit); decimal expenseReportAmount; if (ConsoleInput.TryReadDecimal("Expense report amount: ", out expenseReportAmount)) { string approverName; IExpenseReport expenseReport = new ExpenseReport(expenseReportAmount); ApprovalResponse response = YigitAli.Approve(expenseReport, out approverName); string approvedBy = !string.IsNullOrEmpty(approverName) ? $" by {approverName}." : "."; Console.WriteLine($"The request was {response}{approvedBy}"); } }
public static void Run() { var head = new ExpenseHandler(new Employee("1st Level Manager", 1000)) .SetNext(new ExpenseHandler(new Employee("2nd Level Manager", 10000))) .SetNext(new ExpenseHandler(new Employee("3nd Level Manager", 100000))) .SetNext(new ExpenseHandler(new Employee("C Level Manager", 1000000))); var smallCost = 500; var largeCost = 95000; var hugeCost = 950000; var massiveCost = 1500000; var smallExpense = new ExpenseReport(smallCost, worthIt: true); var largeExpense = new ExpenseReport(largeCost, worthIt: false); var hugeExpense = new ExpenseReport(hugeCost, worthIt: true); var massiveExpense = new ExpenseReport(massiveCost, worthIt: true); Console.WriteLine($"{smallCost:C} expense: {head.Approve(smallExpense)}"); Console.WriteLine($"{largeCost:C} expense: {head.Approve(largeExpense)}"); Console.WriteLine($"{hugeCost:C} expense: {head.Approve(hugeExpense)}"); Console.WriteLine($"{massiveCost:C} expense: {head.Approve(massiveExpense)}"); }
static void Main(string[] args) { ExpenseHandler william = new ExpenseHandler(new Employee("William Worker", Decimal.Zero)); ExpenseHandler mary = new ExpenseHandler(new Employee("Mary Managerr", 1000)); ExpenseHandler victor = new ExpenseHandler(new Employee("Victor VP", 5000)); ExpenseHandler paula = new ExpenseHandler(new Employee("Paula President", 20000)); william.RegisterNext(mary); mary.RegisterNext(victor); victor.RegisterNext(paula); Decimal expenseAmount; Decimal.TryParse(Console.ReadLine(), out expenseAmount); var expense = new Expense(expenseAmount); ApprovalResponse response = william.Approve(expense); Console.WriteLine($"The expense was {response}"); Console.ReadKey(); }
public static void ChainOfCommand() { ExpenseHandler william = new ExpenseHandler(new Employee(0)); ExpenseHandler mary = new ExpenseHandler(new Employee(1000)); ExpenseHandler victor = new ExpenseHandler(new Employee(5000)); ExpenseHandler paula = new ExpenseHandler(new Employee(20000)); william.RegisterNext(mary); mary.RegisterNext(victor); paula.RegisterNext(paula); Console.WriteLine("Please enter the expense amount;"); var expenseAmount = decimal.Parse(Console.ReadLine()); var response = william.Approve(new ExpenseReport() { Total = expenseAmount }); Console.WriteLine(response); Console.ReadKey(); }
private static void DemoChainOfResponsibilityPattern() { #region Problem of Logic to handle expenseReport is captured at wrong level. /* Caller is responsible for iterating over the list. * This means, business logic of how expenseReport is promoted in Management chain is captured at the wrong level. * I should not be worried about getting approval of my expense report in Management chain. My manager should do it for me. * With the Chain of Responsibility pattern, we'll able to achieve the abstraction required behind the scenes. */ //var employees = new List<Employee> //{ // new Employee("William Worker", decimal.Zero), // new Employee("Mary Manager", 1000), // new Employee("Victor VicePres", 5000), // new Employee("Paula President", 20000), //}; //decimal expenseReportAmount; //while(ConsoleInput.TryReadDecimal("Expense report amount:", out expenseReportAmount)) //{ // IExpenseReport expense = new ExpenseReport(expenseReportAmount); // bool expenseProcessed = false; // foreach (var approver in employees) // { // var response = approver.ApproveResponse(expense); // if (response != ApprovalResponse.BeyondApprovalLimit) // { // Console.WriteLine($"The request was {response}."); // expenseProcessed = true; // break; // } // } // if (!expenseProcessed) // { // Console.WriteLine("No one was able to approve your expense."); // } //} #endregion #region Solution through Chain of Responsibility pattern ExpenseHandler william = new ExpenseHandler(new Employee("William Worker", decimal.Zero)); ExpenseHandler mary = new ExpenseHandler(new Employee("Mary Manager", 1000)); ExpenseHandler victor = new ExpenseHandler(new Employee("Victor VicePres", 5000)); ExpenseHandler paula = new ExpenseHandler(new Employee("Paula President", 20000)); william.RegisterNext(mary); mary.RegisterNext(victor); victor.RegisterNext(paula); while (ConsoleInput.TryReadDecimal("Expense report amount:", out decimal expenseReportAmount)) { IExpenseReport expense = new ExpenseReport(expenseReportAmount); ApprovalResponse response = william.Approve(expense); Console.WriteLine($"The request was {response}."); } #endregion }
public ExpenseController(ExpenseHandler handler, IExpenseRepository expenseRepository) { _handler = handler; _expenseRepository = expenseRepository; }
public SaveExpenseResponse EvaluateExpense(ManageExpenseRequest request) { return(ExpenseHandler.ManageExpenses(request)); }
public SaveExpenseResponse SaveExpense(SaveExpenseRequest request) { return(ExpenseHandler.SubmitExpenseToDatabase(request)); }
public ExpenseResponse GetExpensesByUserID(GetExpensesRequest request) { return(ExpenseHandler.GetAllExpenses(request)); }