holllowknight

ДЗ Поезда

Apr 6th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TrainPlanner
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             TrainPlan Planner = new TrainPlan();
  14.             Train ActiveTrain = new Train();
  15.             bool RunProgram = true;
  16.  
  17.             while (RunProgram)
  18.             {
  19.                 Console.Clear();
  20.                 ActiveTrain.ShowInfo();
  21.                 Console.WriteLine("1 - Задать новый рейс\n2 - Выход");
  22.                 int userInput = Convert.ToInt32(Console.ReadLine());
  23.  
  24.                 switch (userInput)
  25.                 {
  26.                     case 1:
  27.                         Planner.CreateRoute();
  28.                         Planner.SellTickets();
  29.                         Planner.FormTrain();
  30.                         ActiveTrain = Planner.RunTrain();
  31.                         break;
  32.                     case 2:
  33.                         RunProgram = false;
  34.                         break;
  35.                 }
  36.             }
  37.         }
  38.  
  39.         class Car
  40.         {
  41.             public int Capacity;
  42.             public Car(int carCapacity)
  43.             {
  44.                 Capacity = carCapacity;
  45.             }
  46.         }
  47.  
  48.         class Train
  49.         {
  50.             public string Route;
  51.             public Car[] Cars;
  52.             public bool IsEnRoute;
  53.             public int Capacity { get; private set; }
  54.             public int OccupiedPlaces;
  55.  
  56.             public Train()
  57.             {
  58.                 Cars = new Car[] { };
  59.             }
  60.            
  61.             public void InsertCar(Car car)
  62.             {
  63.                 Capacity += car.Capacity;
  64.                 Array.Resize(ref Cars, Cars.Length + 1);
  65.             }
  66.            
  67.             public void ShowInfo()
  68.             {
  69.                 if (IsEnRoute)
  70.                 {
  71.                     Console.WriteLine($"Поезд <{Route}> - всего мест:{Capacity} - из них занято: {OccupiedPlaces}");
  72.                 }
  73.                 else
  74.                 {
  75.                     Console.WriteLine("Состав не сформирован");
  76.                 }
  77.             }
  78.         }
  79.         class TrainPlan
  80.         {
  81.             private Train _processedTrain;
  82.             public int SoldTickets;
  83.            
  84.             public TrainPlan()
  85.             {
  86.                 _processedTrain = new Train();
  87.             }
  88.            
  89.             public void CreateRoute()
  90.             {
  91.                 Console.WriteLine("Введите маршрут");
  92.                 _processedTrain.Route = Console.ReadLine();
  93.             }
  94.  
  95.             public void SellTickets()
  96.             {
  97.                 Random rand = new Random();
  98.                 SoldTickets = rand.Next(10, 1000);
  99.             }
  100.  
  101.             public void FormTrain()
  102.             {
  103.                 Random rand = new Random();
  104.                 int carCapacity;
  105.  
  106.                 while (_processedTrain.Capacity < SoldTickets)
  107.                 {
  108.                     carCapacity = rand.Next(30, 50);
  109.                     _processedTrain.InsertCar(new Car(carCapacity));
  110.                 }
  111.                 _processedTrain.OccupiedPlaces = SoldTickets;
  112.                 Console.WriteLine("Общая вместимость поезда: " + _processedTrain.Capacity);
  113.             }
  114.  
  115.             public Train RunTrain()
  116.             {
  117.                 _processedTrain.IsEnRoute = true;
  118.                 return _processedTrain;
  119.             }
  120.         }
  121.     }
  122. }
Add Comment
Please, Sign In to add comment