コード例 #1
0
        private void chargeBatteryFromMainMenu()
        {
            bool isInputOk = true;

            do
            {
                try
                {
                    isInputOk = true;
                    string licenseNumber   = getLicenseNumberFromUser();
                    float  minutesToCharge = getHowMuchMinutesToCharge(licenseNumber);
                    m_Garage.ChargeBattery(minutesToCharge, licenseNumber);
                    Console.WriteLine("Your vehicle was chargesd successfully");
                }

                catch (ArgumentException exception)
                {
                    Console.WriteLine(exception.Message);
                }

                catch (ValueOutOfRangeException outOfRangeException)
                {
                    Console.WriteLine(outOfRangeException.Message);
                    Console.WriteLine("Note: The max is {0}.", outOfRangeException.MaxValue);
                    isInputOk = false;
                }

                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    isInputOk = false;
                }
            }while (!isInputOk);
        }
コード例 #2
0
        private static void chargeScreen()
        {
            Console.Clear();
            Console.WriteLine("Please Enter Vehicle License Number that you want to Charge: ");
            string license = verifyStringContainsOnlyNumbers(k_LengthOfLicenseNumber);

            if (checkIfLicenseDoesntExist(license))
            {
                Console.WriteLine(
                    string.Format(
                        @"Vehicle you wanted to Charge doesnt exist is the system. 
Press Enter to go back to Main Menu..."));
                Console.ReadLine();
            }
            else
            {
                bool isValidMinutes = false;
                while (!isValidMinutes)
                {
                    Console.WriteLine("Please Enter number of Minutes you want to Charge:");
                    try
                    {
                        float minutesToAdd = verifyInputIsANumber();
                        s_GarageManager.ChargeBattery(license, minutesToAdd);
                        isValidMinutes = true;
                        Console.Clear();
                        Console.WriteLine(
                            string.Format(
                                @"Vehicle was Charged Successfully!
Press Enter to return to the main menu"));
                        Console.ReadLine();
                    }
                    catch (ValueOutOfRangeException vore)
                    {
                        if (vore.Message.Equals("Battery Fully Charged"))
                        {
                            isValidMinutes = true;
                            Console.WriteLine(
                                "Battery Already Fully Charged! Press Enter to return to the main menu...");
                            Console.ReadLine();
                            break;
                        }
                        else
                        {
                            Console.WriteLine("You exceeded the amount of time you can charge the Battery. Try again:");
                            continue;
                        }
                    }

                    catch (InvalidCastException ice)
                    {
                        Console.WriteLine(
                            string.Format(
                                @"You tried to Charge Fuel-Based Vehicle!
Press Enter to return to main menu... "));
                        Console.ReadLine();
                        break;
                    }

                    catch (FormatException fm)
                    {
                        Console.WriteLine("Not a valid Number! Try Again:");
                        continue;
                    }

                    catch (ArgumentException ae)
                    {
                        Console.WriteLine("Wrong Type of Energy Type");
                        continue;
                    }
                }
            }
        }