Esempio n. 1
0
        //Get method that will pull in the current inventory from the CSV file
        private static void GetCurrentInventory()
        {
            //first point the StreamReader object at the text file that holds the current inventory in CSV format
            StreamReader sr = new StreamReader(@"C:\Users\ShaKy\Source\Repos\CarLotCOMPLETE\CarLot\CarLotDB.txt");

            //string array to hold the split CSV row before parsing into necessary Car object
            string[] csvArray;

            //string that grabs and holds the first line of the CSV text file
            string line = sr.ReadLine();

            //while loop to iterate through the text file of CSV's building inventory List
            while (line != null)//as long as the first line of the text file is not null then continue with parsing
            {
                //spilt the CSV on the comma's until we have the sparate values indexed in our string array
                csvArray = line.Split(',');

                //check to see what type of Car object is in each CSV
                if (csvArray[0] == "1")//if it starts with a number 1 then it will be a NewCar object
                {
                    //we add a NewCar object passing a CSV string in to our method which returns a new object
                    currentInventory.Add(NewCar.CSVToCar(line));
                }
                else
                {
                    //we add a UsedCar object passing a CSV string in to our method which returns a new object
                    currentInventory.Add(UsedCar.CSVToCar(line));
                }

                //we advance the CSV text file to the next row of data
                line = sr.ReadLine();
            }

            //close the text file when done with File I/O operations
            sr.Close();
        }