Beispiel #1
0
        public Charge CalculateSubsidy(Volume volume, Tariff tariff)
        {
            var currentCharge = new Charge();

            if (IsDataCorrect(tariff, volume) == true)
            {
                OnNotify?.Invoke(this, $"Расчёт начат в {DateTime.Now}");
                currentCharge.Value     = volume.Value * tariff.Value;
                currentCharge.HouseId   = tariff.HouseId;
                currentCharge.ServiceId = tariff.ServiceId;
                currentCharge.Month     = volume.Month;
                OnNotify?.Invoke(this, $"Расчёт успешно завершён в {DateTime.Now}");
                return(currentCharge);
            }
            OnException?.Invoke(this, new Tuple <string, Exception>("some message", _ex));
            return(null);
        }
Beispiel #2
0
        public Charge CalculateSubsidy(Volume volumes, Tariff tariff)
        {
            OnNotify?.Invoke(this, $"Расчёт начат в {DateTime.UtcNow:G}");

            if (volumes.ServiceId != tariff.ServiceId)
            {
                InvalidInput("ServiceId", new Exception("Идентификаторы услуг у объёма и у тарифа не совпадают!"));
            }
            if (volumes.HouseId != tariff.HouseId)
            {
                InvalidInput("HouseId", new Exception("Идентификаторы домов у объёма и у тарифа не совпадают!"));
            }
            if (!(tariff.PeriodBegin.Month <= volumes.Month.Month && volumes.Month.Month <= tariff.PeriodEnd.Month))
            {
                InvalidInput("Month", new Exception("Месяц объёма не входит в период действия тарифа!"));
            }
            if (tariff.Value <= 0)
            {
                InvalidInput("Tariff Value", new Exception("Нулевое или отрицательное значение тарифа!"));
            }
            if (volumes.Value < 0)
            {
                InvalidInput("Volumes Value", new Exception("Отрицательное значение объема!"));
            }

            Charge charge = null;

            try
            {
                charge           = new Charge();
                charge.ServiceId = volumes.ServiceId;
                charge.HouseId   = volumes.HouseId;
                charge.Month     = volumes.Month;
                charge.Value     = volumes.Value * tariff.Value;
            }
            catch (Exception ex)
            {
                OnException?.Invoke(this, new Tuple <string, Exception>("Subsidy calculation error! Message: ", ex));
                throw;
            }

            OnNotify?.Invoke(this, $"Расчёт успешно завершён в {DateTime.UtcNow:G}");

            return(charge);
        }
 public Charge CalculateSubsidy(Volume volumes, Tariff tariff)
 {
     OnNotify?.Invoke(this, $"Расчёт начат {DateTime.Now}");
     if (volumes.HouseId != tariff.HouseId)
     {
         OnException?.Invoke(this, new Tuple <string, Exception>(null, new Exception("Error in HouseId")));
         throw new Exception("Error in HouseId");
     }
     else if (volumes.ServiceId != tariff.ServiceId)
     {
         OnException?.Invoke(this, new Tuple <string, Exception>(null, new Exception("Error in ServiceId")));
         throw new Exception("Error in ServiceId");
     }
     else if (volumes.Month <= tariff.PeriodBegin || volumes.Month >= tariff.PeriodEnd)
     {
         OnException?.Invoke(this, new Tuple <string, Exception>(null, new Exception("Error in Mounth")));
         throw new Exception("Error in Mounth");
     }
     else if (tariff.Value <= 0)
     {
         OnException?.Invoke(this, new Tuple <string, Exception>(null, new Exception("Error in tariff")));
         throw new Exception("Error in tariff value");
     }
     else if (volumes.Value < 0)
     {
         OnException?.Invoke(this, new Tuple <string, Exception>(null, new Exception("Error in volume")));
         throw new Exception("Error in volume value");
     }
     else
     {
         OnNotify?.Invoke(this, $"Расчёт закончен {DateTime.Now}");
         return(new Charge()
         {
             ServiceId = volumes.ServiceId,
             HouseId = volumes.HouseId,
             Month = volumes.Month,
             Value = volumes.Value * tariff.Value
         });
     }
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            Tariff tariff = new Tariff()
            {
                ServiceId = 1, HouseId = 1, PeriodBegin = DateTime.UtcNow, PeriodEnd = DateTime.UtcNow, Value = -10
            };
            Volume volume = new Volume()
            {
                ServiceId = 1, HouseId = 1, Month = DateTime.UtcNow, Value = 1000
            };
            SubsidyCalculation subsidyCalculation = new SubsidyCalculation();

            try {
                Charge charge = subsidyCalculation.CalculateSubsidy(volume, tariff);
                Console.WriteLine("{0} {1} {2} {3}", charge.ServiceId, charge.HouseId, charge.Month, charge.Value);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error! Message: " + ex.Message);
            }
            Console.ReadLine();
        }
Beispiel #5
0
 public Charge CalculateSubsidy(Volume volumes, Tariff tariff)
 {
     try
     {
         Check(volumes, tariff);
         OnNotify.Invoke(this, $"Расчет начат в {DateTime.Now:T}");
         Charge charge = new Charge
         {
             HouseId   = volumes.HouseId,
             Month     = volumes.Month,
             ServiceId = volumes.ServiceId,
             Value     = volumes.Value * tariff.Value
         };
         OnNotify.Invoke(this, $"Расчет успешно завершен в {DateTime.Now:T}");
         return(charge);
     }
     catch (Exception e)
     {
         OnException.Invoke(this, new Tuple <string, Exception>("Ошибка", e));
         throw;
     }
 }
Beispiel #6
0
        public Charge CalculateSubsidy(Volume volumes, Tariff tariff)
        {
            Notify($"Расчёт начат в {DateTime.Now}");

            try
            {
                if (
                    Validate((volumes, tariff), x => x.Item1 != null, "Volume must not be null")
                    & Validate((volumes, tariff), x => x.Item2 != null, "Tariff must not be null") &&
                    Validate((volumes, tariff), x => x.Item2.Value > 0, "Tariff value must be positive")
                    & Validate((volumes, tariff), x => x.Item1.Value >= 0, "Volume value must be 0 or greater")
                    & Validate((volumes, tariff), x => x.Item1.HouseId == x.Item2.HouseId, "HouseId not equal")
                    & Validate((volumes, tariff), x => x.Item1.ServiceId == x.Item2.ServiceId, "ServiceId not equal")
                    & Validate((volumes, tariff), x => x.Item1.Month >= x.Item2.PeriodBegin & x.Item1.Month <= x.Item2.PeriodEnd, "Volume month not included in tariff"))
                {
                    Notify($"Расчёт успешно завершен в {DateTime.Now}");
                    return(new Charge(volumes.ServiceId, volumes.HouseId, DateTime.Now, volumes.Value * tariff.Value));
                }
                else
                {
                    return(null);
                }
            }
Beispiel #7
0
        static void Main(string[] args)
        {
            var subsidyCalculation = new SubsidyCalculation();

            subsidyCalculation.OnException += WriteMessageAboutException;
            subsidyCalculation.OnNotify    += WriteNotificationAboutCalculation;
            var volume1 = new Volume()
            {
                HouseId   = 1,
                ServiceId = 1,
                Month     = new DateTime(2021, 1, 1),
                Value     = 10
            };
            var tariff1 = new Tariff()
            {
                HouseId     = 1,
                ServiceId   = 1,
                PeriodBegin = new DateTime(2021, 1, 1),
                PeriodEnd   = new DateTime(2021, 3, 31),
                Value       = 2
            };

            subsidyCalculation.CalculateSubsidy(volume1, tariff1);
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            Volume[] volume = new Volume[5];
            ///Данные по использованному объему(подходящие)
            volume[0] = new Volume()
            {
                ServiceId = 1,
                HouseId   = 1,
                Month     = new DateTime(2021, 2, 27),
                Value     = 10
            };
            ///Данные по использованному объему с некорректным идентификатором услуги
            volume[1] = new Volume()
            {
                ServiceId = 2,
                HouseId   = 1,
                Month     = new DateTime(2021, 2, 27),
                Value     = 11
            };
            ///Данные по использованному объему с некорректным идентификатором дома
            volume[2] = new Volume()
            {
                ServiceId = 1,
                HouseId   = 1,
                Month     = new DateTime(2021, 2, 27),
                Value     = 12
            };
            ///Данные по использованному объему с некорректным месяцем
            volume[3] = new Volume()
            {
                ServiceId = 1,
                HouseId   = 1,
                Month     = new DateTime(2021, 3, 27),
                Value     = 13
            };
            ///Данные по использованному объему с некорректным значением
            volume[4] = new Volume()
            {
                ServiceId = 1,
                HouseId   = 1,
                Month     = new DateTime(2021, 2, 27),
                Value     = -14
            };

            Tariff[] tariff = new Tariff[2];
            ///Данные подходящего тарифа
            tariff[0] = new Tariff()
            {
                ServiceId   = 1,
                HouseId     = 1,
                PeriodBegin = new DateTime(2021, 2, 1),
                PeriodEnd   = new DateTime(2021, 2, 28),
                Value       = 100
            };
            ///Данные тарифа со значением 0
            tariff[1] = new Tariff()
            {
                ServiceId   = 1,
                HouseId     = 1,
                PeriodBegin = new DateTime(2021, 2, 1),
                PeriodEnd   = new DateTime(2021, 2, 28),
                Value       = 0
            };


            ///Расчет
            SubsidyCalculation Subsidy = new SubsidyCalculation();

            Subsidy.OnNotify    += dispOnNotify;
            Subsidy.OnException += dispOnException;

            Charge charge;

            charge = Subsidy.CalculateSubsidy(volume[0], tariff[0]);
            Console.WriteLine(charge.Value);
        }
 public Charge CalculateSubsidy(Volume volumes, Tariff tariff)
 {
 }