// this method loads the list of rentals from the file. private void LoadRentalsFromFile() { // Load all and store the rental csv lines into string array RentalsCsvLines = File.ReadAllLines(RentalsFile); // Create datatable for rentals DataTable rentals = new DataTable(); // load list of rentals and store into datable CsvOperations.CsvToTable(RentalsFile, rentals); // Store datatable fields in string arrays: registration and CustomerID string[] Registration = rentals.AsEnumerable().Select(row => row.Field <string>("Registration")).ToArray(); string[] customerID = rentals.AsEnumerable().Select(row => row.Field <string>("CustomerID")).ToArray(); // Convert CustomerID to int array int[] CustomerID = Array.ConvertAll(customerID, int.Parse); for (int i = 0; i < customerID.Length; i++) { RentalListRego.Add(Registration[i]); RentalListID.Add(CustomerID[i]); } foreach (string rego in Registration) { DailyCost.Add(GetVehicle(rego).dailyRate); } }
// This method removes the customer with the provided customer ID from the CRM if they // are not currently renting a vehicle. It returns true if the removal was successful, // otherwise it returns false. public bool RemoveCustomer(int ID, Fleet fleet) { if (!(fleet.IsRenting(ID))) { CsvOperations.DeleteRecord("customers.csv", ID.ToString(), CrmFile, 0); return(true); } return(false); }
//This method removes the vehicle with the provided rego from the fleet if it is not //currently rented.it returns true if the removal was successful and false otherwise. public bool RemoveVehicle(string registration) { // if registration of the vehicle is not rented, delete the vehicle record relative to the fleet file if (!IsRented(registration)) { CsvOperations.DeleteRecord("fleet.csv", registration, FleetFile, 0); VehicleList.Remove(GetVehicle(registration)); return(true); } return(false); }
// this method loads the list of vehicles from the file. private void LoadVehiclesFromFile() { // Create datatable for fleet DataTable fleet = new DataTable(); // load list of vehicles and store into datatable CsvOperations.CsvToTable(FleetFile, fleet); // Create string arrays for each attribute of a vehicle and store the data from the datable into the string arrays. // (Note that string array variables starting with lower case letters need to be converted to their appropriate // Data type. When converted, they will encompass uppercase lettering. string[] Registration = fleet.AsEnumerable().Select(row => row.Field <string>("Registration")).ToArray(); string[] grade = fleet.AsEnumerable().Select(row => row.Field <string>("Grade")).ToArray(); string[] Make = fleet.AsEnumerable().Select(row => row.Field <string>("Make")).ToArray(); string[] Model = fleet.AsEnumerable().Select(row => row.Field <string>("Model")).ToArray(); string[] year = fleet.AsEnumerable().Select(row => row.Field <string>("Year")).ToArray(); string[] numSeats = fleet.AsEnumerable().Select(row => row.Field <string>("NumSeats")).ToArray(); string[] transmission = fleet.AsEnumerable().Select(row => row.Field <string>("Transmission")).ToArray(); string[] fuel = fleet.AsEnumerable().Select(row => row.Field <string>("Fuel")).ToArray(); string[] gps = fleet.AsEnumerable().Select(row => row.Field <string>("GPS")).ToArray(); string[] sunRoof = fleet.AsEnumerable().Select(row => row.Field <string>("SunRoof")).ToArray(); string[] dailyRate = fleet.AsEnumerable().Select(row => row.Field <string>("DailyRate")).ToArray(); string[] Colour = fleet.AsEnumerable().Select(row => row.Field <string>("Colour")).ToArray(); //Convert string arrays of grade, fuel and transmission into enum arrays Vehicle.VehicleGrade[] Grade = grade.Select(s => Enum.Parse(typeof(Vehicle.VehicleGrade), s)).Cast <Vehicle.VehicleGrade>().ToArray(); Vehicle.TransmissionType[] Transmission = transmission.Select(s => Enum.Parse(typeof(Vehicle.TransmissionType), s)).Cast <Vehicle.TransmissionType>().ToArray(); Vehicle.FuelType[] Fuel = fuel.Select(s => Enum.Parse(typeof(Vehicle.FuelType), s)).Cast <Vehicle.FuelType>().ToArray(); // Convert string arrays of year and numSeats & dailyRate into type int and double, respectively. int[] Year = Array.ConvertAll(year, int.Parse); int[] NumSeats = Array.ConvertAll(numSeats, int.Parse); double[] DailyRate = Array.ConvertAll(dailyRate, double.Parse); // Convert string arrays of gps and sunRoof to boolean datatype bool[] GPS = Array.ConvertAll(gps, bool.Parse); bool[] SunRoof = Array.ConvertAll(sunRoof, bool.Parse); // The number of vehicles in the fleet csv file is equal to the number of lines contained within the file. // Since registration is a mandatory field, it will not have missing entries, so we count the size of the registration array // to determine the number of vehicles in the csv file. int numVehicles = Registration.Length; // Using a loop, create a list of vehicles by instatiating the vehicle class. // Import values from the arrays above (taken from the csv file) into the constructor of vehicle class. for (int i = 0; i < numVehicles; i++) { VehicleList.Add(new Vehicle(Registration[i], Grade[i], Make[i], Model[i], Year[i], NumSeats[i], Transmission[i], Fuel[i], GPS[i], SunRoof[i], DailyRate[i], Colour[i])); } }
// This method returns the customer ID of the current renter of the vehicle. If it is // rented by no one, it returns -1. Technically this method can replace IsRented. public int RentedBy(string registration) { // Use for loop to iterate through every line of the rentals csv file for (int i = 0; i < RentalsCsvLines.Length; i++) { // Split fields separated by a comma into indidual elements and store into array string[] fields = RentalsCsvLines[i].Split(','); // If the registration exists in the rentals, return customerID. // Note that, registration and customer ID is located in position 0 and 1 of the rentals file, respectively. int regoCsvPos = 0, customerIDPos = 1; if (CsvOperations.RecordExists(registration, fields, regoCsvPos)) { return(int.Parse(CsvOperations.ReadRecordAttribute(registration, RentalsFile, 0, customerIDPos))); } } return(-1); }
// this method loads the list of rentals from the file. private void LoadRentalsFromFile() { // Create datatable for rentals DataTable rentals = new DataTable(); // load list of rentals and store into datable CsvOperations.CsvToTable(RentalsFile, rentals); // Store datatable fields in string arrays: registration and CustomerID string[] Registration = rentals.AsEnumerable().Select(row => row.Field <string>("Registration")).ToArray(); string[] customerID = rentals.AsEnumerable().Select(row => row.Field <string>("CustomerID")).ToArray(); // Convert CustomerID to int array int[] CustomerID = Array.ConvertAll(customerID, int.Parse); for (int i = 0; i < customerID.Length; i++) { Rentals.Add(Registration[i], CustomerID[i]); } }
// This method saves the current list of rentals to file. public void SaveRentalsToFile() { // Creat new file string newFile = "rentals.csv"; //Write the header fields at the top of the new file string csvFieldHeader = "Registration," + "CustomerID\n"; File.WriteAllText(newFile, csvFieldHeader); // Loop through the rentals dictionary and write the registration and customer ID to the file output foreach (KeyValuePair <string, int> kvp in Rentals) { CsvOperations.WriteToCsv(newFile, kvp.Key + "," + kvp.Value.ToString()); } // delete old file File.Delete(RentalsFile); // move new file to old file directory File.Move(newFile, RentalsFile); }
// This method saves the current state of the CRM to file. public void SaveToFile() { // create newfile string newFile = "customers.csv"; // Write field headers at the top of the new file string csvFieldHeader = "ID," + "Title," + "FirstName," + "LastName," + "Gender," + "DOB\n"; File.WriteAllText(newFile, csvFieldHeader); // For each of the vehicles added into the existing vehicles list, write to the new csv file. foreach (Customer customer in CustomerList) { CsvOperations.WriteToCsv(newFile, customer.ToCSVstring()); } // delete old file File.Delete(CrmFile); // move new file to old file directory File.Move(newFile, CrmFile); }
// This method saves the current list of vehicles to file. public void SaveVehiclesToFile() { //Create new file string newFile = "fleet.csv"; //Write the header fields at the top of the new file string csvFieldHeader = "Registration," + "Grade," + "Make," + "Model," + "Year," + "NumSeats," + "Transmission," + "Fuel," + "GPS," + "SunRoof," + "DailyRate," + "Colour\n"; File.WriteAllText(newFile, csvFieldHeader); // For each of the vehicles added into the unsaved vehicles list, write to the fleet csv file. foreach (Vehicle vehicle in VehicleList) { CsvOperations.WriteToCsv(newFile, vehicle.ToCSVstring()); } // delete old file File.Delete(FleetFile); // move new file to old file directory File.Move(newFile, FleetFile); }
// This method saves the current list of rentals to file. public void SaveRentalsToFile() { // Creat new file string newFile = "rentals.csv"; //Write the header fields at the top of the new file string csvFieldHeader = "Registration," + "CustomerID\n"; File.WriteAllText(newFile, csvFieldHeader); // The rego and id lists are zipped into a parallel list, where it will be used in a foreach loop // to write into csv file var parallelList = RentalListRego.Zip(RentalListID, (rego, id) => new { Rego = rego, ID = id }); foreach (var element in parallelList) { CsvOperations.WriteToCsv(newFile, element.Rego + "," + element.ID.ToString()); } // delete old file File.Delete(RentalsFile); // move new file to old file directory File.Move(newFile, RentalsFile); }
// This method loads the state of the CRM from file. public void LoadFromFile() { // Store all the lines of the customers csv in a string array CustomersCsvLines = File.ReadAllLines(CrmFile); // create customers datatable DataTable customers = new DataTable(); // load list of rentals and store into datable CsvOperations.CsvToTable(CrmFile, customers); // Create string arrays for each attribute of a customer and store the data from the datable into the string arrays. // (Note that string array variables starting with lower case letters need to be converted to their appropriate // Data type. When converted, they will encompass uppercase lettering. string[] id = customers.AsEnumerable().Select(row => row.Field <string>("ID")).ToArray(); string[] Title = customers.AsEnumerable().Select(row => row.Field <string>("Title")).ToArray(); string[] FirstName = customers.AsEnumerable().Select(row => row.Field <string>("FirstName")).ToArray(); string[] LastName = customers.AsEnumerable().Select(row => row.Field <string>("LastName")).ToArray(); string[] gender = customers.AsEnumerable().Select(row => row.Field <string>("Gender")).ToArray(); string[] dob = customers.AsEnumerable().Select(row => row.Field <string>("DOB")).ToArray(); //Convert id string arr to int Array int[] CustomerID = Array.ConvertAll(id, int.Parse); // Convert gender string arr to Enum array Customer.Gender[] Gender = gender.Select(s => Enum.Parse(typeof(Customer.Gender), s)).Cast <Customer.Gender>().ToArray(); // Convert dob string arr to DateTime array DateTime[] DOB = Array.ConvertAll(dob, DateTime.Parse); // The number of customers in the customers csv file is equal to the number of lines contained within the file. // Since ID is a primary field, it will not have missing entries, so we count the size of the ID array // to determine the number of customers in the csv file. int numCustomers = CustomerID.Length; // Using a loop, create a list of customer by instatiating the customer class and add to list. // Import values from the arrays above (taken from the csv file) into the constructor of customer class. for (int i = 0; i < numCustomers; i++) { CustomerList.Add(new Customer(CustomerID[i], Title[i], FirstName[i], LastName[i], Gender[i], DOB[i])); } }