public void sortHiredList() { if (hiredDates.Count > 1) { for (int i = 0; i < hiredDates.Count; i++) { for (int x = 0; x < hiredDates.Count - 1; x++) { if (hiredDates[x].StartDate.CompareTo(hiredDates[x + 1].StartDate) == 1) { Hired temp = hiredDates[x]; hiredDates[x] = hiredDates[x + 1]; hiredDates[x + 1] = temp; } } } } }
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(); } }