using System;
using System.Collections.Generic;
public class TrainCar
{
public string Type { get; set; }
public int Capacity { get; set; }
public TrainCar(string type, int capacity)
{
Type = type;
Capacity = capacity;
}
}
public class Station
{
public string Name { get; set; }
public int PassengersBoarding { get; set; }
public int PassengersLeaving { get; set; }
public Station(string name, int boarding, int leaving)
{
Name = name;
PassengersBoarding = boarding;
PassengersLeaving = leaving;
}
}
public class Train
{
private List<TrainCar> cars = new List<TrainCar>();
private int totalPassengers = 0;
public void AddCar(TrainCar car)
{
cars.Add(car);
}
public int GetTotalCapacity()
{
int totalCapacity = 0;
foreach (var car in cars)
{
totalCapacity += car.Capacity;
}
return totalCapacity;
}
public void UpdateStations(List<Station> stations)
{
foreach (var station in stations)
{
Console.WriteLine($"Arriving at {station.Name}...");
// تحديث عدد الركاب بناءً على المغادرين
totalPassengers -= station.PassengersLeaving;
// التأكد من عدم مغادرة عدد أكبر من الموجودين
if (totalPassengers < 0)
{
Console.WriteLine("Error: More passengers leaving than on board!");
return;
}
// إضافة الركاب الصاعدين
if (totalPassengers + station.PassengersBoarding > GetTotalCapacity())
{
Console.WriteLine("Error: Not enough capacity for all boarding passengers!");
return;
}
totalPassengers += station.PassengersBoarding;
// طباعة المعلومات الخاصة بالمحطة
PrintStationInfo(station);
}
}
private void PrintStationInfo(Station station)
{
Console.WriteLine($"At {station.Name}:");
Console.WriteLine($"- Passengers Boarding: {station.PassengersBoarding}");
Console.WriteLine($"- Passengers Leaving: {station.PassengersLeaving}");
Console.WriteLine($"- Total Passengers Now: {totalPassengers}");
Console.WriteLine(new string('-', 30));
}
}
class Program
{
static void Main(string[] args)
{
Train train = new Train();
// إضافة عربات للقطار
train.AddCar(new TrainCar("A", 40));
train.AddCar(new TrainCar("B", 25));
train.AddCar(new TrainCar("C", 10));
// تعريف المحطات
List<Station> stations = new List<Station>
{
new Station("Station 1", 20, 0),
new Station("Station 2", 10, 5),
new Station("Station 3", 15, 10),
new Station("Station 4", 5, 20),
new Station("Station 5", 0, 10)
};
// تحديث المحطات
train.UpdateStations(stations);
}
}