Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Clight_29_OOP_Trains
- {
- class Program
- {
- static void Main(string[] args)
- {
- int xDefault = 0;
- int ySecondDiv = 5;
- int yThirdDiv = 9;
- Random random = new Random();
- int priceOneKM = 3;
- int wallet = 0;
- float speedTrain = 80; //средняя скорость (км/ч)
- float speedTrainMinute = speedTrain / 60;
- int numPlaceInBigCar = 48;
- int numPlaceInMidCar = 24;
- int numPlaceInSmallCar = 12;
- Station Piter = new Station("С-Петербург");
- Station Moscow = new Station("Москва");
- Station Ekaterinburg = new Station("Екатеринбург");
- Station Novosibirsk = new Station("Новосибирск");
- Station Irkutsk = new Station("Иркутск");
- Station UlanUde = new Station("Улан-Удэ");
- Station Chita = new Station("Чита");
- Station Yakutsk = new Station("Якутск");
- Route westRussia = new Route("Зап. Россия",
- new Station[] { Piter, Moscow, Ekaterinburg, Novosibirsk, Irkutsk },
- new int[] { 700, 1500, 1650, 1730 });
- Route eastRussia = new Route("Вост. Россия",
- new Station[] { Irkutsk, UlanUde, Chita, Yakutsk },
- new int[] { 500, 730, 1650, 1670 });
- RailWayCompany company = new RailWayCompany("Рельсы и шпалы", new Route[] { westRussia, eastRussia }, wallet, priceOneKM);
- RailwayCar carBig = new RailwayCar(numPlaceInBigCar);
- RailwayCar carMid = new RailwayCar(numPlaceInMidCar);
- RailwayCar carSmall = new RailwayCar(numPlaceInSmallCar);
- RailwayCar[] rangeOfCars = { carMid, carSmall, carBig };
- while (true)
- {
- Console.CursorVisible = false;
- Console.Clear();
- Console.WriteLine($"Вас приветствует ж/д компания \"{company.Name}\". Бюджет: { company.Wallet} руб.\n");
- WriteColorLine("F1 - Купить билет");
- WriteColorLine("Esc - Выход");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.Escape:
- Environment.Exit(0);
- break;
- case ConsoleKey.F1:
- bool isChooseRoute = true;
- while (isChooseRoute)
- {
- Console.Clear();
- Console.WriteLine("Маршруты компании: ");
- company.ShowRoutes();
- Console.SetCursorPosition(xDefault, ySecondDiv);
- WriteColorLine("F1 - Выбрать маршрут");
- WriteColorLine("Esc - Назад");
- ConsoleKeyInfo keyRoute = Console.ReadKey();
- switch (keyRoute.Key)
- {
- case ConsoleKey.Escape:
- isChooseRoute = false;
- break;
- case ConsoleKey.F1:
- int userInputRoute = CheckUserInput("Выберете номер маршрута", company.Routes.Length, xDefault, yThirdDiv);
- Route tempRoute = company.Routes[userInputRoute - 1];
- bool isChoosePoints = true;
- while (isChoosePoints)
- {
- Console.Clear();
- Console.WriteLine("Вы выбрали маршрут: ");
- tempRoute.ShowRoute();
- Console.SetCursorPosition(xDefault, ySecondDiv);
- WriteColorLine("F1 - Выбрать станции отправки и назначения");
- WriteColorLine("Esc - Назад");
- ConsoleKeyInfo keyPoints = Console.ReadKey();
- switch (keyPoints.Key)
- {
- case ConsoleKey.Escape:
- isChoosePoints = false;
- break;
- case ConsoleKey.F1:
- int pointA = CheckUserInput("Выберете пункт отправления", tempRoute.RouteStation.Length, xDefault, yThirdDiv);
- int pointB = CheckUserInput("Выберете пункт назначения", tempRoute.RouteStation.Length, pointA, xDefault, yThirdDiv + 1);
- int travelDistance = tempRoute.GetTravelDistance(pointA, pointB);
- int travelTimeMinute = Convert.ToInt32(travelDistance / speedTrainMinute);
- int[] travelTimeDayHourMinute = ConvertMinuteToHour(travelTimeMinute);
- int passangers = random.Next(20, 501);
- int ticketPrice = company.GetTicketPrice(travelDistance);
- int sumTicketPrice = passangers * ticketPrice;
- Dictionary<RailwayCar, int> train = CreateTrain(passangers, rangeOfCars);
- Console.Clear();
- Console.WriteLine($"ж/д компания \"{company.Name}\". Бюджет: {company.Wallet} руб.\n");
- WriteColorLine("F1 - Сформировать и запустить ж/д состав");
- WriteColorLine("Esc - Назад");
- Console.SetCursorPosition(xDefault, ySecondDiv);
- Console.Write("Ваш маршрут: \n");
- tempRoute.ShowRoute(pointA, pointB);
- Console.WriteLine("\n\nИнформация о маршруте:");
- Console.WriteLine($"Расстояние: {travelDistance} км");
- ShowTravelTime(travelTimeDayHourMinute);
- Console.WriteLine("\nФинансовая информация:");
- Console.WriteLine($"Цена билета: {ticketPrice} руб.");
- Console.WriteLine($"Куплено билетов: {passangers}");
- Console.WriteLine($"Итого: {sumTicketPrice} руб.\n");
- Console.WriteLine("Информация о составе:");
- ShowTrainDetail(train);
- ConsoleKeyInfo keyStartTrain = Console.ReadKey();
- switch (keyStartTrain.Key)
- {
- case ConsoleKey.Escape:
- isChoosePoints = false;
- break;
- case ConsoleKey.F1:
- company.AddWallet(sumTicketPrice);
- Console.Clear();
- Console.WriteLine($"ж/д компания \"{company.Name}\". Бюджет: {company.Wallet} руб.\n");
- WriteColorLine("Процесс прошел успешно.", ConsoleColor.DarkGreen);
- Console.ReadKey();
- isChoosePoints = false;
- isChooseRoute = false;
- break;
- }
- break;
- }
- }
- break;
- }
- }
- break;
- }
- }
- }
- public static void WriteColorLine(string text, ConsoleColor color = ConsoleColor.DarkYellow)
- {
- Console.ForegroundColor = color;
- Console.WriteLine(text);
- Console.ResetColor();
- }
- public static int CheckUserInput(string text, int numOfCheck, int x, int y)
- {
- bool isChecking = true;
- int input = 0;
- while (isChecking)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(text + ": ");
- bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
- if (goodInputUser && value > 0 && value <= numOfCheck)
- {
- input = value;
- isChecking = false;
- }
- else
- {
- Console.SetCursorPosition(x, y + 1);
- WriteColorLine($"Введите число от 1 до {numOfCheck}.", ConsoleColor.DarkRed);
- Console.ReadKey();
- Console.SetCursorPosition(x, y);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return input;
- }
- public static int CheckUserInput(string text, int numOfCheck, int pointA, int x, int y)
- {
- bool isChecking = true;
- int input = 0;
- while (isChecking)
- {
- Console.SetCursorPosition(x, y);
- Console.Write(text + ": ");
- bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
- if (goodInputUser && value > 0 && value <= numOfCheck && value != pointA)
- {
- input = value;
- isChecking = false;
- }
- else if (value == pointA)
- {
- Console.SetCursorPosition(x, y + 1);
- WriteColorLine($"Пункт назначения совпадает с пунктом отправления." +
- $"Введите число от 1 до {numOfCheck}, кроме {pointA}", ConsoleColor.DarkRed);
- Console.ReadKey();
- Console.SetCursorPosition(x, y);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- else
- {
- Console.SetCursorPosition(x, y + 1);
- WriteColorLine($"Введите число от 1 до {numOfCheck}, кроме {pointA}", ConsoleColor.DarkRed);
- Console.ReadKey();
- Console.SetCursorPosition(x, y);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return input;
- }
- public static int[] ConvertMinuteToHour(int travelTimeMinute)
- {
- int days = 0;
- int hours = travelTimeMinute / 60;
- int minutes = travelTimeMinute % 60;
- if (hours > 24)
- {
- days = hours / 24;
- hours -= days * 24;
- }
- int[] dayHourMinute = new int[] { days, hours, minutes };
- return dayHourMinute;
- }
- public static void ShowTravelTime(int[] travelTimeHourMinute)
- {
- if (travelTimeHourMinute[0] > 0)
- {
- Console.WriteLine($"Время в пути: {travelTimeHourMinute[0]} дн. {travelTimeHourMinute[1]} час. {travelTimeHourMinute[2]} мин.");
- }
- else
- {
- Console.WriteLine($"Время в пути: {travelTimeHourMinute[1]} час. {travelTimeHourMinute[2]} мин.");
- }
- }
- public static Dictionary<RailwayCar, int> CreateTrain(int passengers, RailwayCar[] rangeOfCars)
- {
- bool sorting = true;
- RailwayCar[] tempRangeOfCars = rangeOfCars;
- RailwayCar[] formedSortRangeOfCars = new RailwayCar[rangeOfCars.Length];
- int numDeleteCar = 0;
- while (sorting)
- {
- for (int j = 0; j < rangeOfCars.Length; j++)
- {
- RailwayCar currentMaxCar = new RailwayCar(0);
- if (tempRangeOfCars.Length == 0)
- {
- sorting = false;
- }
- else
- {
- for (int i = 0; i < tempRangeOfCars.Length; i++)
- {
- if (i < (tempRangeOfCars.Length - 1) && tempRangeOfCars[i].ItemPlace > currentMaxCar.ItemPlace)
- {
- currentMaxCar = tempRangeOfCars[i];
- numDeleteCar = i;
- }
- else if (i == (tempRangeOfCars.Length - 1) && tempRangeOfCars[i].ItemPlace > currentMaxCar.ItemPlace)
- {
- currentMaxCar = tempRangeOfCars[i];
- numDeleteCar = i;
- }
- }
- formedSortRangeOfCars[j] = currentMaxCar;
- RailwayCar[] temp2RangeOfCars = new RailwayCar[tempRangeOfCars.Length - 1];
- if (tempRangeOfCars.Length > 0)
- {
- for (int i = 0; i < numDeleteCar; i++)
- {
- temp2RangeOfCars[i] = tempRangeOfCars[i];
- }
- for (int i = numDeleteCar; i < temp2RangeOfCars.Length; i++)
- {
- temp2RangeOfCars[i] = tempRangeOfCars[i + 1];
- }
- }
- tempRangeOfCars = temp2RangeOfCars;
- }
- }
- }
- int numCurrentCar = 0;
- int[] itemCar = new int[formedSortRangeOfCars.Length];
- for (int i = 0; i < formedSortRangeOfCars.Length; i++)
- {
- numCurrentCar = passengers / formedSortRangeOfCars[i].ItemPlace;
- if (numCurrentCar > 0)
- {
- itemCar[i] = numCurrentCar;
- passengers -= numCurrentCar * formedSortRangeOfCars[i].ItemPlace;
- }
- else
- {
- itemCar[i] = 0;
- }
- }
- if (passengers > 0)
- {
- itemCar[itemCar.Length - 1] += 1;
- }
- if (itemCar[itemCar.Length - 1] > 1 &&
- formedSortRangeOfCars[formedSortRangeOfCars.Length - 2].ItemPlace >=
- 2 * formedSortRangeOfCars[formedSortRangeOfCars.Length - 1].ItemPlace)
- {
- itemCar[itemCar.Length - 1] -= 2;
- itemCar[itemCar.Length - 2] += 1;
- }
- Dictionary<RailwayCar, int> train = new Dictionary<RailwayCar, int> { };
- for (int i = 0; i < formedSortRangeOfCars.Length; i++)
- {
- train.Add(formedSortRangeOfCars[i], itemCar[i]);
- }
- return train;
- }
- public static void ShowTrainDetail(Dictionary<RailwayCar, int> train)
- {
- foreach (KeyValuePair<RailwayCar, int> keyValue in train)
- {
- if (keyValue.Value > 0)
- {
- Console.WriteLine($"Вагон {keyValue.Key.Name}: {keyValue.Value} ед.");
- }
- }
- }
- }
- class Station
- {
- public string Name { get; private set; }
- public Station(string name)
- {
- this.Name = name;
- }
- }
- class Route
- {
- public string Name { get; private set; }
- public Station[] RouteStation { get; private set; }
- public int[] RouteDistance { get; private set; }
- public Route(string name, Station[] routeStation, int[] routeDistance)
- {
- this.Name = name;
- this.RouteStation = routeStation;
- this.RouteDistance = routeDistance;
- }
- public int GetTravelDistance(int pointA, int pointB)
- {
- int tempPointA;
- int tempPointB;
- int tempTravelDistance = 0;
- if (pointA < pointB) { tempPointA = pointA; tempPointB = pointB; }
- else { tempPointA = pointB; tempPointB = pointA; }
- for (int i = tempPointA - 1; i < tempPointB - 1; i++)
- {
- tempTravelDistance += RouteDistance[i];
- }
- return tempTravelDistance;
- }
- public void ShowRoute()
- {
- int step = 0;
- for (int i = 0; i < RouteStation.Length; i++)
- {
- step++;
- Console.Write($"({step}) {RouteStation[i].Name}");
- if (i < RouteStation.Length - 1)
- {
- Console.Write(" - ");
- }
- }
- }
- public void ShowRoute(int pointA, int pointB)
- {
- int step = 0;
- if (pointA < pointB)
- {
- for (int i = pointA - 1; i < pointB; i++)
- {
- step++;
- Console.Write($"({step}) {RouteStation[i].Name}");
- if (i < pointB - 1)
- {
- Console.Write(" - ");
- }
- }
- }
- else
- {
- for (int i = pointA - 1; i >= pointB - 1; i--)
- {
- step++;
- Console.Write($"({step}) {RouteStation[i].Name}");
- if (i >= pointB)
- {
- Console.Write(" - ");
- }
- }
- }
- }
- }
- class RailWayCompany
- {
- public string Name { get; private set; }
- public Route[] Routes { get; private set; }
- public int Wallet { get; private set; }
- public int PriceOneKM { get; private set; }
- public RailWayCompany(string name, Route[] routes, int wallet, int priceOneKM)
- {
- Name = name;
- Routes = routes;
- PriceOneKM = priceOneKM;
- }
- public void ShowRoutes()
- {
- for (int i = 0; i < Routes.Length; i++)
- {
- Console.Write($"{i + 1}. ");
- Routes[i].ShowRoute();
- Console.WriteLine();
- }
- }
- public int GetTicketPrice(int travelDistance)
- {
- int tempTicketPrice = travelDistance * PriceOneKM;
- return tempTicketPrice;
- }
- public void AddWallet(int sumTicketPrice)
- {
- Wallet += sumTicketPrice;
- }
- }
- class RailwayCar
- {
- public string Name { get; private set; }
- public int ItemPlace { get; private set; }
- public RailwayCar(int itemPlace)
- {
- ItemPlace = itemPlace;
- Name = "Van-" + itemPlace;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement