コード例 #1
0
        public void TestBestTransporterFail4()
        {
            TransporterQueryInput tqi = new TransporterQueryInput();

            tqi.Time     = 1019; //4:59pm
            tqi.Distance = 1;
            tqi.RefrigerationRequired = true;
            IConfigurationRoot        config    = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            BestTransporterController btc       = new BestTransporterController(config);
            BestTransporter           bestTrans = btc.GetBestTransporter(tqi);

            Assert.IsTrue(!bestTrans.FoundTransporterToUse, "Expected not to find transporter");
        }
コード例 #2
0
        /*
         *  Get the Best Transporter to use for the given input data.
         */
        public BestTransporter GetBestTransporter(TransporterQueryInput userData)
        {
            // Get the transporters from the repository - This data could be cached for better performance if we had thousands of transporters
            string transportersFilename        = _config.GetSection("AppSettings")["TransportRepositoryFilename"];
            TransporterRepository repo         = new TransporterRepository(transportersFilename);
            List <Transporter>    transporters = repo.GetTransporters();

            // Find the Best Transporter and the cost to transport.
            double?cost            = null;
            string transporterName = "";

            foreach (Transporter t in transporters)
            {
                // first see if you can use current transporter
                if (userData.Time >= t.StartTime && userData.Time <= t.EndTime)
                {
                    // check for refrigeration not needed (all transporters allowed) or if Refridge needed only use transporters with Refridge cablable transports
                    if (userData.RefrigerationRequired == false || userData.RefrigerationRequired == t.RefridgeratedBox)
                    {
                        // calc cost and see if this is the lowest cost transporter
                        double currCost = userData.Distance * t.CostPerMile;
                        if (cost == null || currCost < cost)
                        {
                            cost            = currCost;
                            transporterName = t.Name;
                        }
                    }
                }
            }

            // check if we found a transporter
            BestTransporter bestTrans = new BestTransporter();

            if (cost == null || transporterName.Length == 0)
            {
                // no transporters available for this time.
                bestTrans.Error = "No Transporter is available";
                bestTrans.FoundTransporterToUse = false;
            }
            else
            {
                // Found a transporter to use
                bestTrans.Error = "";
                bestTrans.FoundTransporterToUse = true;
            }
            // return Name and Cost if data is available.
            bestTrans.Name = transporterName;
            bestTrans.Cost = cost;
            return(bestTrans);
        }
コード例 #3
0
        public void TestBestTransporterSucceed5()
        {
            TransporterQueryInput tqi = new TransporterQueryInput();

            tqi.Time     = 781; //1:01pm
            tqi.Distance = 3;
            tqi.RefrigerationRequired = true;
            IConfigurationRoot        config    = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            BestTransporterController btc       = new BestTransporterController(config);
            BestTransporter           bestTrans = btc.GetBestTransporter(tqi);

            Assert.IsTrue(bestTrans.FoundTransporterToUse, "Expected to find transporter");
            Assert.IsTrue(bestTrans.Name == "Geoff", "Expected to find Geoff as transporter");
            Assert.IsTrue(bestTrans.Cost == 6.00, "Expected cost to be $6.00");
        }
コード例 #4
0
        public void TestBestTransporterSucceed3()
        {
            TransporterQueryInput tqi = new TransporterQueryInput();

            tqi.Time     = 960; //3pm
            tqi.Distance = 1;
            tqi.RefrigerationRequired = false;
            IConfigurationRoot        config    = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
            BestTransporterController btc       = new BestTransporterController(config);
            BestTransporter           bestTrans = btc.GetBestTransporter(tqi);

            Assert.IsTrue(bestTrans.FoundTransporterToUse, "Expected to find transporter");
            Assert.IsTrue(bestTrans.Name == "Martin", "Expected to find Martin as transporter");
            Assert.IsTrue(bestTrans.Cost == 1.50, "Expected cost to be $1.50");
        }
コード例 #5
0
        /*
         * Validate all the input (Time, Distance and Refridge Required)
         */
        public Tuple <TransporterQueryInput, string> ValidateAll(TransporterViewInput userInput)
        {
            string error = "";
            TransporterQueryInput tqi = new TransporterQueryInput();

            Tuple <int, string> tupTime = ValidateTime(userInput.Time);

            if (tupTime.Item2.Length > 0)
            {
                tqi.Time = -1;
                error   += tupTime.Item2;
            }
            else
            {
                tqi.Time = tupTime.Item1;
            }

            Tuple <int, string> tupDist = ValidateDistance(userInput.Distance);

            if (tupDist.Item2.Length > 0)
            {
                tqi.Distance = -1;
                error       += tupDist.Item2;
            }
            else
            {
                tqi.Distance = tupDist.Item1;
            }

            Tuple <bool, string> tupRefrig = ValidateRefridgerationRequired(userInput.RefrigerationRequired);

            if (tupRefrig.Item2.Length > 0)
            {
                tqi.RefrigerationRequired = false;
                error += tupRefrig.Item2;
            }
            else
            {
                tqi.RefrigerationRequired = tupRefrig.Item1;
            }

            return(new Tuple <TransporterQueryInput, string>(tqi, error));
        }