// Help-method to generate a random vehicle // takes no arguments // returns the generated Vehicle private Vehicle GenerateRandomVehicle() { string regNr; while (GetVehicle(regNr = getRandomRegNr()) != null) { ; // Find a regNr that isn't already used by a vehicle in the garage } v_Vehicle type = GetRandomVehicleType(); string now = GetDate(); switch (type) { case v_Vehicle.Bus: return(new Bus() { regNr = regNr, dateTime = now }); case v_Vehicle.Car: return(new Car() { regNr = regNr, dateTime = now, }); case v_Vehicle.MC: return(new MC() { regNr = regNr, dateTime = now, }); case v_Vehicle.Truck: return(new Truck() { regNr = regNr, dateTime = now, }); default: return(null); } }
public MenuVehicles(DisplayController dc, GarageController gc, v_Vehicle type) { int nav = 0; do { string[] vehicles = gc.FindVehiclesByType(type).ToArray(); Titel = "Choose Vehicle:"; Answers = new string[vehicles.Length + 1]; Answers[0] = "Back"; vehicles.CopyTo(Answers, 1); dc.Layer++; Length = Answers.Length; nav = GetAnswer(dc); if (nav != 0) { new MenuThisVehicle(dc, gc, Answers[nav]); } dc.RemoveLine(); dc.UpdateDisplay(); dc.Layer--; dc.UpdateDisplay(); } while (nav != 0); }
// !Mainly used for manual testing with different vehicle types! // Adds a new Vehicle to the list of Vehicles - // Creates a new Vehicle instance and assigns it a free parking space if available // takes argument v_Vehicle - type of vehicle to be created // returns a string with the return message of the method public string ParkVehicle(v_Vehicle type) { // Checks if the garage is full, so we can exit earlier if the garage is full if (NumberOfParkingSpaces == m_Vehicles.Count) { return($"The garage is full."); } // Get the current date string dateNow = GetDate(); // the new vehicle to be added Vehicle vehicle; // = GenerateRandomVehicle(); switch (type) { case v_Vehicle.Bus: vehicle = new Bus() { regNr = getRandomRegNr(), dateTime = dateNow }; break; case v_Vehicle.Car: vehicle = new Car() { regNr = getRandomRegNr(), dateTime = dateNow }; break; case v_Vehicle.MC: vehicle = new MC() { regNr = getRandomRegNr(), dateTime = dateNow }; break; case v_Vehicle.Truck: vehicle = new Truck() { regNr = getRandomRegNr(), dateTime = dateNow }; break; default: vehicle = null; break; } // so we can exit earlier if there's not enough space in the garage if (GetNumberOfFreeSpaces() < vehicle.vehicleSize) { return($"The garage is full!!."); } ParkingSpot index = FindFreeParkingSpace(vehicle.vehicleSize); // index used to find empty parking space // checks if we found a free parking space for the type of vehicle if (index.X == -1 && index.Y == -1) { return($"Not enough empty spaces in the garage for a vehicle of type: { vehicle.vehicleType.ToString()}."); } // sets the found parking space(s) to occupied/true for (int i = 0; i < vehicle.vehicleSize; i++) { m_ParkingSpaces[index.X, index.Y + i] = !m_ParkingSpaces[index.X, index.Y + i]; } // assigns the vehicle its parking space vehicle.parkingSpot = index; // add the new vehicle to the list m_Vehicles.Add(vehicle); return($"The vehicle of the type: {vehicle.vehicleType.ToString()} " + $"with the registration number: {vehicle.regNr} \nwas parked in the garage at " + $"parking space(s): {index.ToString()}" + (vehicle.vehicleSize > 1 ? $" to [{index.X + 1},{index.Y + vehicle.vehicleSize}]." : "")); }
// Finds all vehicles of the given type // takes argument: v_Vehicle type - the type of the vehicle // returns a string with the return message of the method public List <string> FindVehiclesByType(v_Vehicle type) { return(m_Vehicles.Where(v => v.vehicleType == type).Select(v => v.BasicInfo()).ToList()); }