public void LoadFromFile() { try { string[] lines = System.IO.File.ReadAllLines(fleetFile); for (int lineNum = 0; lineNum < lines.Length; lineNum++) { string[] vehColumns = lines[lineNum].Split(','); string rego = vehColumns[0]; string make = vehColumns[1]; string model = vehColumns[2]; int year = int.Parse(vehColumns[3]); Vehicle.VehicleClass vehicleClass = (Vehicle.VehicleClass)Enum.Parse(typeof(Vehicle.VehicleClass), vehColumns[4]); int numseat = int.Parse(vehColumns[5]); Vehicle.TransmissionType transmissionType = (Vehicle.TransmissionType)Enum.Parse(typeof(Vehicle.TransmissionType), vehColumns[6]); Vehicle.FuelType fuelType = (Vehicle.FuelType)Enum.Parse(typeof(Vehicle.FuelType), vehColumns[7]); bool gps = bool.Parse(vehColumns[8]); bool sunroof = bool.Parse(vehColumns[9]); string colour = vehColumns[10]; double dailyRate = double.Parse(vehColumns[11]); _vehicleFleet.Add(new Vehicle(rego, vehicleClass, make, model, year, numseat, transmissionType, fuelType, gps, sunroof, dailyRate, colour)); } } catch (System.IO.DirectoryNotFoundException e) { Console.WriteLine("Could not load file" + fleetFile); } }
} // end SaveToFile() method /// <summary> /// /// Loading all data from the .csv files to local Variables. /// This data can then be used to populate the data grid views of the Windows Form application /// /// This method creates a temporary array that reads and writess each column of the .csv file as an element and /// Then instantiates a class with all of the temp. variables as input params. /// /// Each time adding the new instace to either the Fleet or the CRM /// /// </summary> public void LoadFromFile() { // Create empty local arrays to append with .csv data fleet = new List <Vehicle>(); rentedRego = new List <string>(); rentedCustomer = new List <int>(); if (File.Exists(fleetFile)) { // Use StreamReader to read each line of the csv file using (StreamReader sr = new StreamReader(fleetFile)) { sr.ReadLine(); // Accounting for header row of each .csv file while (sr.Peek() != -1) { string line = sr.ReadLine(); List <string> lineValues = line.Split(',').ToList(); string vehicleRego = lineValues[0]; string make = lineValues[1]; string model = lineValues[2]; int year = int.Parse(lineValues[3]); Vehicle.VehicleClass vehicleClass = (Vehicle.VehicleClass)Enum.Parse(typeof(Vehicle.VehicleClass), lineValues[4]); int numSeats = int.Parse(lineValues[5]); Vehicle.TransmissionType transmissionType = (Vehicle.TransmissionType)Enum.Parse(typeof(Vehicle.TransmissionType), lineValues[6]); Vehicle.FuelType fuelType = (Vehicle.FuelType)Enum.Parse(typeof(Vehicle.FuelType), lineValues[7]); bool GPS = bool.Parse(lineValues[8]); bool sunRoof = bool.Parse(lineValues[9]); string colour = lineValues[10]; double dailyRate = double.Parse(lineValues[11]); // Create instance of Class Vehicle Vehicle current_vehicle = new Vehicle(vehicleRego, make, model, year, vehicleClass, numSeats, transmissionType, fuelType, GPS, sunRoof, colour, dailyRate); // Append Vehicle to fleet fleet.Add(current_vehicle); } } // end using } if (File.Exists(rentalsFile)) { using (StreamReader sr = new StreamReader(rentalsFile)) { sr.ReadLine(); // Accounting for header row while (sr.Peek() != -1) { string line = sr.ReadLine(); List <string> lineValues = line.Split(',').ToList(); string vehicleRego = lineValues[0]; int customerID = int.Parse(lineValues[1]); // Append to rented array rentedRego.Add(vehicleRego); // Append to customer array rentedCustomer.Add(customerID); } } // end using } } // end LoadFromFile() method
// Loads the data from csv files at given locations. Separates the data by ",". // Reads in each line as a vehicle and assigns each attribute. public void LoadFromFile() { string csvVehicle; StreamReader inFile; inFile = new StreamReader(fleetFile); while ((csvVehicle = inFile.ReadLine()) != null) { string[] listVehicle = csvVehicle.Split(','); if (listVehicle[0] != "Rego") // Skips the Headers { vehicleRego = listVehicle[0]; make = listVehicle[1]; model = listVehicle[2]; year = int.Parse(listVehicle[3]); Vehicle.VehicleClass vehicleClass = (Vehicle.VehicleClass)Enum.Parse(typeof(Vehicle.VehicleClass), listVehicle[4]); numSeats = int.Parse(listVehicle[5]); Vehicle.TransmissionType transmission = (Vehicle.TransmissionType)Enum.Parse(typeof(Vehicle.TransmissionType), listVehicle[6]); Vehicle.FuelType fuel = (Vehicle.FuelType)Enum.Parse(typeof(Vehicle.FuelType), listVehicle[7]); if (listVehicle[8] == "False") { GPS = false; } else { GPS = true; } if (listVehicle[9] == "False") { sunRoof = false; } else { sunRoof = true; } colour = listVehicle[10]; dailyRate = double.Parse(listVehicle[11]); Vehicle newVehicle = new Vehicle(vehicleRego, vehicleClass, make, model, year, numSeats, transmission, fuel, GPS, sunRoof, dailyRate, colour); vehicles.Add(newVehicle); } } inFile.Close(); // loads the rental data into the dictionary string csvRental; StreamReader inFileRental; inFileRental = new StreamReader(rentalFile); while ((csvRental = inFileRental.ReadLine()) != null) { string[] listRental = csvRental.Split(','); if (listRental[0] != "Vehicle") // Skips the Headers { rego = listRental[0]; customerID = int.Parse(listRental[1]); // makes sure no vehicle is added twice try { rentals.Add(rego, customerID); } catch (ArgumentException) { Console.WriteLine("Vehicle Already exists"); } } } Console.WriteLine(rentals); inFileRental.Close(); }