Advertisement
ranee

Поезд

Apr 1st, 2023
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.87 KB | None | 0 0
  1. using System.Net.Sockets;
  2.  
  3. namespace PassengerTrainConfigurator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             BoxOffice boxOffice = new BoxOffice();
  10.             RailwayStation railwayStation = new RailwayStation();
  11.            
  12.             while(boxOffice.GetDirectionCount() < 10)
  13.             {
  14.                 Console.Clear();
  15.                 boxOffice.Display();
  16.                 railwayStation.Display();
  17.                 boxOffice.CreateRoute();
  18.                 boxOffice.CreateNewClients();
  19.                 boxOffice.SellTicket();
  20.                 railwayStation.CreateTrain(boxOffice);
  21.                 Console.ReadKey();
  22.             }
  23.         }
  24.     }
  25.  
  26.     class RailwayStation
  27.     {
  28.         private List<Direction> _directions;
  29.         BusinessWagon _businessWagon = new BusinessWagon();
  30.         EconomyWagon _economyWagon = new EconomyWagon();
  31.         private List<Train> _train = new List<Train>();
  32.  
  33.         public void CreateTrain(BoxOffice boxOffice)
  34.         {
  35.             _directions = boxOffice.GetDirection();
  36.            
  37.             if(_directions.Count == 0)
  38.             {
  39.                 Console.WriteLine("Ошибка");
  40.             }
  41.             else
  42.             {
  43.                 int lastDirection = _directions.Count - 1;
  44.                 Direction chosenDirection = _directions[lastDirection];
  45.                 Train train = new Train(chosenDirection, CalculateVagon(boxOffice.CountBusiness, _businessWagon.Seats), CalculateVagon(boxOffice.CountEconomy, _economyWagon.Seats));
  46.                 _train.Add(train);
  47.             }
  48.            
  49.             Console.WriteLine($"Состав готов. Состоит из {CalculateVagon(boxOffice.CountBusiness, _businessWagon.Seats)} бизнес и " +
  50.                               $"{CalculateVagon(boxOffice.CountEconomy, _economyWagon.Seats)} эконом вагонов.");
  51.            
  52.             boxOffice.RestoreCountTicket();
  53.         }
  54.        
  55.         private int CalculateVagon(int countTickets, int totalNumberOfSeatsInWagon)
  56.         {
  57.             return (int)Math.Ceiling((double)countTickets / totalNumberOfSeatsInWagon);
  58.         }
  59.  
  60.         public void Display()
  61.         {
  62.             int yAxisIndentation = 40;
  63.            
  64.             for (int i = 0; i < _train.Count; i++)
  65.             {
  66.                 Train train = _train[i];
  67.                 Console.SetCursorPosition(yAxisIndentation, i + 1);
  68.                 train.ShowInfo();
  69.             }
  70.         }
  71.     }
  72.  
  73.     class BoxOffice
  74.     {
  75.         private int _money = 0;
  76.         private List<Client> _clients = new List<Client>();
  77.         private List<Ticket> _ticket = new List<Ticket>();
  78.         private List<Direction> _direction = new List<Direction>();
  79.        
  80.         public int CountBusiness { get; private set; }
  81.         public int CountEconomy { get; private set; }
  82.        
  83.         public void CreateRoute()
  84.         {
  85.             string from = "Екатеринрург";
  86.             Console.WriteLine("Куда поедет поезд: ");
  87.             string lastCity = Console.ReadLine();
  88.             _direction.Add(new Direction(from, lastCity));
  89.         }
  90.  
  91.         public int GetDirectionCount()
  92.         {
  93.             return _direction.Count;
  94.         }
  95.  
  96.         public List<Direction> GetDirection()
  97.         {
  98.             List<Direction> _directionNew = new List<Direction>(_direction);
  99.             return _directionNew;
  100.         }
  101.  
  102.         public void CreateNewClients()
  103.         {
  104.             _clients.Clear();
  105.             Random random = new Random();
  106.             int count = random.Next(5, 501);
  107.            
  108.             for (int i = 0; i < count; i++)
  109.             {
  110.                 _clients.Add(new Client());
  111.             }
  112.         }
  113.  
  114.         public void SellTicket()
  115.         {
  116.             int priceEconom = 300;
  117.             int priceBusunis = 400;
  118.            
  119.             Direction currentDirection = _direction[_direction.Count - 1];
  120.  
  121.             for (int i = 0; i < _clients.Count; i++)
  122.             {
  123.                 Client client = _clients[i];
  124.                
  125.                 if (client.Money > priceBusunis)
  126.                 {
  127.                     Ticket ticket = new Ticket(currentDirection, priceBusunis, client);
  128.                     _ticket.Add(ticket);
  129.                     SellTicket(priceBusunis);
  130.                     client.Buy(priceBusunis);
  131.                     CountBusiness++;
  132.                 }
  133.                 else
  134.                 {
  135.                     Ticket ticket = new Ticket(currentDirection, priceEconom, client);
  136.                     _ticket.Add(ticket);
  137.                     SellTicket(priceEconom);
  138.                     client.Buy(priceEconom);
  139.                     CountEconomy++;
  140.                 }
  141.             }
  142.            
  143.             Console.WriteLine($"Продано {CountBusiness} бизнес и {CountEconomy} эконом билетов.");
  144.         }
  145.  
  146.         public void RestoreCountTicket()
  147.         {
  148.             CountEconomy = 0;
  149.             CountBusiness = 0;
  150.         }
  151.  
  152.         public void SellTicket(int price)
  153.         {
  154.             _money += price;
  155.         }
  156.  
  157.         public void Display()
  158.         {
  159.             Console.WriteLine($"Деньги в кассе вокзала: {_money}");
  160.            
  161.             if(_direction.Count==0)
  162.             {
  163.                 Console.WriteLine($"Направление: Не создано.");
  164.             }
  165.             else
  166.             {
  167.                 for (int i = 0; i < _direction.Count; i++)
  168.                 {
  169.                     Direction direction = _direction[i];
  170.                     direction.ShowInfo();
  171.                 }
  172.             }
  173.         }
  174.     }
  175.  
  176.     class Direction
  177.     {
  178.         public Direction(string from, string lastCity)
  179.         {
  180.             From = from;
  181.             LastCity = lastCity;
  182.         }
  183.  
  184.         public string From { get; private set; }
  185.         public string LastCity { get; private set; }
  186.  
  187.         public void ShowInfo()
  188.         {
  189.             Console.WriteLine($"Направление:{From}-{LastCity}");
  190.         }
  191.     }
  192.  
  193.     class Ticket
  194.     {
  195.         private static int _idCounter = 1;
  196.        
  197.         private Direction _direction;
  198.         private Client _client;
  199.         private string _econom = "Эконом";
  200.         private string _businus = "Бизнес";
  201.        
  202.         public Ticket(Direction direction, int price, Client client)
  203.         {
  204.             _direction = direction;
  205.             _client = client;
  206.             Price = price;
  207.             UniqueNumber = _idCounter++;
  208.  
  209.             if (price >= 400)
  210.             {
  211.                 Type = _businus;
  212.             }
  213.             else
  214.             {
  215.                 Type = _econom;
  216.             }
  217.         }
  218.  
  219.         public int Price { get; private set; }
  220.         public string Type { get; private set; }
  221.         public int UniqueNumber { get; private set; }
  222.     }
  223.  
  224.     class Client
  225.     {
  226.         private int _minimumAmountOfMoney = 300;
  227.         private int _maximumAmountOfMoney = 600;
  228.         private static int _idCounter = 1;
  229.         Random random = new Random();
  230.  
  231.         public Client()
  232.         {
  233.             Money = random.Next(_minimumAmountOfMoney, _maximumAmountOfMoney);
  234.             UniqueNumber = _idCounter++;
  235.         }
  236.  
  237.         public int Money { get; private set; }
  238.         public int UniqueNumber { get; private set; }
  239.  
  240.         public void Buy(int price)
  241.         {
  242.             Money -= price;
  243.         }
  244.     }
  245.  
  246.     class Train
  247.     {
  248.         private static int _idCounter = 1;
  249.         private Direction _direction;
  250.         private List<Wagon> _wagons = new List<Wagon>();
  251.        
  252.         public Train(Direction direction, int businessWagons, int economyWagons)
  253.         {
  254.             UniqueNumber = _idCounter++;
  255.             _direction = direction;
  256.            
  257.             for (int i = 0; i < businessWagons; i++)
  258.             {
  259.                 _wagons.Add(new BusinessWagon());
  260.             }
  261.  
  262.             for (int i = 0; i < economyWagons; i++)
  263.             {
  264.                 _wagons.Add(new EconomyWagon());
  265.             }
  266.         }
  267.  
  268.         public int UniqueNumber { get; private set; }
  269.  
  270.         public int WagonCount()
  271.         {
  272.             return _wagons.Count;
  273.         }
  274.  
  275.         public void ShowInfo()
  276.         {
  277.             Console.WriteLine($"Номер поезда {UniqueNumber}, количество вагонов {WagonCount()}, поезд в пути.");
  278.         }
  279.     }
  280.     class Wagon
  281.     {
  282.         protected int seats;
  283.  
  284.         public int Seats
  285.         {
  286.             get { return seats; }
  287.             set { seats = value; }
  288.         }
  289.     }
  290.  
  291.     class BusinessWagon : Wagon
  292.     {
  293.         public BusinessWagon()
  294.         {
  295.             seats = 30;
  296.         }
  297.     }
  298.  
  299.     class EconomyWagon : Wagon
  300.     {
  301.         public EconomyWagon()
  302.         {
  303.             seats = 54;
  304.         }
  305.     }
  306. }
  307.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement