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();
        }
Esempio n. 2
0
        //method to Save/Update the current inventory
        private static void SaveCurrentInventory()
        {
            //create new streamwriter object
            StreamWriter sw = new StreamWriter(@"..\CarLotCOMPLETE\CarLot\CarLotDB.txt");

            //iterate through our list of cars and first make CSV string out of the objects data, and then write that data to the CSV text file
            foreach (Car car in currentInventory)
            {
                //check if the object is a NewCar or a UsedCar
                if (car is NewCar)
                {
                    //if its a NewCar then we call the method that is in the NewCar class
                    sw.WriteLine(NewCar.CarToCSV((NewCar)car));
                }
                else
                {
                    //if it is a UsedCar then we call then methiod that is in the UsedCar class
                    sw.WriteLine(UsedCar.CarToCSV((UsedCar)car));
                }
            }

            //closed the connection saving data to the text file
            sw.Close();
        }
Esempio n. 3
0
 /// <summary>
 /// return string in CSV format
 /// </summary>
 /// <param name="car"></param>
 public static string CarToCSV(UsedCar car)
 {
     //added a number 2 to the string to indicate a UsedCar object
     return($"{2},{car.Make},{car.Model},{car.Year},{car.Mileage}");
 }