Advertisement
ZhongNi

Train dispatcher

Sep 10th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Classes
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             TrainDispatcher dispatcher = new TrainDispatcher();
  11.             dispatcher.WorkMenu();
  12.         }
  13.     }
  14.  
  15.     class TrainDispatcher
  16.     {
  17.         private List<Train> trains = new List<Train>();
  18.         private Random random = new Random();
  19.  
  20.         public void WorkMenu()
  21.         {
  22.             Console.CursorVisible = false;
  23.             Console.WriteLine($"To create a train press anything...\nTo exit press 0.");
  24.  
  25.             while (Console.ReadKey().Key != ConsoleKey.D0)
  26.             {
  27.                 Console.Clear();
  28.                 CreateTrain();
  29.                 Console.WriteLine($"\nTo create a train press anything...\nTo exit press 0.\n");
  30.                 ShowTrains();
  31.             }
  32.         }
  33.  
  34.         private string CreateDirection()
  35.         {
  36.             char[] points = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' };
  37.             char pointStart = points[random.Next(points.Length)];
  38.             string direction = null;
  39.  
  40.             bool isEqual = true;
  41.            
  42.             while (isEqual)
  43.             {
  44.                 char pointFinish = points[random.Next(points.Length)];
  45.                 direction = pointStart + " - " + pointFinish;
  46.  
  47.                 isEqual = pointFinish == pointStart ? true : false;
  48.             }
  49.  
  50.             Console.WriteLine($"{direction} train created");
  51.             return direction;
  52.         }
  53.  
  54.         private int SellTickets()
  55.         {
  56.             int maxPassengers = 100;
  57.             int numberPassengers = random.Next(maxPassengers);
  58.             Console.WriteLine($"{numberPassengers} tickets sold");
  59.             return numberPassengers;
  60.         }
  61.  
  62.         private int AttachCars()
  63.         {
  64.             int passengersInCar = 10;
  65.             int numberPassengers = SellTickets();
  66.  
  67.             int numberCars = numberPassengers % passengersInCar == 0 ?
  68.                 numberPassengers / passengersInCar : numberPassengers / passengersInCar + 1;
  69.  
  70.             Console.WriteLine($"Attached {numberCars} cars");
  71.             return numberCars;
  72.         }
  73.  
  74.         private void CreateTrain()
  75.         {
  76.             trains.Add(new Train(CreateDirection(), AttachCars()));
  77.         }
  78.  
  79.         private void ShowTrains()
  80.         {
  81.             char carSymbol = '=';
  82.             string trainSign = ">>";
  83.  
  84.             for (int i = 0; i < 10; i++)
  85.             {
  86.                 int index = trains.Count - 1 - i;
  87.  
  88.                 i = trains.Count - 1 - (i + 1) >= 0 ? i : 10;
  89.  
  90.                 Console.WriteLine(trains[index].Direction + " " + new string(carSymbol, trains[index].CarValue) + trainSign);
  91.             }
  92.         }
  93.     }
  94.  
  95.     class Train
  96.     {
  97.         public Train(string direction, int carValue)
  98.         {
  99.             Direction = direction;
  100.             CarValue = carValue;
  101.         }
  102.  
  103.         public string Direction { get; private set; }
  104.         public int CarValue { get; private set; }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement