/// <summary>
        /// Adds a car to the ArrayList. The method asks for user's input in defining the car, then calls the Car constructor.
        /// After the car is created, it's added to the ArrayList and also written into the .txt file.
        /// This method is used when inputing cars manually
        /// </summary>
        public void AddCar()
        {
            Console.WriteLine("Please define the specific parameters of your car:");

            int modelYear = Checker.CheckYear("What is the year of production?");

            int kms = Checker.CheckKms("How many kilometers has the car driven?");

            string brand = Checker.CheckString("What is the brand of this car? If there are 2 words, please join them: AlfaRomeo");

            string model = Checker.CheckString("What is the model of this car?");

            Car.FuelEnum fuel = Checker.CheckFuel($"What fuel does the car take? Select one of the following:{Car.getFuelTypes()}");

            decimal price = Checker.CheckPrice("What is the car's price?");

            string city = Checker.CheckString("Where is the car being sold?");

            int doors = Checker.CheckDoors("How many doors does the car have?");

            bool crashed = Checker.CheckCrashed("Has the car been crashed?");

            Car newCar = new Car(++Incrementor, modelYear, kms, brand, model, fuel, price, city, doors, crashed);

            carDatabase.Add(newCar);
            File.AppendAllText(FilePath, newCar.ToString() + "\n");
            Console.WriteLine("The following car has been successfully added to the database:");
            Console.WriteLine(newCar.ToString());
            Console.WriteLine("Press any key to continue to the main menu");
            Console.ReadKey();
            Console.Clear();
        }
Exemple #2
0
        public Car GetByID(int id)
        {
            try
            {
                using (SqlConnection connection = new SqlConnection(ConnString))
                {
                    connection.Open();

                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT * FROM [Car] LEFT JOIN [Fuel] ON Car.ID_Fuel = Fuel.ID_Fuel WHERE Car.ID_Car = @idInput";
                        command.Parameters.Add("@idInput", SqlDbType.Int).Value = id;
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Car.FuelEnum thisFuel  = (Car.FuelEnum)Enum.Parse(typeof(Car.FuelEnum), reader.GetString(11));
                                Car          loadedCar = new Car(reader.GetInt32(0), reader.GetInt32(2), reader.GetInt32(3), reader.GetString(4),
                                                                 reader.GetString(5), thisFuel, reader.GetDecimal(6), reader.GetString(7), reader.GetInt32(8), reader.GetBoolean(9));
                                return(loadedCar);
                            }
                        }
                    }
                }
                return(new Car());
            }

            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(new Car());
            }
        }
Exemple #3
0
        public List <Car> GetAll()
        {
            List <Car> carsFromDB = new List <Car>();

            try
            {
                using (SqlConnection connection = new SqlConnection(ConnString))
                {
                    connection.Open();

                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT * FROM [Car] LEFT JOIN [Fuel] ON Car.ID_Fuel = Fuel.ID_Fuel";

                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                Car.FuelEnum thisFuel  = (Car.FuelEnum)Enum.Parse(typeof(Car.FuelEnum), reader.GetString(11));
                                Car          loadedCar = new Car(reader.GetInt32(0), reader.GetInt32(2), reader.GetInt32(3), reader.GetString(4),
                                                                 reader.GetString(5), thisFuel, reader.GetDecimal(6), reader.GetString(7), reader.GetInt32(8), reader.GetBoolean(9));
                                carsFromDB.Add(loadedCar);
                            }
                        }
                    }
                }
                return(carsFromDB);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(carsFromDB);
            }
        }
        /// <summary>
        /// Loads cars into an ArrayList. The .txt file with cars is set at startup. If user doesn't specify, it's defaultCarLot.txt.
        /// </summary>
        public void LoadCars()
        {
            List <Car> carList = new List <Car>();

            try
            {
                string[] lines = File.ReadAllLines(FilePath);
                foreach (string line in lines)
                {
                    string[]     parts    = line.Split('\t');
                    Car.FuelEnum thisFuel = (Car.FuelEnum)Enum.Parse(typeof(Car.FuelEnum), parts[5]);
                    carList.Add(new Car(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), parts[3], parts[4], thisFuel,
                                        decimal.Parse(parts[6]), parts[7], int.Parse(parts[8]), bool.Parse(parts[9])));
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("The file you tried to load is missing.\n" +
                                  "A new empty database file will be created.\n" +
                                  "Press any key to continue");
                Console.ReadKey();
                if (!FilePath.Contains(".txt"))
                {
                    FilePath += ".txt";
                }
            }
            catch (Exception)
            {
                FilePath = CreateFilePath();
                Console.WriteLine("The file you tried to load is corrupted or the data in it doesn't fit the required format.\n" +
                                  $"A new empty database file was created named {FilePath}\n" +
                                  "Press any key to continue");
                Console.ReadKey();
                FilePath = CreateFilePath();
            }


            if (carList.Count != 0)
            {
                Console.WriteLine("The following cars have been successfully loaded from the database:");
                foreach (var car in carList)
                {
                    Console.WriteLine(car.ToString());
                }
            }
            else
            {
                Console.WriteLine("Your text file is empty.");
            }

            Console.WriteLine("Press any key to return to the main menu.");
            Console.ReadKey();
            Console.Clear();
            carDatabase = carList;
            Incrementor = getHighestID();
        }
 public Car(int id, int modelYear, int kMs, string brand, string model, Car.FuelEnum fuel, decimal price, string city, int doors, bool crashed)
 {
     ID        = id;
     ModelYear = modelYear;
     KMs       = kMs;
     Brand     = brand;
     Model     = model;
     Fuel      = fuel;
     Price     = price;
     City      = city;
     Doors     = doors;
     Crashed   = crashed;
 }
        public static void FilterCars(List <Car> carLotDatabase)
        {
            List <Car> CarsToFilter = new List <Car>(carLotDatabase);
            List <Car> FilteredCars = new List <Car>(CarsToFilter);

            bool selecting = true;
            bool byYear    = false;
            bool byKms     = false;
            bool byBrand   = false;
            bool byFuel    = false;
            bool byPrice   = false;
            bool byCity    = false;
            bool byDoors   = false;
            bool byCrashed = false;


            Console.Clear();
            Console.WriteLine("Welcome to the filtration tool");
            while (selecting)
            {
                Console.Clear();
                Console.WriteLine("Please select the filtration criteria. Press the number of filter you'd like to use and press Enter. When done, type in a 0");
                Console.WriteLine($"1 - filter by model year - {byYear}\n" +
                                  $"2 - filter by km's driven - {byKms}\n" +
                                  $"3 - filter by carmaker - {byBrand}\n" +
                                  $"4 - filter by fuel - {byFuel}\n" +
                                  $"5 - filter by price - {byPrice}\n" +
                                  $"6 - filter by city - {byCity}\n" +
                                  $"7 - filter by doors - {byDoors}\n" +
                                  $"8 - check if you'd like to include crashed vehicles - {byCrashed}\n");

                int selection = int.Parse(Console.ReadLine());

                switch (selection)
                {
                case (0): { selecting = false; break; }

                case (1): { byYear = !byYear; break; }

                case (2): { byKms = !byKms; break; }

                case (3): { byBrand = !byBrand; break; }

                case (4): { byFuel = !byFuel; break; }

                case (5): { byPrice = !byPrice; break; }

                case (6): { byCity = !byCity; break; }

                case (7): { byDoors = !byDoors; break; }

                case (8): { byCrashed = !byCrashed; break; }

                default: { Console.WriteLine("Wrong input"); break; }
                }
            }

            if (byYear)
            {
                int minYear = Checker.CheckYear("What is the lowest model year?");
                int maxYear = Checker.CheckYear("What is the highest model year?");
                foreach (Car car in CarsToFilter)
                {
                    if ((car.ModelYear <= minYear) || (car.ModelYear >= maxYear))
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byKms)
            {
                int minKms = Checker.CheckKms("What is the lowest kms?");
                int maxKms = Checker.CheckKms("What is the highest kms?");

                foreach (Car car in CarsToFilter)
                {
                    if ((car.KMs <= minKms) || (car.KMs >= maxKms))
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byBrand)
            {
                List <string> brands     = new List <string>();
                bool          moreBrands = true;
                do
                {
                    string userInput = Checker.CheckString("Type in a brand. Brands containing 2 words written as \"AlfaRomeo\"");
                    brands.Add(userInput);

                    Console.WriteLine("Would you like to add another brand to the filter? Type \"yes\" if so, any other input will mean this was the last brand");
                    string userAnswer = Console.ReadLine();
                    if (userAnswer.Equals("yes") || userAnswer.Equals("Yes") || userAnswer.Equals("YES"))
                    {
                        moreBrands = true;
                    }
                    else
                    {
                        moreBrands = false;
                    }
                } while (moreBrands);

                foreach (Car car in CarsToFilter)
                {
                    bool carToKeep = false;
                    foreach (string brand in brands)
                    {
                        if (car.Brand.Equals(brand))
                        {
                            carToKeep = true;
                        }
                    }
                    if (!carToKeep)
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byFuel)
            {
                List <Car.FuelEnum> fuels = new List <Car.FuelEnum>();
                bool moreFuels            = true;
                do
                {
                    Car.FuelEnum userInput = Checker.CheckFuel($"Type in a fuel type. Select one of the following:{Car.getFuelTypes()}");
                    fuels.Add(userInput);

                    Console.WriteLine("Would you like to add another fuel to the filter? Type \"yes\" if so, any other input will mean this was the last brand");
                    string userAnswer = Console.ReadLine();
                    if (userAnswer.Equals("yes") || userAnswer.Equals("Yes") || userAnswer.Equals("YES"))
                    {
                        moreFuels = true;
                    }
                    else
                    {
                        moreFuels = false;
                    }
                } while (moreFuels);

                foreach (Car car in CarsToFilter)
                {
                    bool carToKeep = false;
                    foreach (Car.FuelEnum fuel in fuels)
                    {
                        if (car.Fuel.Equals(fuel))
                        {
                            carToKeep = true;
                        }
                    }
                    if (!carToKeep)
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byPrice)
            {
                decimal minPrice = Checker.CheckPrice("What is the lowest price?");
                decimal maxPrice = Checker.CheckPrice("What is the highest price?");

                foreach (Car car in CarsToFilter)
                {
                    if ((car.Price <= minPrice) || (car.Price >= maxPrice))
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byCity)
            {
                List <string> cities     = new List <string>();
                bool          moreCities = true;
                do
                {
                    string userInput = Checker.CheckString("Type in a city.");
                    cities.Add(userInput);

                    Console.WriteLine("Would you like to add another city to the filter? Type \"yes\" if so, any other input will mean this was the last brand");
                    string userAnswer = Console.ReadLine();
                    if (userAnswer.Equals("yes") || userAnswer.Equals("Yes") || userAnswer.Equals("YES"))
                    {
                        moreCities = true;
                    }
                    else
                    {
                        moreCities = false;
                    }
                } while (moreCities);

                foreach (Car car in CarsToFilter)
                {
                    bool carToKeep = false;
                    foreach (string city in cities)
                    {
                        if (car.City.Equals(city))
                        {
                            carToKeep = true;
                        }
                    }
                    if (!carToKeep)
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (byDoors)
            {
                int doorValue = Checker.CheckKms("What is the required number of doors?");

                foreach (Car car in CarsToFilter)
                {
                    if (!(car.Doors == doorValue))
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (!byCrashed)
            {
                foreach (Car car in CarsToFilter)
                {
                    if ((car.Crashed == true))
                    {
                        if (FilteredCars.Contains(car))
                        {
                            FilteredCars.Remove(car);
                        }
                    }
                }
            }

            if (FilteredCars.Count > 0)
            {
                Console.WriteLine("The following cars fit your search criteria");
                foreach (Car car in FilteredCars)
                {
                    Console.WriteLine(car.ToString());
                }
            }
            else
            {
                Console.WriteLine("No cars fit your search criteria");
            }

            Console.WriteLine("Press any key to get back to the menu");
            Console.ReadLine();
            Console.Clear();
        }