Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Home_Work
- {
- class Program
- {
- static void Main(string[] args)
- {
- LogisticsManager manager = new LogisticsManager();
- Console.WriteLine("Загрузка системы.");
- Console.WriteLine("Нажмите любую клавишу, что бы продолжить.");
- Console.ReadKey();
- Console.Clear();
- manager.Work();
- }
- }
- class LogisticsManager
- {
- private List<Train> _trains;
- public LogisticsManager()
- {
- _trains = new List<Train>();
- }
- public void Work()
- {
- bool isWorking = true;
- while (isWorking)
- {
- Console.Write("1. Сформировать поезд. \n" +
- "2. Посмотреть все отправленные поезда.\n" +
- "3. Выход.\n" +
- "Введите номер команды: ");
- switch (Console.ReadLine())
- {
- case "1":
- CreateTrain();
- break;
- case "2":
- ShowAllTrains();
- break;
- case "3":
- isWorking = false;
- break;
- default:
- Console.WriteLine("Команда не распознана.");
- Console.ReadKey();
- break;
- }
- Console.Clear();
- }
- }
- private void CreateTrain()
- {
- Direction direction = new Direction();
- CashierOffice cashierOffice = new CashierOffice();
- Console.Clear();
- direction.Create();
- cashierOffice.ApproveTicketPrice();
- cashierOffice.SellTickets();
- Train train = new Train(direction, cashierOffice);
- train.AddVagons();
- train.ShowInfo();
- _trains.Add(train);
- SendTrain(train);
- }
- private void SendTrain(Train train)
- {
- Console.WriteLine("Введите любую клавишу для отправки поезда.");
- Console.ReadKey();
- Console.Clear();
- train.Send();
- Console.WriteLine("Поезд отправлен!");
- Console.ReadKey();
- }
- private void ShowAllTrains()
- {
- Console.Clear();
- int index = 1;
- if (_trains.Count > 0)
- {
- foreach (Train train in _trains)
- {
- Console.WriteLine($"Поезд {index}.");
- train.ShowInfo();
- Console.WriteLine("______________________________________________________________________\n");
- index++;
- }
- }
- else
- {
- Console.WriteLine("Вы ещё не создали ни ондого поезда.\n" +
- "Приступайте к работе.");
- }
- Console.ReadKey();
- }
- }
- class Direction
- {
- private static IReadOnlyList<string> _stations;
- private string _pointOfDeparture;
- private string _pointOfArrival;
- static Direction ()
- {
- _stations = new List<string> { "Москва", "Санкт-Петербург", "Новосибирск", "Казань", "Хабаровск", "Владивосток", "Томск", "Калининград", "Самара" };
- }
- public Direction()
- {
- _pointOfDeparture = "Отсутствует";
- _pointOfArrival = "Отсутсвует";
- }
- public string PointOfDeparture => _pointOfDeparture;
- public string PointOfArrival => _pointOfArrival;
- public void Create()
- {
- bool isCreate = false;
- while (isCreate == false)
- {
- ShowStations();
- Console.WriteLine();
- _pointOfDeparture = ChooseStation("с которой отправиться поезд");
- Console.WriteLine($"Станция отправления поезда - {_pointOfDeparture}. \n");
- _pointOfArrival = ChooseStation("на которую прибудет поезд");
- Console.WriteLine($"Станция прибытия поезда - {_pointOfArrival}. \n");
- if (_pointOfDeparture == _pointOfArrival)
- {
- Console.WriteLine("Ошибка, ты не можешь уехать и приехать в один и тот же город, это бессмыслено! Давай по новой!");
- }
- else
- {
- isCreate = true;
- Console.WriteLine("");
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void ShowStations()
- {
- int index = 1;
- foreach (string station in _stations)
- {
- Console.WriteLine(index + ". " + station);
- index++;
- }
- }
- private string ChooseStation(string messageForChoose)
- {
- bool isSelected = false;
- string userInput;
- while (isSelected == false)
- {
- Console.Write($"Введите номер станции, {messageForChoose}: ");
- userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out int index))
- {
- if (index > 0 && index <= _stations.Count)
- {
- return _stations[index - 1];
- }
- else
- {
- Console.WriteLine("Станции с таким индексом нет.");
- }
- }
- else
- {
- Console.WriteLine("Это не число.");
- }
- }
- return null;
- }
- }
- class CashierOffice
- {
- static private int _maxPassengers;
- private int _passengers;
- private int _ticketPrice;
- static CashierOffice()
- {
- _maxPassengers = 1000;
- }
- public CashierOffice()
- {
- _passengers = 0;
- _ticketPrice = 0;
- }
- public int Passengers => _passengers;
- public int Cash => _passengers * _ticketPrice;
- public void SellTickets()
- {
- Random random = new Random();
- _passengers = random.Next(_maxPassengers);
- }
- public void ApproveTicketPrice()
- {
- bool isSetPrice = false;
- while (isSetPrice == false)
- {
- Console.Clear();
- Console.Write("Задайте цену билета: ");
- if (Int32.TryParse(Console.ReadLine(), out int price))
- {
- if (price > 0)
- {
- Console.WriteLine($"Цена билета на этот поезд - {price} рублей.");
- _ticketPrice = price;
- isSetPrice = true;
- }
- else
- {
- Console.WriteLine("Цена не может быть отрицательна или равна 0.");
- }
- }
- else
- {
- Console.WriteLine("Введите целое число больше 0.");
- }
- }
- Console.ReadKey();
- Console.Clear();
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Продано {_passengers} билетов по {_ticketPrice} рублей на общую сумму {Cash} рублей.");
- }
- }
- class Train
- {
- private List<Carriage> _carriages;
- private Direction _direction;
- private CashierOffice _cashierOffice;
- private int _passengers;
- private int _numberOfSeats;
- private bool _isSend;
- public Train(Direction direction, CashierOffice cashierOffice)
- {
- _carriages = new List<Carriage>();
- _direction = direction;
- _cashierOffice = cashierOffice;
- _passengers = _cashierOffice.Passengers;
- _isSend = false;
- }
- public bool IsEnoughPlaces => _numberOfSeats - _passengers >= 0;
- public void AddVagons()
- {
- int capacitySmallCarriage = 20;
- int capacityMediumCarriage = 60;
- int capacityLargeCarriage = 100;
- while (IsEnoughPlaces == false)
- {
- Console.WriteLine($"Необходимы места для {_passengers} пассажиров.");
- ShowCapacity();
- Console.WriteLine("\nНужны ещё вагоны.");
- Console.WriteLine($"1. Добавить маленький вагон. Вместимость - {capacitySmallCarriage} мест.\n" +
- $"2. Добавить средний вагон. Вместимость - {capacityMediumCarriage} мест.\n" +
- $"3. Добавить большой вагон. Вместимость - {capacityLargeCarriage} мест.\n");
- Console.Write("Введите, какой вагон добавить: ");
- switch (Console.ReadLine())
- {
- case "1":
- _carriages.Add(new SmallCarriage(capacitySmallCarriage));
- break;
- case "2":
- _carriages.Add(new MediumCarriage(capacityMediumCarriage));
- break;
- case "3":
- _carriages.Add(new LargeCarriage(capacityLargeCarriage));
- break;
- default:
- Console.WriteLine("Такого вагона нет.");
- break;
- }
- SumPlaces();
- Console.WriteLine("Нажмите любую клавишу для продолжения.");
- Console.ReadKey();
- Console.Clear();
- }
- }
- public void ShowInfo()
- {
- if (IsEnoughPlaces && _isSend == false)
- {
- Console.WriteLine($"Поезд готов к отправке.");
- ShowCapacity();
- }
- else if (_isSend)
- {
- Console.WriteLine("Поезд отправлен.");
- ShowCapacity();
- _cashierOffice.ShowInfo();
- Console.WriteLine($"Маршрут {_direction.PointOfDeparture} - {_direction.PointOfArrival}.");
- }
- }
- public void Send()
- {
- _isSend = true;
- }
- private void SumPlaces()
- {
- int countPlaces = 0;
- foreach (var carriage in _carriages)
- {
- countPlaces += carriage.NumberPlaces;
- }
- _numberOfSeats = countPlaces;
- }
- private void ShowCapacity()
- {
- Console.WriteLine($"Вы подготовили {_carriages.Count} вагон(ов), общей вместительностью на {_numberOfSeats} мест.");
- }
- }
- class Carriage
- {
- protected int Capacity;
- public Carriage (int capacity)
- {
- Capacity = capacity;
- }
- public int NumberPlaces => Capacity;
- }
- class SmallCarriage : Carriage
- {
- public SmallCarriage(int capacity) : base(capacity) { }
- }
- class MediumCarriage : Carriage
- {
- public MediumCarriage(int capacity) : base(capacity) { }
- }
- class LargeCarriage : Carriage
- {
- public LargeCarriage(int capacity) : base(capacity) { }
- }
- }
Add Comment
Please, Sign In to add comment