Esempio n. 1
0
        public ParkingRateDto Calculate(IEnumerable <Standard> standardRates, DateTime start, DateTime end)
        {
            var result = new ParkingRateDto()
            {
                Name = CONSTANTS.RATE.STANDARD
            };

            var resultStandardMacro = 0.0;
            var resultStandardMicro = 0.0;
            var isStandard          = false;
            var duration            = (end - start).TotalHours;
            var maxStandardRate     = standardRates.OrderBy(nr => nr.MaxHours).LastOrDefault();

            // More than max duration
            if (duration >= maxStandardRate.MaxHours)
            {
                resultStandardMacro = Math.Floor(duration / maxStandardRate.MaxHours) * maxStandardRate.Rate;
                duration            = duration % maxStandardRate.MaxHours;
            }
            if (duration > 0)
            {
                foreach (var standardRate in standardRates)
                {
                    if (!isStandard && duration <= standardRate.MaxHours)
                    {
                        isStandard          = true;
                        resultStandardMicro = standardRate.Rate;
                    }
                }
            }

            result.Price = resultStandardMacro + resultStandardMicro;

            return(result);
        }
        private ParkingRateDto calculateNormal(IEnumerable <Normal> normalRates, DateTime start, DateTime end)
        {
            var result = new ParkingRateDto()
            {
                Name = CONSTANTS.RATE.STANDARD
            };

            var resultNormalMacro = 0.0;
            var resultNormalMicro = 0.0;
            var isNormal          = false;
            var duration          = (end - start).TotalHours;
            var maxNormalRate     = normalRates.OrderBy(nr => nr.MaxHours).LastOrDefault();

            // More than max duration
            if (duration >= maxNormalRate.MaxHours)
            {
                resultNormalMacro = Math.Floor(duration / maxNormalRate.MaxHours) * maxNormalRate.Rate;
                duration          = duration % maxNormalRate.MaxHours;
            }
            if (duration > 0)
            {
                foreach (var normalRate in normalRates)
                {
                    if (!isNormal && duration <= normalRate.MaxHours)
                    {
                        isNormal          = true;
                        resultNormalMicro = normalRate.Rate;
                    }
                }
            }

            result.Price = resultNormalMacro + resultNormalMicro;

            return(result);
        }
Esempio n. 3
0
        public async Task <ParkingRateDto> Calculate(TimerDto input)
        {
            var specialRates = await _specialService.GetAllAsync();

            var normalRates = await _normalService.GetAllAsync();

            var result = new ParkingRateDto();

            // TODO: Refactor this with Abstraction like IValidator
            // TODO: and orchestrate with Chain of Responsibility pattern
            var resultSpecial = _specialService.Calculate(specialRates, input.Entry, input.Exit);

            result = resultSpecial;

            var resultNormal = _normalService.Calculate(normalRates, input.Entry, input.Exit);

            // choose for either normal vs special
            if (resultNormal.Price > 0 && (result.Price == 0 || result.Price > resultNormal.Price))
            {
                result.Name  = resultNormal.Name;
                result.Price = resultNormal.Price;
            }

            return(result);
        }
        // TODO: Refactor this with Abstraction like IValidator
        // TODO: and orchestrate with Chain of Responsibility pattern
        public ParkingRateDto Calculate(IEnumerable <Special> specialRates, DateTime start, DateTime end)
        {
            var result = new ParkingRateDto();

            foreach (var specialRate in specialRates)
            {
                var counter = 0;

                // Entry
                bool isSpecial = (specialRate.Entry.Start <= start.TimeOfDay && start.TimeOfDay <= specialRate.Entry.End) ||
                                 (specialRate.MaxDays > 0 &&
                                  (specialRate.Entry.Start <= start.TimeOfDay &&
                                   start.TimeOfDay <= specialRate.Entry.End.Add(TimeSpan.FromDays(1))) ||
                                  (specialRate.Entry.Start.Subtract(TimeSpan.FromDays(1)) <= start.TimeOfDay &&
                                   start.TimeOfDay <= specialRate.Entry.End));


                if (
                    !specialRate.Entry.Days.Any(
                        d => string.Equals(d, start.DayOfWeek.ToString(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    isSpecial = false;
                }

                // Exit
                var maxExitDay = start.AddDays(specialRate.MaxDays);
                var maxExit    = new DateTime(maxExitDay.Year, maxExitDay.Month, maxExitDay.Day, specialRate.Exit.End.Hours,
                                              specialRate.Exit.End.Minutes, 0);
                if (end > maxExit)
                {
                    isSpecial = false;
                }

                if (!specialRate.Exit.Days.Any(
                        d => string.Equals(d, end.DayOfWeek.ToString(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    isSpecial = false;
                }

                // Max days
                if ((end - start).Days > specialRate.MaxDays)
                {
                    isSpecial = false;
                }

                if (isSpecial)
                {
                    if (result.Price == 0 || result.Price > specialRate.TotalPrice)
                    {
                        result.Name  = specialRate.Name;
                        result.Price = specialRate.TotalPrice;
                    }
                }
            }

            return(result);
        }
        public void UpdateParkingRate(ParkingRateDto parkingRate)
        {
            var rate = _context.ParkingRates.Where(p => p.Id == parkingRate.Id).FirstOrDefault();

            if (rate != null)
            {
                rate.Hours  = parkingRate.Hours;
                rate.IsUsed = parkingRate.IsUsed;
                rate.Order  = parkingRate.Order;
                rate.Rate   = parkingRate.Rate;
                _context.SaveChanges();
            }
        }
        public void AddParkingRate(ParkingRateDto parkingRateDto)
        {
            //throw new NotImplementedException();
            ParkingRate parkingRate = new ParkingRate()
            {
                Hours  = parkingRateDto.Hours,
                IsUsed = 1,
                Order  = parkingRateDto.Order,
                Rate   = parkingRateDto.Rate
            };

            _context.ParkingRates.Add(parkingRate);
            _context.SaveChanges();
        }
        //Todo: transfer the calculation logic to the Domain project (ParkingManager)
        private async Task <ParkingRateDto> calculate(TimerDto input)
        {
            var normalRates = await _normalRepository.SelectAllAsync();

            var specialRates = await _specialRepository.SelectAllAsync();

            var result = new ParkingRateDto();

            var resultSpecial = calculateSpecial(specialRates, input.Entry, input.Exit);

            result = resultSpecial;

            var resultNormal = calculateNormal(normalRates, input.Entry, input.Exit);

            if (resultNormal.Price > 0 && (result.Price == 0 || result.Price > resultNormal.Price))
            {
                result.Name  = resultNormal.Name;
                result.Price = resultNormal.Price;
            }

            return(result);
        }
        public async Task <ParkingRateDto> Calculate(TimerDto input)
        {
            var specialRates = await _specialService.GetAllAsync();

            var standardRates = await _standardService.GetAllAsync();

            var result = new ParkingRateDto();

            var resultSpecial = _specialService.Calculate(specialRates, input.Entry, input.Exit);

            result = resultSpecial;

            var resultStandard = _standardService.Calculate(standardRates, input.Entry, input.Exit);

            // select the lowest price
            if (resultStandard.Price > 0 && (result.Price == 0 || result.Price > resultStandard.Price))
            {
                result.Name  = resultStandard.Name;
                result.Price = resultStandard.Price;
            }

            return(result);
        }