Advertisement
LeRoY_Go

Untitled

Mar 26th, 2022
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace C_Sharp_Junior
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             Storage storage = new Storage();
  11.             bool isExit = false;
  12.  
  13.             while (isExit == false)
  14.             {
  15.                 Console.Write("1 - Создать книгу\n2 - Удалить книгу\n3 - Показать все книги\n4 - Поиск книг по параметрам\n5 - Закрыть программу\nВаш выбор:");
  16.                 bool successfulConversion = int.TryParse(Console.ReadLine(), out int userInput);
  17.                 Console.Clear();
  18.                 if (successfulConversion == true)
  19.                 {
  20.                     switch (userInput)
  21.                     {
  22.                         case 1:
  23.                             storage.CreateBook();
  24.                             break;
  25.                         case 2:
  26.                             storage.DeleteBook();
  27.                             break;
  28.                         case 3:
  29.                             storage.ShowBooks();
  30.                             break;
  31.                         case 4:
  32.                             storage.BookSearch();
  33.                             break;
  34.                         case 5:
  35.                             isExit = true;
  36.                             break;
  37.                     }
  38.                 }
  39.  
  40.                 Console.Write("Нажмите любую кнопку для возвращения в главное меню");
  41.                 Console.ReadKey();
  42.                 Console.Clear();
  43.             }
  44.         }
  45.     }
  46.  
  47.     class Storage
  48.     {
  49.         private List<Book> _books;
  50.  
  51.         public Storage()
  52.         {
  53.             _books = new List<Book> { new Book("Хулиномика", "Алексей Марков", 2017, 5), new Book("Самый богатый человек в Вавилоне", "Джордж Клейсон", 2012), new Book("Теория игр", "Диксит Авинаш", 2022, 4) };
  54.         }
  55.  
  56.         public void ShowBooks()
  57.         {
  58.             for (int i = 0; i < _books.Count; i++)
  59.             {
  60.                 Console.Write($"№{i + 1} | ");
  61.                 _books[i].ShowInfo();
  62.             }
  63.         }
  64.  
  65.         public void DeleteBook()
  66.         {
  67.             ShowBooks();
  68.             Console.Write("\nВведите название книги: ");
  69.             try
  70.             {
  71.                 int index = Convert.ToInt32(Console.ReadLine()) - 1;
  72.                 _books.RemoveAt(index);
  73.                 Console.WriteLine("Книга удалена успешна");
  74.             }
  75.             catch (Exception)
  76.             {
  77.                 Console.WriteLine("\nПроизошла ошибка(такой книги нет), повторите попытку удалить книгу");
  78.                 Console.ReadKey();
  79.                 Console.Clear();
  80.                 DeleteBook();
  81.             }
  82.         }
  83.  
  84.         public void CreateBook()
  85.         {
  86.             int initialNumberElements = _books.Count;
  87.             do
  88.             {
  89.                 try
  90.                 {
  91.                     Console.Write("Введите название книги: ");
  92.                     string title = Console.ReadLine();
  93.                     Console.Write("Введите автора книги: ");
  94.                     string author = Console.ReadLine();
  95.                     Console.Write("Введите год издания книги: ");
  96.                     int yearRelease = Convert.ToInt32(Console.ReadLine());
  97.                     Console.Write("Введите рейтинг книги от 0 до 5: ");
  98.                     int rating = Convert.ToInt32(Console.ReadLine());
  99.                     _books.Add(new Book(title, author, yearRelease, rating));
  100.                 }
  101.                 catch (Exception)
  102.                 {
  103.                     Console.WriteLine("Произошла ошибка, повторите попытку создать книгу");
  104.                     CreateBook();
  105.                 }
  106.             } while (initialNumberElements > _books.Count);
  107.  
  108.             Console.WriteLine("Книга созданна успешна");
  109.         }
  110.  
  111.         public void BookSearch()
  112.         {
  113.             Console.Write("Введите значение по которому будет осуществляться поиск: ");
  114.             string userInput = Console.ReadLine();
  115.  
  116.             if (userInput != null)
  117.             {
  118.                 for (int i = 0; i < _books.Count; i++)
  119.                 {
  120.                     if (_books[i].Title.Contains(userInput) || _books[i].Author.Contains(userInput))
  121.                     {
  122.                         _books[i].ShowInfo();
  123.                     }
  124.                     bool successfulConversion = int.TryParse(userInput, out int numberUserInput);
  125.                     if (successfulConversion == true && numberUserInput == _books[i].Rating || numberUserInput == _books[i].YearRelease)
  126.                     {
  127.                         _books[i].ShowInfo();
  128.                     }
  129.                 }
  130.             }
  131.             else
  132.             {
  133.                 Console.Write("Произошла ощибка. Повторите попытку поиска.");
  134.             }
  135.         }
  136.     }
  137.  
  138.     class Book
  139.     {
  140.         public string Title { get; private set; }
  141.         public string Author { get; private set; }
  142.         public int YearRelease { get; private set; }
  143.         public int Rating { get; private set; }
  144.  
  145.         public Book(string title, string author, int yearRelease = 0, int rating = 0)
  146.         {
  147.             Title = title;
  148.             Author = author;
  149.  
  150.             if (yearRelease >= 0)
  151.             {
  152.                 YearRelease = yearRelease;
  153.             }
  154.             if (rating >= 0 && rating <= 5)
  155.             {
  156.                 Rating = rating;
  157.             }
  158.         }
  159.  
  160.         public void ShowInfo()
  161.         {
  162.             Console.WriteLine($"Название: {Title} | Автор: {Author} | Год издания: {YearRelease} | Рейтинг: {Rating}");
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement