/// <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> /// 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> /// 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]; } }