public static VehicleType GetVehicleTypeOfParkedVehicle(string[] parkingPlace, int position, string registrationNumber) { VehicleType type; type = ParkingSlot.GetVehicleTypeOfParkedVehicle(parkingPlace[position], registrationNumber); return(type); }
/// <summary> /// Removes a vehicle. /// </summary> /// <param name="parkingPlace"></param> /// <param name="registrationNumber"></param> /// <returns>parking place number, check in timestamp</returns> public static KeyValuePair <int, string> Remove(string[] parkingPlace, string registrationNumber) { string checkInTimeStamp = ""; KeyValuePair <int, string> result = new KeyValuePair <int, string>(); int found = -1; for (int i = 0; i < parkingPlace.Length; i++) { if (ParkingSlot.ContainsVehicle(parkingPlace[i], registrationNumber)) { found = i; VehicleType vehicleType = GetVehicleTypeOfParkedVehicle(parkingPlace, found, registrationNumber); if (vehicleType == VehicleType.Car) { checkInTimeStamp = ParkingSlot.GetCheckInTimeStamp(parkingPlace[found]); } result = new KeyValuePair <int, string>(found, checkInTimeStamp); ParkingSlot.RemoveVehicle(ref parkingPlace[i], registrationNumber); break; } } if (found < 0) { throw new VehicleNotFoundException(); } return(result); }
/// <summary> /// Adding the Vehicle and check the type of vehicle /// </summary> /// <param name="parkingPlace"></param> /// <param name="registrationNumber"></param> /// <param name="vehicleType"></param> /// <returns></returns> public static int Add(string [] parkingPlace, string registrationNumber, VehicleType vehicleType) { int pos = FindDistinct(parkingPlace, registrationNumber); if (pos != -1) { // The registration number already exists throw new RegistrationNumberAlreadyExistException(); } pos = FindFreePlace(parkingPlace, vehicleType); bool containsOneMc = (ParkingSlot.CountMc(parkingPlace[pos]) == 1); // contains one mc and adding one mc if ((parkingPlace[pos] != null) && containsOneMc && vehicleType == VehicleType.Mc) // If parking place not empty and vehicle is motorcyle { parkingPlace[pos] = string.Concat(registrationNumber, parkingPlace[pos]); // then add the motorcycle before the ':' char before first motorcycle } else // adding to empty place { if (vehicleType == VehicleType.Mc) // if parking place empty and the vehicle is a motorcycle ? { parkingPlace[pos] = string.Concat(':', registrationNumber); // add a char of ':' at beginning of registration number string to mark it as a motorcycle } else { parkingPlace[pos] = registrationNumber + "," + DateTime.Now; // else, add it. Add timestamp } } return(pos); }
/// <summary> /// Finds a vehicle with a distinct registration number. /// </summary> /// <param name="parkingPlace"></param> /// <param name="registrationNumber"></param> /// <returns>Position of the found vehicle. -1 if not found</returns> public static int FindDistinct(string[] parkingPlace, string registrationNumber) { for (int i = 0; i < parkingPlace.Length; i++) { // Try to find motorcycle if (ParkingSlot.ContainsMc(parkingPlace[i], registrationNumber)) { // Mc found return(i); } else if (parkingPlace[i] == null) { // empty parking place. Do nothing. } else { // try to find a car string CarRegNr = null; int indexOfRegNrDateSeparator = parkingPlace[i].IndexOf(','); if (indexOfRegNrDateSeparator > -1) { CarRegNr = ParkingSlot.GetRegistrationNumber(parkingPlace[i]); } // Try to find car if (registrationNumber == CarRegNr) { // Car found return(i); } } } //Your Vehicle is not found return(-1); }
/// <summary> /// Counts the number of free parking places availabel for a specifik type of vehicle. /// </summary> /// <param name="parkingPlace"></param> /// <returns>Number of free parking places.</returns> public static int NumberOfFreeParkingPlaces(string[] parkingPlace, VehicleType type) { int numnberOfFree = 0; if (type == VehicleType.Car) { for (int i = 0; i < parkingPlace.Length; i++) { if (parkingPlace[i] == null) { numnberOfFree++; } } } else { // search for Motorcycle places for (int i = 0; i < parkingPlace.Length; i++) { if (parkingPlace[i] == null) { numnberOfFree++; } if (ParkingSlot.CountMc(parkingPlace[i]) == 1) { numnberOfFree++; } } } return(numnberOfFree); }
/// <summary> /// Moves a vehicel from a position to a new position. /// Should be used if the old position is known. /// </summary> /// <param name="parkingPlaces"></param> /// <param name="registrationNumber"></param> /// <param name="vehicleType"></param> /// <param name="oldPosition"></param> /// <param name="newPosition"></param> public static void Move(string[] parkingPlaces, string registrationNumber, VehicleType vehicleType, int oldPosition, int newPosition) { if (oldPosition < 0) { throw new ArgumentException(); } if (oldPosition > (parkingPlaces.Length - 1)) { throw new ArgumentException(); } if (newPosition < 0) { throw new ArgumentException(); } if (newPosition > (parkingPlaces.Length - 1)) { throw new ArgumentException(); } if (string.IsNullOrEmpty(registrationNumber)) { throw new ArgumentException(); } if (vehicleType == VehicleType.Car) { if (parkingPlaces[newPosition] != null) { throw new ParkingPlaceOccupiedException(); } parkingPlaces[newPosition] = parkingPlaces[oldPosition]; parkingPlaces[oldPosition] = null; } else if (vehicleType == VehicleType.Mc) { int numberOfMcAtOldPosition = ParkingSlot.CountMc(parkingPlaces[oldPosition]); if (numberOfMcAtOldPosition < 0) { throw new ArgumentException("No Mc found to move at that position."); } if (numberOfMcAtOldPosition == 2) { Remove(parkingPlaces, registrationNumber); AddMcAtPosition(parkingPlaces, registrationNumber, newPosition); } if (numberOfMcAtOldPosition == 1) { AddMcAtPosition(parkingPlaces, registrationNumber, newPosition); parkingPlaces[oldPosition] = null; } } }
/// <summary> /// Searches the parking place for vehicles with registration number mathing the search string /// </summary> /// <param name="parkingPlace">The parking place</param> /// <param name="searchString">Search string for registraion number</param> /// <returns></returns> public static Dictionary <int, string> FindSearchString(string[] parkingPlace, string searchString) { Dictionary <int, string> matchingVehicles = new Dictionary <int, string>(); for (int i = 0; i < parkingPlace.Length; i++) { string[] slotSearchResult = ParkingSlot.SearchVehicle(parkingPlace[i], searchString); if (slotSearchResult != null && slotSearchResult.Length > 0) { foreach (string match in slotSearchResult) { matchingVehicles.Add(i, match); } } } return(matchingVehicles); }
/// <summary> /// Counts the number of full parking places. /// </summary> /// <param name="parkingPlace"></param> /// <returns>Number of full parking places.</returns> public static int NumberOfFullParkingPlaces(string[] parkingPlace) { int numnberOfFull = 0; for (int i = 0; i < parkingPlace.Length; i++) { if (ParkingSlot.CountMc(parkingPlace[i]) == 2) { numnberOfFull++; } else if (ParkingSlot.CountCar(parkingPlace[i]) == 1) { numnberOfFull++; } } return(numnberOfFull); }
/// <summary> /// find the last position for the parked motocycle into the parking place /// </summary> /// <param name="parkingPlace"></param> /// <param name="startPosition"></param> /// <returns></returns> public static int FindLastSingleParkedMc(string[] parkingPlace, int startPosition) { if (startPosition > (parkingPlace.Length - 1)) { throw new ArgumentException(); } if (startPosition < 0) { throw new ArgumentException(); } for (int i = startPosition; i >= 0; i--) { if (ParkingSlot.CountMc(parkingPlace[i]) == 1) { return(i); } } // No single parked mc found => returning -1 return(-1); }
/// <summary> /// Adding the motocycle at position into the parking place /// </summary> /// <param name="parkingPlaces"></param> /// <param name="registrationNumber"></param> /// <param name="newPosition"></param> public static void AddMcAtPosition(string[] parkingPlaces, string registrationNumber, int newPosition) { if (newPosition < 0) { throw new ArgumentException(); } if (newPosition > (parkingPlaces.Length - 1)) { throw new ArgumentException(); } if (string.IsNullOrEmpty(registrationNumber)) { throw new ArgumentException(); } int numberOfMcAtNewPosition = ParkingSlot.CountMc(parkingPlaces[newPosition]); if (numberOfMcAtNewPosition == 0 && parkingPlaces[newPosition] != null) { throw new ParkingPlaceOccupiedException("The new place to move the MC to is occupied by a car."); } if (numberOfMcAtNewPosition == 2) { throw new ParkingPlaceOccupiedException("The new place to move the MC to is occupied by two MCs."); } if (parkingPlaces[newPosition] == null) { parkingPlaces[newPosition] = ":" + registrationNumber; } else { if (ParkingSlot.CountMc(parkingPlaces[newPosition]) != 1) { throw new DataMisalignedException("The parkingplace should contain one Mc but doesn't."); } // Add registration numnber at left side of sign parkingPlaces[newPosition] = registrationNumber + parkingPlaces[newPosition]; } }
/// <summary> /// remove the vehicle /// </summary> /// <param name="parkingSlot"></param> /// <param name="registrationNumber"></param> public static void RemoveVehicle(ref string parkingSlot, string registrationNumber) { VehicleType type = ParkingSlot.GetVehicleTypeOfParkedVehicle(parkingSlot, registrationNumber); if (type == VehicleType.Car) { if (ParkingSlot.GetRegistrationNumber(parkingSlot) == registrationNumber) { // Setting to null tells its empty parkingSlot = null; } else { throw new VehicleNotFoundException(); } } else if (type == VehicleType.Mc) { // Remove Mc RemoveMc(ref parkingSlot, registrationNumber); } }
/// <summary> /// Searches the parking place for a free place for a specific vehicle type. /// </summary> /// <param name="parkingPlace"></param> /// <param name="vehicleType">The type of vehicle</param> /// <returns></returns> public static int FindFreePlace(string[] parkingPlace, VehicleType vehicleType) { bool found = false; int position = 0; do { if (ParkingSlot.IsFreeFor(parkingPlace[position], vehicleType)) { found = true; } else { position++; } } while (!found & (position < parkingPlace.Length)); // Not found => throw exception if (!found && position >= parkingPlace.Length) { throw new ParkingPlaceFullException(); } return(position); }