public static void writeFile(List <Car> cars, List <Van> vans) { String[] contents = new String[cars.Count + vans.Count]; int counter = 0; for (int x = 0; x < cars.Count; x++) { Car c = cars[x]; contents[counter] = c.formatLine() + c.Available.ToString() + "|" + c.AvailableOn.ToString() + "|" + c.RegNumber; for (int i = 0; i < cars[x].hiredDates.Count; i++) { contents[counter] += "|" + cars[x].hiredDates[i].StartDate.ToString() + "|" + cars[x].hiredDates[i].ReturnDate.ToString(); } counter++; } for (int y = 0; y < vans.Count; y++) { Van v = vans[y]; contents[counter] = v.formatLine() + "|" + v.Available.ToString() + "|" + v.AvailableOn.ToString() + "|" + v.RegNumber; for (int i = 0; i < vans[y].hiredDates.Count; i++) { contents[counter] += "|" + vans[y].hiredDates[i].StartDate.ToString() + "|" + vans[y].hiredDates[i].ReturnDate.ToString(); } counter++; } System.IO.File.WriteAllLines(filepath, contents); }
private static void sortByPrice(bool lowToHigh) { for (int x = 0; x < cars.Count; x++) { for (int i = 0; i < cars.Count - 1; i++) { if (lowToHigh) { if (cars[i].Price > cars[i + 1].Price) { Car temp = cars[i]; cars[i] = cars[i + 1]; cars[i + 1] = temp; } } else { if (cars[i].Price < cars[i + 1].Price) { Car temp = cars[i]; cars[i] = cars[i + 1]; cars[i + 1] = temp; } } } } for (int x = 0; x < vans.Count; x++) { for (int i = 0; i < vans.Count - 1; i++) { if (lowToHigh) { if (vans[i].Price > vans[i + 1].Price) { Van temp = vans[i]; vans[i] = vans[i + 1]; vans[i + 1] = temp; } } else { if (vans[i].Price < vans[i + 1].Price) { Van temp = vans[i]; vans[i] = vans[i + 1]; vans[i + 1] = temp; } } } } }
static void applyFilters(Van inputVehicle) { bool pass = true; switch (transmissionType.ToLower()) { case "any": break; case "manual": if (!inputVehicle.Manual) { pass = false; } break; case "automatic": if (inputVehicle.Manual) { pass = false; } break; } switch (fuelType.ToLower()) { case "any": break; case "petrol": if (inputVehicle.FuelType.ToLower() != "petrol") { pass = false; } break; case "diesel": if (inputVehicle.FuelType.ToLower() != "diesel") { pass = false; } break; } switch (wheelbase.ToLower()) { case "any": break; case "long": if (inputVehicle.Wheelbase != 'l') { pass = false; } break; case "short": if (inputVehicle.Wheelbase != 's') { pass = false; } break; } if (pass) { availableVans.Add(inputVehicle); } }
public static void readFile(out List <Car> cars_, out List <Van> vans_) { vans.Clear(); cars.Clear(); String[] fileContent = System.IO.File.ReadAllLines(filepath); for (int i = 0; i < fileContent.Length; i++) { String[] param = fileContent[i].Split('|'); switch (param[0].ToLower()) { case "car": Car c = new Car(); c.Make = param[1]; c.Model = param[2]; c.EngineSize = int.Parse(param[3]); c.FuelType = param[4]; c.Manual = bool.Parse(param[5]); c.Price = double.Parse(param[6]); c.Doors = int.Parse(param[7]); c.Seats = int.Parse(param[8]); c.Body = param[9]; //c.Available = bool.Parse(param[10]); //try //{ // c.AvailableOn = DateTime.Parse(param[11]); //} //catch //{ // c.AvailableOn = new DateTime(); //} c.RegNumber = param[12]; try { DateTime start = new DateTime(), end; if (param.Length > 13) { for (int index = 13; index < param.Length; index++) { if (index % 2 == 1) { start = DateTime.Parse(param[index]); } else { end = DateTime.Parse(param[index]); Hired temp = new Hired(start, end); c.hiredDates.Add(temp); } } } } catch { } cars.Add(c); break; case "van": Van v = new Van(); v.Make = param[1]; v.Model = param[2]; v.EngineSize = int.Parse(param[3]); v.FuelType = param[4]; v.Manual = bool.Parse(param[5]); v.Price = double.Parse(param[6]); v.CargoSpace = double.Parse(param[7]); v.SideDoor = bool.Parse(param[8]); v.Wheelbase = param[9][0]; //v.Available = bool.Parse(param[10]); //try //{ // v.AvailableOn = DateTime.Parse(param[11]); //} //catch //{ // v.AvailableOn = new DateTime(); //} v.RegNumber = param[12]; try { DateTime start = new DateTime(), end; if (param.Length > 13) { for (int index = 13; index < param.Length; index++) { if (index % 2 == 1) { start = DateTime.Parse(param[index]); } else { end = DateTime.Parse(param[index]); Hired temp = new Hired(start, end); v.hiredDates.Add(temp); } } } } catch { } vans.Add(v); break; } } for (int i = 0; i < cars.Count; i++) { Console.WriteLine(cars[i].RegNumber); } //DatabaseOperations.writeDatabase(cars, vans); cars_ = cars; vans_ = vans; }
public static void readDatabase(out List <Car> cars, out List <Van> vans) { List <Car> car_ = new List <Car>(); List <Van> van_ = new List <Van>(); try { connect.Open(); SqlCommand read = new SqlCommand("SELECT * FROM dbo.Vehicles;", connect); SqlDataReader reader = read.ExecuteReader(); while (reader.Read()) { if ((bool)reader["Car"]) { Car c = new Car(); c.Make = reader["Make"].ToString(); c.Model = reader["Model"].ToString(); c.EngineSize = (int)reader["Engine"]; c.FuelType = reader["Fuel"].ToString(); c.Manual = (bool)reader["Manual"]; c.Price = double.Parse(reader["Price"].ToString()); c.Doors = (int)reader["Doors"]; c.Seats = (int)reader["Seats"]; c.Body = reader["Body"].ToString(); //c.Available = (bool)reader["Available"]; //c.AvailableOn = (DateTime)reader["AvailableOn"]; c.RegNumber = reader["RegNumber"].ToString(); car_.Add(c); } else { Van v = new Van(); v.Make = reader["Make"].ToString(); v.Model = reader["Model"].ToString(); v.EngineSize = (int)reader["Engine"]; v.FuelType = reader["Fuel"].ToString(); v.Manual = (bool)reader["Manual"]; v.Price = double.Parse(reader["Price"].ToString()); v.CargoSpace = Convert.ToDouble(reader["Cargo"]); v.SideDoor = (bool)reader["SideDoor"]; v.Wheelbase = Convert.ToChar(reader["WheelBase"]); // v.Available = (bool)reader["Available"]; // v.AvailableOn = (DateTime)reader["AvailableOn"]; v.RegNumber = reader["RegNumber"].ToString(); van_.Add(v); } } } catch { } finally { connect.Close(); } cars = car_; vans = van_; try { for (int i = 0; i < cars.Count; i++) { connect.Open(); SqlCommand readDates = new SqlCommand("SELECT * FROM dbo.HireDates WHERE RegNumber LIKE @regNumber;", connect); readDates.Parameters.AddWithValue("@regNumber", cars[i].RegNumber); readDates.ExecuteNonQuery(); SqlDataReader datesReader = readDates.ExecuteReader(); while (datesReader.Read()) { Hired temp = new Hired((DateTime)datesReader["StartDate"], (DateTime)datesReader["ReturnDate"]); Console.WriteLine(temp); cars[i].hiredDates.Add(temp); } cars[i].sortHiredList(); connect.Close(); } for (int i = 0; i < vans.Count; i++) { connect.Open(); SqlCommand readDates = new SqlCommand("SELECT * FROM dbo.HireDates WHERE RegNumber LIKE @regNumber;", connect); readDates.Parameters.AddWithValue("@regNumber", vans[i].RegNumber); readDates.ExecuteNonQuery(); SqlDataReader datesReader = readDates.ExecuteReader(); while (datesReader.Read()) { Hired temp = new Hired((DateTime)datesReader["StartDate"], (DateTime)datesReader["ReturnDate"]); Console.WriteLine(temp); vans[i].hiredDates.Add(temp); } vans[i].sortHiredList(); connect.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { connect.Close(); } }