Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Clight_28_OOP_Avtoservice
- {
- class Program
- {
- static void Main(string[] args)
- {
- int xStep = 23;
- int xDefault = 0;
- int spaceBetweenRow = 8;
- int yLinesMainMenu = 6;
- int yLinesSlaveMenu = 3;
- int yLinesAllMenu = yLinesMainMenu + yLinesSlaveMenu + 3;
- int numDetailInRow = 3;
- int fine = 1000; //штраф
- Random random = new Random();
- Dictionary<Detail, int> storage = new Dictionary<Detail, int>
- {
- {new Engine(), 10 },
- {new Wheels(), 20 },
- {new Transmission(), 17 },
- {new Brake(), 13 },
- {new Electrical(), 13 },
- {new Chassis(), 13 },
- };
- Avtoservice avtoservice = new Avtoservice("КлиникаАвто", 35000, storage);
- string[] brandCar = { "Toyota", "Volvo", "BMW", "Mercedez-Benz", "Nissan", "Fiat", "Renault", "Peugeot", "Kia", "Hundai" };
- string[] nameOwner = { "Михаил", "Виталий", "Артем", "Евгений", "Андрей", "Олег", "Петр", "Роман", "Мария", "Анна", "Ольга", "Нина" };
- while (true)
- {
- Console.CursorVisible = false;
- bool isRepair = true;
- Console.WriteLine($"Автосервис \"{avtoservice.name}\"\n");
- Console.WriteLine("F1 - Ремонт машины");
- Console.WriteLine("F5 - Выйти");
- ConsoleKeyInfo key = Console.ReadKey();
- switch (key.Key)
- {
- case ConsoleKey.F1:
- while (isRepair)
- {
- Console.Clear();
- Car car = CreateCar(random, brandCar);
- string carOwner = nameOwner[random.Next(0, nameOwner.Length)];
- avtoservice.ShowDescription();
- Console.WriteLine("F1 - Выбрать деталь");
- Console.WriteLine("F2 - Посмотреть следующую машину");
- Console.WriteLine("F5 - Назад");
- car.ShowCarDetail(carOwner, xDefault, xStep, yLinesAllMenu, numDetailInRow);
- avtoservice.ShowStorage((xStep * 2) + spaceBetweenRow, xStep, yLinesAllMenu, numDetailInRow);
- ConsoleKeyInfo keySlaveMenu = Console.ReadKey();
- switch (keySlaveMenu.Key)
- {
- case ConsoleKey.F1:
- int userDetailNum = CheckInputUser(xDefault, yLinesMainMenu, car);
- bool goodCheckRepair = car.CheckRepair(userDetailNum);
- string userDetailName = car.GetNameDetail(userDetailNum);
- bool isPenalty = avtoservice.CheckPenalty(userDetailName);
- if (goodCheckRepair)
- {
- bool goodCheckDetailInStorage = avtoservice.CheckRepair(userDetailName);
- if (goodCheckDetailInStorage)
- {
- car.SetRepairDetail(userDetailNum);
- avtoservice.RemoveDetail(car.GetDetailByNumber(userDetailNum));
- avtoservice.AddToWallet(car.GetDetailByNumber(userDetailNum).priceRepair);
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Ремонт прошел успешно.");
- }
- else if (isPenalty)
- {
- avtoservice.RemoveFromWallet(fine);
- WriteSlaveMenu(xDefault, yLinesMainMenu, "На складе не оказалось нужной детали. Штраф 1000 рублей.");
- }
- else
- {
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Сделка не прошла. Вне зоны ответственности Автосервиса.");
- }
- }
- else if (isPenalty)
- {
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Вас спасло от штрафа отсутствие детали на складе.");
- }
- else
- {
- avtoservice.RemoveFromWallet(fine);
- avtoservice.RemoveDetail(car.GetDetailByNumber(userDetailNum));
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Вы заменили изначально рабочую деталь. Штраф 1000 рублей.");
- }
- break;
- case ConsoleKey.F2:
- break;
- case ConsoleKey.F5:
- Console.Clear();
- isRepair = false;
- break;
- }
- }
- break;
- case ConsoleKey.F5:
- Environment.Exit(0);
- break;
- default:
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Не корректный ввод.");
- Console.ResetColor();
- Console.ReadKey();
- Console.Clear();
- break;
- }
- }
- static int GenerateNumber(Random random, int minValue, int maxValue)
- {
- return random.Next(minValue, maxValue);
- }
- static Car CreateCar(Random random, string[] brandCar)
- {
- int numMark = random.Next(0, brandCar.Length);
- Car car = new Car(brandCar[numMark]);
- //Установление деталей, нуждающихся в ремонте.
- int numDamageDetail = GenerateNumber(random, 1, 4);
- int[] damageDetail = new int[numDamageDetail];
- for (int i = 0; i < numDamageDetail; i++)
- {
- damageDetail[i] = GenerateNumber(random, 0, car.GetNumDetail());
- }
- car.SetDamageDetail(damageDetail);
- return car;
- }
- static int CheckInputUser(int xDefault, int yLinesMainMenu, Car car)
- {
- bool isInput = true;
- int input = 0;
- while (isInput)
- {
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.Write("Выберите номер ремонтируемой детали: ");
- bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int value);
- if (goodInputUser && value > 0 && value <= car.GetNumDetail())
- {
- isInput = false;
- input = value;
- }
- else
- {
- Console.WriteLine($"Не корректные данные. Введите от 1 до {car.GetNumDetail()}.");
- Console.ReadKey(true);
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return input;
- }
- static void WriteSlaveMenu(int xDefault, int yLinesMainMenu, string text)
- {
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.WriteLine(text);
- Console.ReadKey();
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.WriteLine(" ");
- }
- }
- }
- abstract class Detail
- {
- public int priceRepair { get; private set; }
- public bool damage { get; private set; }
- private float maxDifficult = 10f;
- public Detail(bool damage = false)
- {
- this.priceRepair = CalculatingPriceRepair(GetPrice());
- this.damage = damage;
- }
- public abstract string GetName();
- protected abstract int GetPrice();
- protected abstract int GetDifficulty();
- protected abstract void SetDifficultyDetail(float maxDifficult);
- private int CalculatingPriceRepair(int price)
- {
- int difficultyWorks = GetDifficulty();
- int priceTemp;
- if (difficultyWorks < 0 && difficultyWorks > maxDifficult)
- {
- SetDifficultyDetail(maxDifficult);
- difficultyWorks = 10;
- }
- float factorDyfficulty = difficultyWorks / maxDifficult;
- priceTemp = Convert.ToInt32(price * factorDyfficulty);
- return priceTemp;
- }
- public void ShowInfo(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
- {
- Console.SetCursorPosition(xDefault, yLinesAllMenu);
- Console.WriteLine($"Имя: {GetName()}");
- Console.SetCursorPosition(xDefault, yLinesAllMenu + 1);
- Console.WriteLine($"Цена: {GetPrice()} руб.");
- }
- public void ShowFullInfo(int numDetail, int xDefault, int yLines, int numDetailInRow)
- {
- string condition;
- ConsoleColor colorCondition;
- if (damage == false)
- {
- condition = "Рабочее";
- colorCondition = ConsoleColor.DarkGreen;
- }
- else
- {
- condition = "Повреждено";
- colorCondition = ConsoleColor.DarkRed;
- }
- Console.SetCursorPosition(xDefault, yLines);
- Console.Write($"{numDetail} {GetName()}");
- Console.SetCursorPosition(xDefault, yLines + 1);
- Console.WriteLine($"Цена: {GetPrice()} руб.");
- Console.SetCursorPosition(xDefault, yLines + 2);
- Console.WriteLine($"Ремонт: {priceRepair} руб.");
- Console.SetCursorPosition(xDefault, yLines + 3);
- Console.Write($"Состояние: ");
- Console.ForegroundColor = colorCondition;
- Console.Write(condition + "\n");
- Console.ResetColor();
- }
- public void RepairDetail()
- {
- damage = false;
- }
- public void SetDamage()
- {
- damage = true;
- }
- }
- class Engine : Detail
- {
- string name = "Двигатель";
- int price = 13000;
- int difficulty = 9;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Wheels : Detail
- {
- private string name = "Колёса";
- private int price = 6500;
- private int difficulty = 6;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Transmission : Detail
- {
- private string name = "Коробка передач";
- private int price = 5500;
- private int difficulty = 5;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Brake : Detail
- {
- private string name = "Тормоз. система";
- private int price = 5000;
- private int difficulty = 5;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Electrical : Detail
- {
- private string name = "Система электр.";
- private int price = 3500;
- private int difficulty = 4;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Chassis : Detail
- {
- private string name = "Ходовая часть";
- private int price = 3000;
- private int difficulty = 14;
- public override string GetName()
- {
- return name;
- }
- protected override int GetPrice()
- {
- return price;
- }
- protected override int GetDifficulty()
- {
- return difficulty;
- }
- protected override void SetDifficultyDetail(float maxDifficult)
- {
- difficulty = Convert.ToInt32(maxDifficult);
- }
- }
- class Car
- {
- private string mark;
- Detail[] car = new Detail[] { new Engine(), new Wheels(), new Transmission(), new Brake(), new Electrical(), new Chassis() };
- public Car(string mark)
- {
- this.mark = mark;
- }
- public void ShowCarDetail(string carOwner, int xDefault, int xStep, int yLines, int numDetailInRow)
- {
- int j = 0;
- int z = 0;
- Console.SetCursorPosition(xDefault, yLines - 3);
- Console.WriteLine("Владелец машины: " + carOwner);
- Console.WriteLine($"Марка машины: {mark}");
- for (int i = 0; i < car.Length; i++)
- {
- if (i > 0)
- {
- j++;
- if (j >= numDetailInRow)
- {
- j = 0;
- z++;
- }
- }
- car[i].ShowFullInfo(i + 1, xDefault + (z * xStep), yLines + (j * 5), numDetailInRow);
- }
- }
- public int GetNumDetail()
- {
- return car.Length;
- }
- public void SetDamageDetail(int[] damageDetail)
- {
- for (int i = 0; i < damageDetail.Length; i++)
- {
- car[damageDetail[i]].SetDamage();
- }
- }
- public void SetRepairDetail(int repairDetail)
- {
- car[repairDetail - 1].RepairDetail();
- }
- public bool CheckRepair(int inputUser)
- {
- if (car[inputUser - 1].damage == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public string GetNameDetail(int inputUser)
- {
- return car[inputUser - 1].GetName();
- }
- public Detail GetDetailByNumber(int inputUser)
- {
- return car[inputUser - 1];
- }
- }
- class Avtoservice
- {
- public string name { get; private set; }
- private int wallet;
- public Dictionary<Detail, int> storage { get; private set; }
- public Avtoservice(string name, int wallet, Dictionary<Detail, int> storage)
- {
- this.name = name;
- this.wallet = wallet;
- this.storage = storage;
- }
- public void ShowDescription()
- {
- Console.WriteLine($"Автосервис \"{name}\". Баланс: {wallet} руб.\n");
- }
- public void ShowStorage(int xDefault, int xStep, int yLinesAllMenu, int numDetailInRow)
- {
- int i = 0;
- int z = 0;
- Console.SetCursorPosition(xDefault, yLinesAllMenu - 3);
- Console.WriteLine("Склад:");
- foreach (KeyValuePair<Detail, int> detail in storage)
- {
- Console.SetCursorPosition(xDefault + (i * xStep), yLinesAllMenu);
- detail.Key.ShowInfo(xDefault + (z * xStep), xStep, yLinesAllMenu + (i * 4), numDetailInRow);
- Console.SetCursorPosition(xDefault + (z * xStep), yLinesAllMenu + (i * 4) + 2);
- Console.WriteLine($"Наличие: {detail.Value} шт.\n");
- i++;
- if (i % numDetailInRow == 0)
- {
- i = 0;
- z++;
- }
- }
- }
- public bool CheckRepair(string nameDetail)
- {
- bool isCheck = false;
- foreach (KeyValuePair<Detail, int> detail in storage)
- {
- if (nameDetail == detail.Key.GetName() && detail.Value > 0)
- {
- isCheck = true;
- break;
- }
- }
- return isCheck;
- }
- public bool CheckPenalty(string nameDetail)
- {
- bool isCheck = false;
- foreach (KeyValuePair<Detail, int> detail in storage)
- {
- if (nameDetail == detail.Key.GetName() && detail.Value <= 0)
- {
- isCheck = true;
- break;
- }
- }
- return isCheck;
- }
- public void RemoveDetail(Detail detail)
- {
- Detail currentDetail = detail;
- storage[currentDetail] --;
- }
- public void AddToWallet(int number)
- {
- wallet += number;
- }
- public void RemoveFromWallet(int number)
- {
- wallet -= number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement