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_28_OOP_Avtoservice
- {
- class Program
- {
- static void Main(string[] args)
- {
- int xStep = 23;
- int xDefault = 0;
- int spaceBetweenRow = 12;
- 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(), 2 },
- {new Wheels(), 4 },
- {new Transmission(), 3 },
- {new Brake(), 5 },
- {new Electrical(), 6 },
- {new Chassis(), 2 },
- };
- Avtoservice avtoservice = new Avtoservice("КлиникаАвто", 35000, storage);
- string[] brandCar = { "Toyota", "Volvo", "BMW", "Mercedez-Benz", "Nissan", "Fiat", "Renault", "Peugeot", "Kia", "Hundai" };
- string[] nameOwner = { "Михаил", "Виталий", "Артем", "Евгений", "Андрей", "Олег", "Петр", "Роман", "Мария", "Анна", "Ольга", "Нина" };
- Car car = CreateCar(random, brandCar);
- string carOwner = nameOwner[random.Next(0, nameOwner.Length)];
- while (true)
- {
- Console.CursorVisible = false;
- bool isRepair = true;
- Console.WriteLine($"Автосервис \"{avtoservice.Name}\"\n");
- Console.WriteLine("F1 - Начать ремонт");
- Console.WriteLine("F2 - Закупить деталь на склад");
- Console.WriteLine("F5 - Выйти");
- ConsoleKeyInfo key = Console.ReadKey(true);
- switch (key.Key)
- {
- case ConsoleKey.F1:
- while (isRepair)
- {
- Console.Clear();
- 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(true);
- 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);
- Detail tempDetail = car.GetDetailByNumber(userDetailNum);
- avtoservice.RemoveDetail(tempDetail);
- avtoservice.AddToWallet(tempDetail.PriceRepair);
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Ремонт прошел успешно.", ConsoleColor.DarkGreen);
- }
- 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:
- car = CreateCar(random, brandCar);
- carOwner = nameOwner[random.Next(0, nameOwner.Length)];
- break;
- case ConsoleKey.F5:
- Console.Clear();
- isRepair = false;
- break;
- }
- }
- break;
- case ConsoleKey.F2:
- bool isAddingDetail = true;
- while (isAddingDetail)
- {
- Console.Clear();
- avtoservice.ShowDescription();
- Console.WriteLine("F1 - Закупить деталь");
- Console.WriteLine("F5 - Назад");
- avtoservice.ShowStorage((xStep * 2) + spaceBetweenRow, xStep, yLinesAllMenu, numDetailInRow);
- ConsoleKeyInfo keyBuyDetail = Console.ReadKey(true);
- switch (keyBuyDetail.Key)
- {
- case ConsoleKey.F1:
- int itemDetail;
- Detail tempDetail = CheckInputDetail(xDefault, yLinesMainMenu, yLinesAllMenu, avtoservice, out itemDetail);
- avtoservice.AddDetails(tempDetail, itemDetail);
- int sumDetail = tempDetail.GetPrice() * itemDetail;
- avtoservice.RemoveFromWallet(sumDetail);
- break;
- case ConsoleKey.F5:
- Console.Clear();
- isAddingDetail = false;
- break;
- }
- }
- break;
- case ConsoleKey.F5:
- Environment.Exit(0);
- break;
- default:
- WriteSlaveMenu(xDefault, yLinesMainMenu, "Не корректный ввод.");
- 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
- {
- WriteSlaveMenu(xDefault, yLinesMainMenu + 1, $"Не корректные данные. Введите от 1 до {car.GetNumDetail()}.");
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return input;
- }
- static Detail CheckInputDetail(int xDefault, int yLinesMainMenu, int yLinesAllMenu, Avtoservice avtoservice, out int itemDetail)
- {
- Detail userDetail = null;
- itemDetail = 0;
- bool isInput = true;
- while (isInput)
- {
- int lengthStorage = avtoservice.Storage.Count;
- Console.SetCursorPosition(xDefault, yLinesAllMenu);
- for (int i = 0; i < lengthStorage; i++)
- {
- Console.WriteLine($"{i + 1} - {avtoservice.Storage.ElementAt(i).Key.GetName()}; Цена: {avtoservice.Storage.ElementAt(i).Key.GetPrice()} руб.");
- }
- Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
- Console.Write("Выберите деталь: ");
- bool goodInputUser = Int32.TryParse(Console.ReadLine(), out int valueNum);
- if (goodInputUser && valueNum > 0 && valueNum <= lengthStorage)
- {
- Console.Write($"Выберите количество покупаемой детали \"{avtoservice.Storage.ElementAt(valueNum - 1).Key.GetName()}\": ");
- bool goodItemDetail = Int32.TryParse(Console.ReadLine(), out int valueItem);
- int sumDetail = avtoservice.Storage.ElementAt(valueNum - 1).Key.GetPrice() * valueItem;
- if (goodItemDetail && sumDetail < avtoservice.GetWallet())
- {
- WriteSlaveMenu(xDefault, yLinesMainMenu + 1, "Покупка прошла успешно.", ConsoleColor.DarkGreen);
- itemDetail = valueItem;
- userDetail = avtoservice.Storage.ElementAt(valueNum - 1).Key;
- isInput = false;
- }
- else if (goodItemDetail && sumDetail > avtoservice.GetWallet())
- {
- int needMoney = sumDetail - avtoservice.GetWallet();
- string text = $"Не хватило { needMoney} руб.на покупку { valueItem} шт. детали \"{avtoservice.Storage.ElementAt(valueNum - 1).Key.GetName()}\"";
- WriteSlaveMenu(xDefault, yLinesMainMenu + 1, text);
- Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- else
- {
- WriteSlaveMenu(xDefault, yLinesMainMenu + 1, "Не корректно введены значения. Попробуйте снова.");
- Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- else
- {
- string text = $"Не корректные данные. Введите от 1 до {avtoservice.Storage.Count}.";
- WriteSlaveMenu(xDefault, yLinesMainMenu + 1, text);
- Console.SetCursorPosition(xDefault, yLinesMainMenu - 1);
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- Console.WriteLine(" ");
- }
- }
- return userDetail;
- }
- static void WriteSlaveMenu(int xDefault, int yLinesMainMenu, string text, ConsoleColor textColor = ConsoleColor.DarkYellow)
- {
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.WriteLine(" ");
- Console.SetCursorPosition(xDefault, yLinesMainMenu);
- Console.ForegroundColor = textColor;
- Console.WriteLine(text);
- Console.ResetColor();
- 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)
- {
- PriceRepair = CalculatingPriceRepair(GetPrice());
- Damage = damage;
- }
- public abstract string GetName();
- public 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;
- }
- public 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;
- }
- public 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;
- }
- public 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;
- }
- public 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;
- }
- public 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;
- }
- public 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 _markCar;
- readonly Detail[] CarDetails = new Detail[] { new Engine(), new Wheels(), new Transmission(), new Brake(), new Electrical(), new Chassis() };
- public Car(string markCar)
- {
- _markCar = markCar;
- }
- public void ShowCarDetail(string carOwner, int xDefault, int xStep, int yLines, int numDetailInRow)
- {
- int numLine = 0;
- int numRow = 0;
- Console.SetCursorPosition(xDefault, yLines - 3);
- Console.WriteLine("Владелец машины: " + carOwner);
- Console.WriteLine($"Марка машины: {_markCar}");
- for (int i = 0; i < CarDetails.Length; i++)
- {
- if (i > 0)
- {
- numLine++;
- if (numLine >= numDetailInRow)
- {
- numLine = 0;
- numRow++;
- }
- }
- CarDetails[i].ShowFullInfo(i + 1, xDefault + (numRow * xStep), yLines + (numLine * 5), numDetailInRow);
- }
- }
- public int GetNumDetail()
- {
- return CarDetails.Length;
- }
- public void SetDamageDetail(int[] damageDetail)
- {
- for (int i = 0; i < damageDetail.Length; i++)
- {
- CarDetails[damageDetail[i]].SetDamage();
- }
- }
- public void SetRepairDetail(int repairDetail)
- {
- CarDetails[repairDetail - 1].RepairDetail();
- }
- public bool CheckRepair(int inputUser)
- {
- if (CarDetails[inputUser - 1].Damage == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public string GetNameDetail(int inputUser)
- {
- return CarDetails[inputUser - 1].GetName();
- }
- public Detail GetDetailByNumber(int inputUser)
- {
- return CarDetails[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 int GetWallet()
- {
- return _wallet;
- }
- 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.Write("Наличие: ");
- ConsoleColor colorQuantity = ConsoleColor.White;
- if (detail.Value < 1)
- {
- colorQuantity = ConsoleColor.DarkRed;
- }
- else if (detail.Value < 4)
- {
- colorQuantity = ConsoleColor.DarkGray;
- }
- Console.ForegroundColor = colorQuantity;
- Console.WriteLine($"{detail.Value} шт.\n");
- Console.ResetColor();
- 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 tempDetail = null;
- foreach (var item in Storage.Keys)
- {
- if (detail.GetType() == item.GetType())
- {
- tempDetail = item;
- }
- }
- Storage[tempDetail]--;
- }
- public void AddDetails(Detail detail, int itemDetail)
- {
- Detail tempDetail = null;
- foreach (var item in Storage.Keys)
- {
- if (detail.GetType() == item.GetType())
- {
- tempDetail = item;
- break;
- }
- }
- Storage[tempDetail] += itemDetail;
- }
- public void AddToWallet(int number)
- {
- _wallet += number;
- }
- public void RemoveFromWallet(int number)
- {
- _wallet -= number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement