示例#1
0
        public async Task <ActionResult> Index(BabysitterTime BtObj)
        {
            apiUrl             = "http://" + Request.Url.Host + ":" + Request.Url.Port + "/api/TimeCalc";
            client             = new HttpClient();
            client.BaseAddress = new Uri(apiUrl);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl, BtObj);

            string responseData = response.Content.ReadAsStringAsync().Result;

            //decimal chargeTotal = JsonConvert.DeserializeObject<decimal>(responseData);

            if (response.IsSuccessStatusCode)
            {
                BtObj.NightlyCharge = JsonConvert.DeserializeObject <decimal>(responseData);
                ViewBag.ErrorMsg    = "";
            }
            else
            {
                ViewBag.ErrorMsg = "Could not calculate: " + response.StatusCode.ToString();
            }

            return(View(BtObj));
        }
        private decimal Calculate(BabysitterTime BTimeObj)
        {
            bool checkOutIsAfterMidnight = (BTimeObj.CheckOut.Hour < BTimeObj.CheckIn.Hour);

            int lastHourOfDay      = 0;
            int hoursBeforeBedTime = 0;
            int hoursAfterBedTime  = 0;
            int hoursAfterMidnight = 0;

            //Get the number of hours afte midnight, if any,
            //and set the lastHourOfDay, to simplify future math
            if (checkOutIsAfterMidnight)
            {
                hoursAfterMidnight = BTimeObj.CheckOut.Hour;
                lastHourOfDay      = 24;
            }
            else
            {
                hoursAfterMidnight = 0;
                lastHourOfDay      = BTimeObj.CheckOut.Hour;
            }

            //Determine the number of hours before and after bedtime
            if (BTimeObj.Bedtime >= startTime && BTimeObj.Bedtime.Hour <= lastHourOfDay)
            {
                hoursBeforeBedTime = BTimeObj.Bedtime.Hour - BTimeObj.CheckIn.Hour;
                hoursAfterBedTime  = lastHourOfDay - BTimeObj.Bedtime.Hour;
            }
            else
            {
                hoursBeforeBedTime = lastHourOfDay - BTimeObj.CheckIn.Hour;
            }

            return((hoursBeforeBedTime * 12) + (hoursAfterBedTime * 8) + (hoursAfterMidnight * 16));
        }
        public decimal GetNightlyCharge(BabysitterTime BTimeObj)
        {
            decimal nightlyCharge = 0.0M;

            //If the CheckIn or CheckOut times are less than today, they are likely not initialized
            if (BTimeObj.CheckIn < DateTime.Today || BTimeObj.CheckOut < DateTime.Today)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            //Eliminate the extra hours before 5PM
            if (BTimeObj.CheckIn < startTime && BTimeObj.CheckIn > endTime)
            {
                BTimeObj.CheckIn = startTime;
            }

            //Eliminate the extra hours after 4AM
            if (BTimeObj.CheckOut > endTime && BTimeObj.CheckOut < startTime)
            {
                BTimeObj.CheckOut = endTime;
            }

            nightlyCharge = Calculate(BTimeObj);

            return(nightlyCharge);
        }
        public void GetCharge_NoBedtimeWithTimePastMidnight()
        {
            // Arrange with a checkin of 6PM, checkout of 1AM, and no bedtime set
            BabysitterTime babysitterTime = new BabysitterTime();

            babysitterTime.CheckIn  = DateTime.Today.AddHours(18);
            babysitterTime.CheckOut = DateTime.Today.AddHours(1);

            TimeCalcController controller = new TimeCalcController();
            var result = controller.GetNightlyCharge(babysitterTime);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(88.0M, result);
        }
        public void GetCharge_CheckInTooEarlyWithBedTime()
        {
            // Arrange with a checkin of 4PM, checkout of 12AM, and a bedtime of 9PM
            BabysitterTime babysitterTime = new BabysitterTime();

            babysitterTime.CheckIn  = DateTime.Today.AddHours(16);
            babysitterTime.CheckOut = DateTime.Today.AddHours(0);
            babysitterTime.Bedtime  = DateTime.Today.AddHours(21);

            TimeCalcController controller = new TimeCalcController();
            var result = controller.GetNightlyCharge(babysitterTime);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(72.0M, result);
        }
        public void GetCharge_WithBedtimeNoTimePastMidnight()
        {
            // Arrange with a checkin of 6PM, checkout of 11PM, and a bedtime of 9PM
            BabysitterTime babysitterTime = new BabysitterTime();

            babysitterTime.CheckIn  = DateTime.Today.AddHours(18);
            babysitterTime.CheckOut = DateTime.Today.AddHours(23);
            babysitterTime.Bedtime  = DateTime.Today.AddHours(21);

            TimeCalcController controller = new TimeCalcController();
            var result = controller.GetNightlyCharge(babysitterTime);

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(52.0M, result);
        }
        public void GetCharge_WithUninitializedCheckOut()
        {
            // Arrange with a checkin 6PM, checkout not initialized, and a bedtime of 9PM
            BabysitterTime babysitterTime = new BabysitterTime();

            babysitterTime.CheckIn = new DateTime();
            babysitterTime.Bedtime = new DateTime();
            babysitterTime.CheckIn.AddHours(18);
            babysitterTime.Bedtime.AddHours(21);

            TimeCalcController controller = new TimeCalcController();

            try
            {
                var result = controller.GetNightlyCharge(babysitterTime);
            }
            catch (HttpResponseException ex)
            {
                // Assert
                Assert.AreEqual(ex.Response.StatusCode, HttpStatusCode.BadRequest);
            }
        }