Пример #1
0
        public static void AddEnergy(Garage i_Garage, Customer i_Customer)
        {
            float howMuchToAdd           = 0;
            bool  shouldConvertToMinutes = false;

            if (i_Customer.Vehicle.Energy is FuelEnergy)
            {
                Printer.PrintMessage(Messages.GetMessage(14));
            }
            else
            {
                shouldConvertToMinutes = true;
                Printer.PrintMessage(Messages.GetMessage(15));
            }

            bool flag = false;

            while (!flag)
            {
                try
                {
                    flag = float.TryParse(Console.ReadLine(), out howMuchToAdd);
                    if (!flag || howMuchToAdd < 0)
                    {
                        flag = false;
                        throw new FormatException();
                    }
                    else
                    {
                        try
                        {
                            if (shouldConvertToMinutes)
                            {
                                i_Garage.AddEnergy(i_Customer, howMuchToAdd / 60f);
                            }
                            else
                            {
                                i_Garage.AddEnergy(i_Customer, howMuchToAdd);
                            }
                        }
                        catch (ValueOutOfRangeException e)
                        {
                            if (i_Customer.Vehicle.Energy is FuelEnergy)
                            {
                                Printer.PrintFormatError("{0}, Gas Tank can hold {1} liters more\n", Messages.GetMessage(13), (i_Customer.Vehicle.Energy.MaxAmountOfEnergy - i_Customer.Vehicle.Energy.AmountOfLeftEnergy()));
                            }
                            else
                            {
                                Printer.PrintFormatError("{0}, Battery maximum limit is  {1} hours\n", Messages.GetMessage(13), (i_Customer.Vehicle.Energy.MaxAmountOfEnergy - i_Customer.Vehicle.Energy.AmountOfLeftEnergy()));
                            }

                            flag = false;
                        }
                    }
                }
                catch (FormatException exp)
                {
                    Printer.PrintError(Messages.GetMessage(11));
                }
            }

            int id = 0;

            id = i_Customer.Vehicle.Energy is FuelEnergy ? 17 : 16;
            Printer.PrintSuccess(Messages.GetMessage(id));
            Printer.PrintMessage(Messages.GetMessage(6));
            Console.ReadLine();
        }