fork download
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class TrainCar
  5. {
  6. public string Type { get; set; }
  7. public int Capacity { get; set; }
  8.  
  9. public TrainCar(string type, int capacity)
  10. {
  11. Type = type;
  12. Capacity = capacity;
  13. }
  14. }
  15.  
  16. public class Station
  17. {
  18. public string Name { get; set; }
  19. public int PassengersBoarding { get; set; }
  20. public int PassengersLeaving { get; set; }
  21.  
  22. public Station(string name, int boarding, int leaving)
  23. {
  24. Name = name;
  25. PassengersBoarding = boarding;
  26. PassengersLeaving = leaving;
  27. }
  28. }
  29.  
  30. public class Train
  31. {
  32. private List<TrainCar> cars = new List<TrainCar>();
  33. private int totalPassengers = 0;
  34.  
  35. public void AddCar(TrainCar car)
  36. {
  37. cars.Add(car);
  38. }
  39.  
  40. public int GetTotalCapacity()
  41. {
  42. int totalCapacity = 0;
  43. foreach (var car in cars)
  44. {
  45. totalCapacity += car.Capacity;
  46. }
  47. return totalCapacity;
  48. }
  49.  
  50. public void UpdateStations(List<Station> stations)
  51. {
  52. foreach (var station in stations)
  53. {
  54. Console.WriteLine($"Arriving at {station.Name}...");
  55.  
  56. // تحديث عدد الركاب بناءً على المغادرين
  57. totalPassengers -= station.PassengersLeaving;
  58.  
  59. // التأكد من عدم مغادرة عدد أكبر من الموجودين
  60. if (totalPassengers < 0)
  61. {
  62. Console.WriteLine("Error: More passengers leaving than on board!");
  63. return;
  64. }
  65.  
  66. // إضافة الركاب الصاعدين
  67. if (totalPassengers + station.PassengersBoarding > GetTotalCapacity())
  68. {
  69. Console.WriteLine("Error: Not enough capacity for all boarding passengers!");
  70. return;
  71. }
  72.  
  73. totalPassengers += station.PassengersBoarding;
  74.  
  75. // طباعة المعلومات الخاصة بالمحطة
  76. PrintStationInfo(station);
  77. }
  78. }
  79.  
  80. private void PrintStationInfo(Station station)
  81. {
  82. Console.WriteLine($"At {station.Name}:");
  83. Console.WriteLine($"- Passengers Boarding: {station.PassengersBoarding}");
  84. Console.WriteLine($"- Passengers Leaving: {station.PassengersLeaving}");
  85. Console.WriteLine($"- Total Passengers Now: {totalPassengers}");
  86. Console.WriteLine(new string('-', 30));
  87. }
  88. }
  89.  
  90. class Program
  91. {
  92. static void Main(string[] args)
  93. {
  94. Train train = new Train();
  95.  
  96. // إضافة عربات للقطار
  97. train.AddCar(new TrainCar("A", 40));
  98. train.AddCar(new TrainCar("B", 25));
  99. train.AddCar(new TrainCar("C", 10));
  100.  
  101. // تعريف المحطات
  102. List<Station> stations = new List<Station>
  103. {
  104. new Station("Station 1", 20, 0),
  105. new Station("Station 2", 10, 5),
  106. new Station("Station 3", 15, 10),
  107. new Station("Station 4", 5, 20),
  108. new Station("Station 5", 0, 10)
  109. };
  110.  
  111. // تحديث المحطات
  112. train.UpdateStations(stations);
  113. }
  114. }
  115.  
Success #stdin #stdout 0.06s 29404KB
stdin
Standard input is empty
stdout
Arriving at Station 1...
At Station 1:
- Passengers Boarding: 20
- Passengers Leaving: 0
- Total Passengers Now: 20
------------------------------
Arriving at Station 2...
At Station 2:
- Passengers Boarding: 10
- Passengers Leaving: 5
- Total Passengers Now: 25
------------------------------
Arriving at Station 3...
At Station 3:
- Passengers Boarding: 15
- Passengers Leaving: 10
- Total Passengers Now: 30
------------------------------
Arriving at Station 4...
At Station 4:
- Passengers Boarding: 5
- Passengers Leaving: 20
- Total Passengers Now: 15
------------------------------
Arriving at Station 5...
At Station 5:
- Passengers Boarding: 0
- Passengers Leaving: 10
- Total Passengers Now: 5
------------------------------