Advertisement
holllowknight

Книг хранилище

Apr 24th, 2023 (edited)
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.10 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace BookStorage
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             const int CommandAddBook = 1;
  11.             const int CommandRemoveBook = 2;
  12.             const int CommandShowAllBooks = 3;
  13.             const int CommandFindByTitle = 4;
  14.             const int CommandFindByAuthor = 5;
  15.             const int CommandFindByReleaseYear = 6;
  16.             const int CommandExit = 7;
  17.  
  18.             bool isRunning = true;
  19.             Storage storage = new Storage();
  20.             storage.AddBook(new Book("Толковый словарь русского языка", "Ожегов", 1994));
  21.             storage.AddBook(new Book("Оно", "Кинг", 2011));
  22.             storage.AddBook(new Book("Грамматика", "Кронгауз", 2021));
  23.  
  24.             Console.WriteLine($"{ CommandAddBook} - Добавить книгу");
  25.             Console.WriteLine($"{ CommandRemoveBook} - Удалить книгу");
  26.             Console.WriteLine($"{ CommandShowAllBooks} - Показать все книги");
  27.             Console.WriteLine($"{ CommandFindByTitle} - Найти книги по названию");
  28.             Console.WriteLine($"{ CommandFindByAuthor} - Найти книги по автору");
  29.             Console.WriteLine($"{ CommandFindByReleaseYear} - Найти книги по году выпуска");
  30.             Console.WriteLine($"{ CommandExit} - Закрыть программу");
  31.  
  32.             while (isRunning)
  33.             {
  34.                 Console.Write("\nВведите команду: ");
  35.                 int userInput = UserUtils.GetNumberFromUser();
  36.  
  37.                 switch (userInput)
  38.                 {
  39.                     case CommandAddBook:
  40.                         storage.AddBook();
  41.                         break;
  42.  
  43.                     case CommandRemoveBook:
  44.                         storage.RemoveBook();
  45.                         break;
  46.  
  47.                     case CommandShowAllBooks:
  48.                         storage.ShowAllBooks();
  49.                         break;
  50.  
  51.                     case CommandFindByTitle:
  52.                         storage.ShowBooksByTitle();
  53.                         break;
  54.  
  55.                     case CommandFindByAuthor:
  56.                         storage.ShowBooksByAuthor();
  57.                         break;
  58.  
  59.                     case CommandFindByReleaseYear:
  60.                         storage.ShowBooksByReleaseYear();
  61.                         break;
  62.  
  63.                     case CommandExit:
  64.                         isRunning = false;
  65.                         break;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.  
  71.     class UserUtils
  72.     {
  73.         public static int GetNumberFromUser()
  74.         {
  75.             string userInput;
  76.             int userNumber;
  77.             bool isNumber = false;
  78.  
  79.             do
  80.             {
  81.                 userInput = Console.ReadLine();
  82.                 isNumber = int.TryParse(userInput, out userNumber);
  83.             }
  84.             while (isNumber == false);
  85.  
  86.             return userNumber;
  87.         }
  88.     }
  89.  
  90.     class Storage
  91.     {
  92.         private List<Book> _books;
  93.  
  94.         public Storage()
  95.         {
  96.             _books = new List<Book>();
  97.         }
  98.  
  99.         public void AddBook(Book book)
  100.         {
  101.             _books.Add(book);
  102.         }
  103.  
  104.         public void AddBook()
  105.         {
  106.             Console.Write("Введите название:");
  107.             string title = Console.ReadLine();
  108.             Console.Write("Введите фамилию автора:");
  109.             string author = Console.ReadLine();
  110.             Console.Write("Введите год выпуска:");
  111.             int releaseYear = UserUtils.GetNumberFromUser();
  112.             _books.Add(new Book(title, author, releaseYear));
  113.         }
  114.  
  115.         public void RemoveBook()
  116.         {
  117.             Console.WriteLine("Введите индекс книги, которую хотите удалить");
  118.             int index = UserUtils.GetNumberFromUser() - 1;
  119.  
  120.             if ((index < _books.Count) && (index >= 0))
  121.             {
  122.                 Console.Write("Удаляем книгу: ");
  123.                 ShowBook(index);
  124.                 _books.RemoveAt(index);
  125.             }
  126.             else
  127.             {
  128.                 Console.WriteLine($"Книга с индексом {index} отсутствует");
  129.             }
  130.         }
  131.  
  132.         public void ShowBooksByTitle()
  133.         {
  134.             Console.WriteLine("Введите название книги, которую хотите найти");
  135.             string title = Console.ReadLine().ToLower();
  136.             int foundBooks = 0;
  137.  
  138.             for (int i = 0; i < _books.Count; i++)
  139.             {
  140.                 if (_books[i].Title.ToLower() == title)
  141.                 {
  142.                     ShowBook(i);
  143.                     foundBooks++;
  144.                 }
  145.             }
  146.  
  147.             if (foundBooks == 0)
  148.                 Console.WriteLine($"В хранилище нет с названием {title}");
  149.         }
  150.  
  151.         public void ShowBooksByAuthor()
  152.         {
  153.             Console.Write("Введите автора книги, которую хотите найти: ");
  154.             string author = Console.ReadLine();
  155.             int foundBooks = 0;
  156.  
  157.             for (int i = 0; i < _books.Count; i++)
  158.             {
  159.                 if (_books[i].Author == author)
  160.                 {
  161.                     ShowBook(i);
  162.                     foundBooks++;
  163.                 }
  164.             }
  165.  
  166.             if (foundBooks == 0)
  167.                 Console.WriteLine($"В хранилище нет книг автора {author}");
  168.         }
  169.  
  170.         public void ShowBooksByReleaseYear()
  171.         {
  172.             Console.Write("Книгу какого года вы хотите найти?");
  173.             int releaseYear = UserUtils.GetNumberFromUser();
  174.             int foundBooks = 0;
  175.  
  176.             for (int i = 0; i < _books.Count; i++)
  177.             {
  178.                 if (_books[i].ReleaseYear == releaseYear)
  179.                 {
  180.                     ShowBook(i);
  181.                     foundBooks++;
  182.                 }
  183.             }
  184.  
  185.             if (foundBooks == 0)
  186.                 Console.WriteLine($"В хранилище нет книг выпущенных в {releaseYear} году");
  187.         }
  188.  
  189.         public void ShowAllBooks()
  190.         {
  191.             for (int i = 0; i < _books.Count; i++)
  192.                 ShowBook(i);
  193.         }
  194.  
  195.         private void ShowBook(int index)
  196.         {
  197.             Console.WriteLine($"{index + 1} {_books[index].GetFullName()}");
  198.         }
  199.     }
  200.  
  201.     class Book
  202.     {
  203.         public Book(string label, string author, int releaseYear)
  204.         {
  205.             Title = label;
  206.             Author = author;
  207.             ReleaseYear = releaseYear;
  208.         }
  209.  
  210.         public string Title { get; private set; }
  211.         public string Author { get; private set; }
  212.         public int ReleaseYear { get; private set; }
  213.  
  214.         public string GetFullName()
  215.         {
  216.             return string.Format($"{Author} '{Title}' {ReleaseYear}");
  217.         }
  218.     }
  219. }
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement