Advertisement
kaloyan_kolev

Parking.cs

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