Advertisement
Guest User

Parking class

a guest
Oct 30th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SoftUniParking
  7. {
  8. public class Parking
  9. {
  10. private List<Car> cars = new List<Car>();
  11. private int capacity;
  12.  
  13.  
  14. public Parking (int capacity)
  15. {
  16. this.Capacity = capacity;
  17. this.Cars = new List<Car>();
  18. }
  19. public Parking(List<Car> cars, int capacity) :this(capacity)
  20. {
  21. this.Cars = new List<Car>();
  22. this.Cars = cars;
  23. }
  24.  
  25. public string AddCar(Car Car)
  26. {
  27. string result = $"";
  28. Car currentCar = Car;
  29. bool carFound = false;
  30. this.Cars = cars;
  31. if (this.Cars.Contains(Car))
  32. {
  33. carFound = true;
  34. result = "Car with that registration number, already exists!";
  35. }
  36. else if (this.Cars.Count() >= this.Capacity)
  37. {
  38. result = "Parking is full!";
  39. }
  40. else
  41. {
  42. this.Cars.Add(currentCar);
  43. result = $"Successfully added new car {currentCar.Make} {currentCar.RegistrationNumber}";
  44. }
  45. return result;
  46. }
  47. public string RemoveCar(string RegistrationNumber)
  48. {
  49. string result = "";
  50. bool carFound = false;
  51. for (int i = 0; i < this.Cars.Count; i++)
  52. {
  53. if (this.Cars[i].RegistrationNumber == RegistrationNumber)
  54. {
  55. carFound = true;
  56. this.Cars.RemoveAt(i);
  57. result = $"Successfully removed {RegistrationNumber}";
  58. break;
  59. }
  60. }
  61. if (!carFound)
  62. {
  63. result = $"Car with that registration number, doesn't exist!";
  64. }
  65. return result;
  66. }
  67. public Car GetCar(string RegistrationNumber)
  68. {
  69. Car currentCar = new Car("","",0,"");
  70. foreach (var car in this.Cars)
  71. {
  72. if (car.RegistrationNumber == RegistrationNumber)
  73. {
  74. currentCar = car;
  75. break;
  76. }
  77. }
  78. return currentCar;
  79. }
  80. public void RemoveSetOfRegistrationNumber(List<string> RegistrationNumbers)
  81. {
  82. for (int i = 0; i < Cars.Count; i++)
  83. {
  84. if(RegistrationNumbers.Contains(this.Cars[i].RegistrationNumber))
  85. {
  86. this.Cars.RemoveAt(i);
  87. i--;
  88. }
  89. }
  90. }
  91. public List<Car> Cars {
  92. get { return cars; }
  93. set { this.cars = value; }
  94. }
  95. public int Capacity
  96. {
  97. get { return capacity; }
  98. set { this.capacity = value; }
  99. }
  100. public int Count
  101. {
  102. get { return cars.Count; }
  103. set { this.Count = cars.Count; }
  104. }
  105. }
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement