private static void StrategyPattern(DayOfWeek dayOfWeek) { IDiscountStrategy discountStrategy; switch (dayOfWeek) { case DayOfWeek.Monday: { discountStrategy = new HighDiscountStrategy(); break; } case DayOfWeek.Thursday: { discountStrategy = new LowDiscountStrategy(); break; } default: { discountStrategy = new NoDiscountStrategy(); break; } } var mall = new ShoppingMall(discountStrategy); mall.CustomerName = "Raquel"; mall.BillAmount = 400; var discount = (double)mall.GetFinalBill() / mall.BillAmount; Console.WriteLine($"The Customer {mall.CustomerName} is bought {mall.BillAmount}€ and got a discount of {100 - discount * 100}%"); }
public void TestNoDiscountStrategy_0pReduction() { var expectedMoney = new Money(100); var checkMoney = new Money(100); expectedMoney = new NoDiscountStrategy().ApplyItemDiscount(expectedMoney); checkMoney %= 0; Assert.True(expectedMoney.Equals(checkMoney)); }
static void Main(string[] args) { // This example takes the bill amount and calculates the final bill as per the discounted strategy BillCalculateStrategy strategy = null; var currentDay = System.DateTime.Now.DayOfWeek; var customerBillAmount = 1000; if(currentDay == DayOfWeek.Sunday || currentDay == DayOfWeek.Saturday) { strategy = new DiscountStrategy(); } else { strategy = new NoDiscountStrategy(); } var finalBillAmount = strategy.CalculateFinalBill(customerBillAmount); Console.WriteLine("your final bill amount is {0}",finalBillAmount); Console.ReadKey(); }