elena1234

SoftUniParking- class parking

Feb 9th, 2021 (edited)
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3.  
  4. namespace SoftUniParking
  5. {
  6.     public class Parking
  7.     {
  8.         private int capacity;
  9.         private List<Car> cars;
  10.  
  11.         public Parking(int capacity)
  12.         {
  13.             this.Cars = new List<Car>();
  14.             this.Capacity = capacity;
  15.         }
  16.  
  17.         public int Capacity { get => capacity; set => capacity = value; }
  18.         public List<Car> Cars { get => cars; set => cars = value; }
  19.         public int Count => this.Cars.Count();
  20.  
  21.         public string AddCar(Car car)
  22.         {
  23.             if (this.Cars.Any(c => c.RegistrationNumber == car.RegistrationNumber))
  24.             {
  25.                 return $"Car with that registration number, already exists!";
  26.             }
  27.  
  28.             else if (this.Cars.Count == this.Capacity)
  29.             {
  30.                 return $"Parking is full!";
  31.             }
  32.  
  33.             else
  34.             {
  35.                 this.Cars.Add(car);
  36.                 return $"Successfully added new car {car.Make} {car.RegistrationNumber}";
  37.             }
  38.         }
  39.  
  40.         public string RemoveCar(string registrationNumber)
  41.         {
  42.             if (!this.Cars.Any(c => c.RegistrationNumber == registrationNumber))
  43.             {
  44.                 return $"Car with that registration number, doesn't exist!";
  45.             }
  46.  
  47.             else
  48.             {
  49.                 Car findedCar = this.Cars.First(c => c.RegistrationNumber == registrationNumber);
  50.                 this.Cars.Remove(findedCar);
  51.                 return $"Successfully removed {registrationNumber}";
  52.             }
  53.         }
  54.  
  55.         public Car GetCar(string registrationNumber)
  56.         {
  57.             return this.Cars.FirstOrDefault(c => c.RegistrationNumber == registrationNumber);
  58.         }
  59.  
  60.         public void RemoveSetOfRegistrationNumber(List<string> RegistrationNumbers)
  61.         {
  62.             foreach (var number in RegistrationNumbers)
  63.             {
  64.                 if (this.Cars.Any(c => c.RegistrationNumber == number))
  65.                 {
  66.                     Car findedCar = this.Cars.First(c => c.RegistrationNumber == number);
  67.                     this.Cars.Remove(findedCar);
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment