Пример #1
0
        private void ValidateRequest(PriceRequest request, ref string errorMessage)
        {
            if (request.RiskData == null)
            {
                errorMessage = PriceRequestErrors.Risk_Data_Missing;
                return;
            }

            if (string.IsNullOrEmpty(request.RiskData.FirstName))
            {
                errorMessage = PriceRequestErrors.First_Name_Required;
                return;
            }

            if (string.IsNullOrEmpty(request.RiskData.LastName))
            {
                errorMessage = PriceRequestErrors.Surname_Required;
                return;
            }

            if (request.RiskData.Value == 0)
            {
                errorMessage = PriceRequestErrors.Value_Required;
                return;
            }
        }
Пример #2
0
        //pass request with risk data with details of a gadget, return the best price retrieved from 3 external quotation engines
        public PriceResponse GetPrice(PriceRequest request, out string errorMessage)
        {
            errorMessage = string.Empty;

            var result = new PriceResponse();

            this.ValidateRequest(request, ref errorMessage);
            if (errorMessage.Length > 0)
            {
                return(result);
            }

            //now call 3 external system and get the best price
            foreach (var quoteSys in this.GetQuotationSystems(request))
            {
                var systemResponse = quoteSys.GetPrice(request);
                if (this.IsResponseValid(systemResponse) && systemResponse.Price < result.Price)
                {
                    result.Price       = Math.Min(systemResponse.Price, result.Price);
                    result.InsurerName = systemResponse.Name;
                    result.Tax         = systemResponse.Tax;
                }
            }

            return(result);
        }
Пример #3
0
        static void Main(string[] args)
        {
            //SNIP - collect input (risk data from the user)

            var request = new PriceRequest()
            {
                RiskData = new RiskData() //hardcoded here, but would normally be from user input above
                {
                    DOB       = DateTime.Parse("1980-01-01"),
                    FirstName = "John",
                    LastName  = "Smith",
                    Make      = "Cool New Phone",
                    Value     = 500
                }
            };

            var priceEngine   = new PriceEngine();
            var error         = string.Empty;
            var priceResponse = priceEngine.GetPrice(request, out error);

            if (priceResponse.Price == Decimal.MaxValue)
            {
                Console.WriteLine(String.Format("There was an error - {0}", error));
            }
            else
            {
                Console.WriteLine(String.Format("You price is {0}, from insurer: {1}. This includes tax of {2}",
                                                priceResponse.Price, priceResponse.InsurerName, priceResponse.Tax));
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Пример #4
0
        private bool IsValidMakeForSystem2(PriceRequest request)
        {
            var makeList = new List <string> {
                "examplemake1", "examplemake2", "examplemake3"
            };

            return(makeList.IndexOf(request.RiskData.Make) >= 0);
        }
Пример #5
0
        private IList <IQuotationSystem> GetQuotationSystems(PriceRequest request)
        {
            var quotationSystems = new List <IQuotationSystem>();

            if (this.IsValidMakeForSystem1(request))
            {
                quotationSystems.Add(new QuotationSystem1("http://quote-system-1.com", "1234"));
            }

            if (this.IsValidMakeForSystem2(request))
            {
                quotationSystems.Add(new QuotationSystem2("http://quote-system-2.com", "1235"));
            }

            quotationSystems.Add(new QuotationSystem3("http://quote-system-3.com", "100"));

            return(quotationSystems);
        }
Пример #6
0
        static async Task Main(string[] args)
        {
            DateTime?DOB   = null;
            decimal? value = null;

            if (DateTime.TryParse(args[4], out var parsedDOB))
            {
                DOB = parsedDOB;
            }
            if (decimal.TryParse(args[3], out var parsedValue))
            {
                value = parsedValue;
            }

            var request = new PriceRequest
            {
                RiskData = new RiskData //hardcoded here, but would normally be from user input above
                {
                    DOB       = DOB,
                    FirstName = args[0],
                    LastName  = args[1],
                    Make      = args[2],
                    Value     = value
                }
            };

            var priceEngine = DIContainer.ResolvePriceEngine();
            var price       = await priceEngine.GetPrice(request);

            Console.WriteLine(!price.Success
                ? $"There was an error - {price.SquashErrors()}"
                : $"You price is {price.Value.Price}, from insurer: {price.Value.InsurerName}. This includes tax of {price.Value.Tax}");

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
Пример #7
0
 private bool IsValidMakeForSystem1(PriceRequest request)
 {
     return(request.RiskData.DOB.HasValue);
 }
Пример #8
0
        //pass request with risk data with details of a gadget, return the best price retrieved from 3 external quotation engines
        public decimal GetPrice(PriceRequest request, out decimal tax, out string insurerName, out string errorMessage)
        {
            //initialise return variables
            tax          = 0;
            insurerName  = "";
            errorMessage = "";

            //validation
            if (request.RiskData == null)
            {
                errorMessage = "Risk Data is missing";
                return(-1);
            }

            if (String.IsNullOrEmpty(request.RiskData.FirstName))
            {
                errorMessage = "First name is required";
                return(-1);
            }

            if (String.IsNullOrEmpty(request.RiskData.LastName))
            {
                errorMessage = "Surname is required";
                return(-1);
            }

            if (request.RiskData.Value == 0)
            {
                errorMessage = "Value is required";

                return(-1);
            }


            //now call 3 external system and get the best price
            decimal price = 0;

            //system 1 requires DOB to be specified
            if (request.RiskData.DOB != null)
            {
                QuotationSystem1 system1 = new QuotationSystem1("http://quote-system-1.com", "1234");

                dynamic systemRequest1 = new ExpandoObject();
                systemRequest1.FirstName = request.RiskData.FirstName;
                systemRequest1.Surname   = request.RiskData.LastName;
                systemRequest1.DOB       = request.RiskData.DOB;
                systemRequest1.Make      = request.RiskData.Make;
                systemRequest1.Amount    = request.RiskData.Value;

                dynamic system1Response = system1.GetPrice(systemRequest1);
                if (system1Response.IsSuccess)
                {
                    price       = system1Response.Price;
                    insurerName = system1Response.Name;
                    tax         = system1Response.Tax;
                }
            }

            //system 2 only quotes for some makes
            if (request.RiskData.Make == "examplemake1" || request.RiskData.Make == "examplemake2" ||
                request.RiskData.Make == "examplemake3")
            {
                dynamic systemRequest2 = new ExpandoObject();
                systemRequest2.FirstName = request.RiskData.FirstName;
                systemRequest2.LastName  = request.RiskData.LastName;
                systemRequest2.Make      = request.RiskData.Make;
                systemRequest2.Value     = request.RiskData.Value;

                QuotationSystem2 system2 = new QuotationSystem2("http://quote-system-2.com", "1235", systemRequest2);

                dynamic system2Response = system2.GetPrice();
                if (system2Response.HasPrice && system2Response.Price < price)
                {
                    price       = system2Response.Price;
                    insurerName = system2Response.Name;
                    tax         = system2Response.Tax;
                }
            }

            //system 3 is always called
            QuotationSystem3 system3        = new QuotationSystem3("http://quote-system-3.com", "100");
            dynamic          systemRequest3 = new ExpandoObject();

            systemRequest3.FirstName = request.RiskData.FirstName;
            systemRequest3.Surname   = request.RiskData.LastName;
            systemRequest3.DOB       = request.RiskData.DOB;
            systemRequest3.Make      = request.RiskData.Make;
            systemRequest3.Amount    = request.RiskData.Value;

            var system3Response = system3.GetPrice(systemRequest3);

            if (system3Response.IsSuccess && system3Response.Price < price)
            {
                price       = system3Response.Price;
                insurerName = system3Response.Name;
                tax         = system3Response.Tax;
            }

            if (price == 0)
            {
                price = -1;
            }

            return(price);
        }