Advertisement
JohnJuly

Homework42

Apr 19th, 2025
349
0
Never
Not a member of Pastebin yet?Sign Up, it unlocks many cool features!
  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3.  
  4. namespace Homework42_
  5. {
  6.     internalclass Program
  7.     {
  8.         staticvoid Main(string[] args)
  9.         {
  10.             Library library =new Library();
  11.  
  12.             library.IsWork();
  13.         }
  14.     }
  15.  
  16.     class Book
  17.     {
  18.         public Book(string title, string author, int yearOfRelease, int number)
  19.         {
  20.             Title = title;
  21.             Author = author;
  22.             YearOfRelease = yearOfRelease;
  23.             Number = number;
  24.         }
  25.  
  26.         publicstring Title {get;privateset;}
  27.         publicstring Author {get;privateset;}
  28.         publicint YearOfRelease {get;privateset;}
  29.         publicint Number {get;privateset;}
  30.  
  31.         publicvoid ShowInfo()
  32.         {
  33.             Console.WriteLine($"{Number}. Название: {Title} | Автор: {Author} | Год выпуска: {YearOfRelease}");
  34.         }
  35.     }
  36.  
  37.     class Library
  38.     {
  39.         private List<Book> _books;
  40.  
  41.         public Library()
  42.         {
  43.             _books =new List<Book>();
  44.         }
  45.  
  46.         publicvoid IsWork()
  47.         {
  48.             conststring CommandAddBook ="add";
  49.             conststring CommandRemoveBook ="del";
  50.             conststring CommandShowBooks ="show";
  51.             conststring CommandFindBook ="find";
  52.             conststring CommandExitLibrary ="exit";
  53.  
  54.             bool isWork =true;
  55.  
  56.             while(isWork)
  57.             {
  58.                 Console.WriteLine("Вы находитесь в хранилище книг");
  59.                 Console.WriteLine($"Введите {CommandAddBook} чтобы добавить книгу в хранилище.");
  60.                 Console.WriteLine($"Введите {CommandRemoveBook} чтобы удалить книгу из хранилища.");
  61.                 Console.WriteLine($"Введите {CommandShowBooks} чтобы посмотреть все книги которые находятся в харнилище.");
  62.                 Console.WriteLine($"Введите {CommandFindBook} чтобы найти нужную книгу.");
  63.                 Console.WriteLine($"Введите {CommandExitLibrary} чтобы выйти из хранилища.");
  64.                 Console.Write("Выберите действие: ");
  65.  
  66.                 string userInput = Console.ReadLine();
  67.  
  68.                 switch(userInput)
  69.                 {
  70.                     case CommandAddBook:
  71.                         AddBook();
  72.                         break;
  73.  
  74.                     case CommandRemoveBook:
  75.                         RemoveBook();
  76.                         break;
  77.  
  78.                     case CommandShowBooks:
  79.                         ShowAllBooks();
  80.                         break;
  81.  
  82.                     case CommandFindBook:
  83.                         FindBook();
  84.                         break;
  85.  
  86.                     case CommandExitLibrary:
  87.                         isWork = ExitProgram();
  88.                         break;
  89.  
  90.                     default:
  91.                         Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
  92.                         break;
  93.                 }
  94.  
  95.                 Console.ReadKey();
  96.                 Console.Clear();
  97.             }
  98.         }
  99.  
  100.         privatevoid AddBook()
  101.         {
  102.             Console.WriteLine("Введите название книги:");
  103.             string bookTitle = Console.ReadLine();
  104.  
  105.             Console.WriteLine("Введите имя автора:");
  106.             string bookAuthor = Console.ReadLine();
  107.  
  108.             int bookYearOfRelease = ReadNumber("Введите год выпуска книги:");
  109.  
  110.             int bookNumber = _books.Count+1;
  111.  
  112.             _books.Add(new Book(bookTitle, bookAuthor, bookYearOfRelease, bookNumber));
  113.  
  114.             Console.WriteLine("Книга добавлена!");
  115.         }
  116.  
  117.         privatevoid RemoveBook()
  118.         {
  119.             if(IsEmptyListBooks()==false)
  120.             {
  121.                 int bookNumber = ReadNumber("Введите номер книги для удаления");
  122.  
  123.                 if(TryGetBook(bookNumber, out Book book))
  124.                 {
  125.                     _books.Remove(book);
  126.                 }
  127.                 else
  128.                 {
  129.                     Console.WriteLine("Нет книги с таким номером!");
  130.                 }
  131.  
  132.                 Console.WriteLine("Книга удалена!");
  133.             }
  134.         }
  135.  
  136.         privatevoid ShowAllBooks()
  137.         {
  138.             if(IsEmptyListBooks()==false)
  139.             {
  140.                 Console.WriteLine("Список книг:");
  141.  
  142.                 foreach(Book book in _books)
  143.                 {
  144.                     book.ShowInfo();
  145.                 }
  146.             }
  147.         }
  148.  
  149.         privatevoid FindBook()
  150.         {
  151.             if(IsEmptyListBooks()==false)
  152.             {
  153.                 conststring CommandFindByName ="name";
  154.                 conststring CommandFindByAuthor ="author";
  155.                 conststring CommandFindByYearOfRelease ="year";
  156.  
  157.                 Console.WriteLine("Выберите параметр поиска");
  158.                 Console.WriteLine($"Введите {CommandFindByName} если хотите найти книгу по названию");
  159.                 Console.WriteLine($"Введите {CommandFindByAuthor} если хотите найти книгу по автору");
  160.                 Console.WriteLine($"Введите {CommandFindByYearOfRelease} если хотите найти книгу по году её релиза");
  161.  
  162.                 string userInput = Console.ReadLine();
  163.  
  164.                 switch(userInput)
  165.                 {
  166.                     case CommandFindByName:
  167.                         FindByName();
  168.                         break;
  169.  
  170.                     case CommandFindByAuthor:
  171.                         FindByAuthor();
  172.                         break;
  173.  
  174.                     case CommandFindByYearOfRelease:
  175.                         FindByYearOfRelease();
  176.                         break;
  177.  
  178.                     default:
  179.                         Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
  180.                         break;
  181.                 }
  182.  
  183.                 Console.ReadKey();
  184.                 Console.Clear();
  185.             }
  186.         }
  187.  
  188.         privatevoid FindByName()
  189.         {
  190.             bool isFoundName =false;
  191.             string searchName = Console.ReadLine();
  192.  
  193.             foreach(Book book in _books)
  194.             {
  195.                 if(searchName == book.Title)
  196.                 {
  197.                     book.ShowInfo();
  198.                     isFoundName =true;
  199.                 }
  200.             }
  201.  
  202.             if(!isFoundName)
  203.             {
  204.                 Console.WriteLine("Нет такого названия");
  205.             }
  206.         }
  207.  
  208.         privatevoid FindByAuthor()
  209.         {
  210.             bool isFoundAuthor =false;
  211.             string searchAuthor = Console.ReadLine();
  212.  
  213.             foreach(Book book in _books)
  214.             {
  215.                 if(searchAuthor == book.Author)
  216.                 {
  217.                     book.ShowInfo();
  218.                     isFoundAuthor =true;
  219.                 }
  220.             }
  221.  
  222.             if(!isFoundAuthor)
  223.             {
  224.                 Console.WriteLine("Нет такого автора");
  225.             }
  226.         }
  227.  
  228.         privatevoid FindByYearOfRelease()
  229.         {
  230.             bool isFoundYearOfRelease =false;
  231.             int searchYear = ReadNumber("Введите год выпуска книги для поиска: ");
  232.  
  233.             foreach(Book book in _books)
  234.             {
  235.                 if(searchYear == book.YearOfRelease)
  236.                 {
  237.                     book.ShowInfo();
  238.                     isFoundYearOfRelease =true;
  239.                 }
  240.             }
  241.  
  242.             if(!isFoundYearOfRelease)
  243.             {
  244.                 Console.WriteLine($"Нет книги с таким годом.");
  245.             }
  246.         }
  247.  
  248.         privatebool ExitProgram()
  249.         {
  250.             Console.WriteLine("Вы вышли из программы!");
  251.  
  252.             returnfalse;
  253.         }
  254.  
  255.         privatebool TryGetBook(int bookNumber, out Book foundBook)
  256.         {
  257.             foundBook =null;
  258.  
  259.             bool isBookFound =true;
  260.  
  261.             foreach(Book book in _books)
  262.             {
  263.                 if(book.Number== bookNumber)
  264.                 {
  265.                     foundBook = book;
  266.  
  267.                     return isBookFound;
  268.                 }
  269.             }
  270.  
  271.             returnfalse;
  272.         }
  273.  
  274.         privatebool IsEmptyListBooks()
  275.         {
  276.             bool isEmpty =false;
  277.  
  278.             if(_books.Count==0)
  279.             {
  280.                 isEmpty =true;
  281.                 Console.WriteLine("Список пуст!");
  282.             }
  283.  
  284.             return isEmpty;
  285.         }
  286.  
  287.         privateint ReadNumber(string message)
  288.         {
  289.             int number =0;
  290.             bool isNumber =false;
  291.  
  292.             while(isNumber ==false)
  293.             {
  294.                 Console.Write(message);
  295.                 isNumber =int.TryParse(Console.ReadLine(), out number);
  296.             }
  297.  
  298.             return number;
  299.         }
  300.     }
  301. }
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement
close