Beispiel #1
0
        public static CarHandler.Car AddCar()
        {
            Console.WriteLine("Du ønsker at oprette en bil");
            Console.WriteLine("Indtast mærket:");
            string brand = Console.ReadLine();

            Console.WriteLine("indtast model:");
            string make = Console.ReadLine();

            Console.WriteLine("Indtast årgang:");
            int pYear = int.Parse(Console.ReadLine());

            Console.WriteLine("Indtast farve:");
            string color = Console.ReadLine();

            CarHandler.Car car          = new CarHandler.Car(brand, make, pYear, color);
            bool           notStarted   = true;
            int            startAttempt = 0;

            while (notStarted)
            {
                if (car.StartCar())
                {
                    startAttempt++;
                    notStarted = false;
                }
                else
                {
                    startAttempt++;
                }
            }
            Console.WriteLine($"{car.ToString()} brugte {startAttempt} forsøg for at starte.");
            return(car);
        }
Beispiel #2
0
        // Create car
        private static void CreateCar()
        {
            bool loopDone = false;

            while (!loopDone)
            {
                Console.WriteLine("Du ønsker at oprette en bil.");
                // Get brand of car
                Console.Write("Indtast mærke: ");
                string carBrand = Console.ReadLine();
                // Get model of car
                Console.Write("Indtast model: ");
                string carModel = Console.ReadLine();
                // Get year of car (MUST be after car was invented AND NOT after current year.
                Console.Write("Indtast årgang: ");
                string carYearString = Console.ReadLine();

                // Parse carYearString into an int, if failed, ask for input again.
                int carYearInt;
                while (!Int32.TryParse(carYearString, out carYearInt))
                {
                    Console.WriteLine("Fejl, kun tal kan modtages.");
                    Console.Write("Indtast årgang: ");
                    carYearString = Console.ReadLine();
                }
                Console.Write("Indtast farve: ");
                string carColor = Console.ReadLine();

                // Create instance of Car
                CarHandler.Car car = default;
                // Try catch for incorrect year
                try
                {
                    // Use input variables as properties of new car.
                    car = new CarHandler.Car(carBrand, carModel, carYearInt, carColor);
                }
                catch (ArgumentOutOfRangeException)
                {
                    // Error message for user and restart method
                    Console.WriteLine("FEJL: Året er før bilen er opfundet.\nEller året er i fremtiden.");
                    CreateCar();
                }
                // Test for engine ignition, return boolean.
                // If engine ignition works
                if (car.StartCar() == true)
                {
                    Console.WriteLine($"{car} brugte 1 forsøg på at starte.");
                }
                // Else if engine ignition fails.
                else if (car.StartCar() == false)
                {
                    Console.WriteLine($"{car} kunne ikke starte.");
                }

                // Add car to list
                listOfCars.Add(car);
                Console.WriteLine("Du har nu oprettet en bil, tast 1 for at afslutte eller 2 for at lave en ny.");
                // Let user choose next action for program
                string userChoice = Console.ReadLine();
                // Return to menu
                if (userChoice == "1")
                {
                    Console.Clear();
                    Main();
                }
                // Create a new car
                else if (userChoice == "2")
                {
                    Console.Clear();
                    CreateCar();
                }
            }
        }