public static bool ListofCars()
        {
            for (int i = 0; i < carType.Count; i++)
            {
                Console.WriteLine($"{i + 1 } \t {carType[i]} \t");
                //  Console.WriteLine(carType[i]);
            }
            Console.WriteLine($"{carType.Count + 1} \t Add a car");
            Console.WriteLine("");
            Console.WriteLine($"{carType.Count + 2} \t quit");
            Console.WriteLine("");
            Console.WriteLine("Which car would you like to buy ? ");
            int choice = Int32.Parse(Console.ReadLine());

            if (choice > 0 && choice < carType.Count + 1)
            {
                Console.WriteLine($" You've selected: {carType[choice - 1]}");

                Console.WriteLine("Would you like to buy this car?");
                string userResponse = Console.ReadLine().ToLower();
                if (userResponse == "y" || userResponse == "yes")
                {
                    Console.WriteLine("Excellent! Our finance department will be in touch shortly.\n");
                    CarLot.RemoveCar(choice, carType);
                    Console.WriteLine("");
                }
                else if (userResponse == "n" || userResponse == "no")
                {
                    Console.WriteLine("Please choose a different option \n");
                    return(true);
                }


                return(true);
            }
            else if (choice == carType.Count + 1)
            {
                Console.WriteLine($"Add a Car");
                CarLot.UserAddedCar();
                return(true);
            }
            else if (choice == carType.Count + 2)

            {
                Console.WriteLine("Have a nice day!");
                return(false);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Kyle Nedbal's Used Car Emporium!");
            Console.WriteLine();

            CarLot.cars.Add(new NewCar("Tesla", "Roadster", 2021, 130000.00));
            CarLot.cars.Add(new NewCar("Ford", "Mustang Mach-EV", 2021, 42500.00));
            CarLot.cars.Add(new NewCar("GMC", "Hummer EV", 2021, 105000.00));
            CarLot.cars.Add(new UsedCar("Lincoln", "Continental", 2018, 32000.00, 25000.00));
            CarLot.cars.Add(new UsedCar("Chevrolet", "Blazer RS", 2019, 29000.00, 36000.00));
            CarLot.cars.Add(new UsedCar("Jeep", "Grand Cherokee", 2017, 28000.00, 40000.00));

            bool userWantsToContinue = true;
            bool userChoice;

            do
            {
                do
                {
                    CarLot.ListCars(CarLot.cars);
                    Console.Write("Which car would you like?: ");
                    int validUserChoice;
                    userChoice = Int32.TryParse(Console.ReadLine(), out validUserChoice);
                    Console.WriteLine();

                    if (userChoice && validUserChoice > 0 && validUserChoice <= CarLot.cars.Count + 2)
                    {
                        if (validUserChoice > 0 && validUserChoice <= CarLot.cars.Count)
                        {
                            CarLot.cars[validUserChoice - 1].ToString();

                            Console.Write("Would you like to buy this car? (y/n): ");
                            string buyYN = Console.ReadLine().ToLower();
                            while (buyYN != "n" && buyYN != "y")
                            {
                                Console.Write("Please enter y or n to choose whether you'd like to buy this car: ");
                                buyYN = Console.ReadLine().ToLower();
                            }

                            if (buyYN == "y")
                            {
                                CarLot.RemoveCar(CarLot.cars[validUserChoice - 1]);
                                Console.WriteLine("Excellent! Our finance department will be in touch shortly");
                                Console.WriteLine();
                            }
                        }

                        else if (validUserChoice == CarLot.cars.Count + 1)
                        {
                            Console.Write("Enter the make: ");
                            string userMake = Console.ReadLine();
                            Console.Write("Enter the model: ");
                            string userModel = Console.ReadLine();
                            Console.Write("Enter the year: ");
                            int userYear = Int32.Parse(Console.ReadLine());
                            Console.Write("Enter the value of the car: ");
                            double userPrice = Double.Parse(Console.ReadLine());
                            Console.Write("Enter the mileage: ");
                            double userMileage = Double.Parse(Console.ReadLine());

                            CarLot.AddCar(new UsedCar(userMake, userModel, userYear, userPrice, userMileage));
                        }
                        else
                        {
                            userWantsToContinue = false;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Invalid choice. Please enter the corresponding number to your choice showing on the list");
                    }
                } while (userChoice == false);
            } while (userWantsToContinue == true);

            Console.WriteLine("Have a great day!");
        }