public PriceCalculatorTest() { var discountConfig = GetTestDiscountConfig(); var rules = DiscountHelper.GetDiscountRuleFromConfig(discountConfig); _calculator = new PriceCalculator(rules); }
public void GetValidConfigTest() { var _config = GetTestDiscountConfig(); var rules = DiscountHelper.GetDiscountRuleFromConfig(_config); Assert.IsType <List <IDiscountRule> >(rules); Assert.Equal(4, rules.Count); var firstRule = rules.First(); Assert.IsType <Discount10>(firstRule); Assert.Equal("Discount 10%", firstRule.AssignedName); }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); IConfiguration config = builder.Build(); var discountConfig = new Discount(); config.GetSection("Discount").Bind(discountConfig); var discountRules = DiscountHelper.GetDiscountRuleFromConfig(discountConfig); var calculator = new PriceCalculator(discountRules); //due to important part is Price calculator model, so skip input args and validation here //assume all data is correct and invalid data will be reject before this Console.WriteLine("-- Start Calculate input"); Console.WriteLine("Test with 1 person, code DIS10, price 500"); var bestRule = calculator.GetBestRule(1, "DIS10", 500); if (!string.IsNullOrEmpty(bestRule.Name)) { Console.WriteLine("-Match with promotion : " + bestRule.Name.ToString()); Console.WriteLine("-Amount : " + bestRule.Price.ToString()); } Console.WriteLine("Test with 1 person, code STARCARD, price 2000"); bestRule = calculator.GetBestRule(1, "STARCARD", 2000); if (!string.IsNullOrEmpty(bestRule.Name)) { Console.WriteLine("-Match with promotion : " + bestRule.Name.ToString()); Console.WriteLine("-Amount : " + bestRule.Price.ToString()); } Console.WriteLine("Test with 2 person, code DIS10, price 1500"); bestRule = calculator.GetBestRule(2, "DIS10", 1500); if (!string.IsNullOrEmpty(bestRule.Name)) { Console.WriteLine("-Match with promotion : " + bestRule.Name.ToString()); Console.WriteLine("-Amount : " + bestRule.Price.ToString()); } Console.WriteLine("-- End Calculation"); }