Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace BookStorage
- {
- class Program
- {
- static void Main(string[] args)
- {
- const int CommandAddBook = 1;
- const int CommandRemoveBook = 2;
- const int CommandShowAllBooks = 3;
- const int CommandFindByTitle = 4;
- const int CommandFindByAuthor = 5;
- const int CommandFindByReleaseYear = 6;
- const int CommandExit = 7;
- bool isRunning = true;
- Storage storage = new Storage();
- storage.AddBook(new Book("Толковый словарь русского языка", "Ожегов", 1994));
- storage.AddBook(new Book("Оно", "Кинг", 2011));
- storage.AddBook(new Book("Грамматика", "Кронгауз", 2021));
- Console.WriteLine($"{ CommandAddBook} - Добавить книгу");
- Console.WriteLine($"{ CommandRemoveBook} - Удалить книгу");
- Console.WriteLine($"{ CommandShowAllBooks} - Показать все книги");
- Console.WriteLine($"{ CommandFindByTitle} - Найти книги по названию");
- Console.WriteLine($"{ CommandFindByAuthor} - Найти книги по автору");
- Console.WriteLine($"{ CommandFindByReleaseYear} - Найти книги по году выпуска");
- Console.WriteLine($"{ CommandExit} - Закрыть программу");
- while (isRunning)
- {
- Console.Write("\nВведите команду: ");
- int userInput = UserUtils.GetNumberFromUser();
- switch (userInput)
- {
- case CommandAddBook:
- storage.AddBook();
- break;
- case CommandRemoveBook:
- storage.RemoveBook();
- break;
- case CommandShowAllBooks:
- storage.ShowAllBooks();
- break;
- case CommandFindByTitle:
- storage.ShowBooksByTitle();
- break;
- case CommandFindByAuthor:
- storage.ShowBooksByAuthor();
- break;
- case CommandFindByReleaseYear:
- storage.ShowBooksByReleaseYear();
- break;
- case CommandExit:
- isRunning = false;
- break;
- }
- }
- }
- }
- class UserUtils
- {
- public static int GetNumberFromUser()
- {
- string userInput;
- int userNumber;
- bool isNumber = false;
- do
- {
- userInput = Console.ReadLine();
- isNumber = int.TryParse(userInput, out userNumber);
- }
- while (isNumber == false);
- return userNumber;
- }
- }
- class Storage
- {
- private List<Book> _books;
- public Storage()
- {
- _books = new List<Book>();
- }
- public void AddBook(Book book)
- {
- _books.Add(book);
- }
- public void AddBook()
- {
- Console.Write("Введите название:");
- string title = Console.ReadLine();
- Console.Write("Введите фамилию автора:");
- string author = Console.ReadLine();
- Console.Write("Введите год выпуска:");
- int releaseYear = UserUtils.GetNumberFromUser();
- _books.Add(new Book(title, author, releaseYear));
- }
- public void RemoveBook()
- {
- Console.WriteLine("Введите индекс книги, которую хотите удалить");
- int index = UserUtils.GetNumberFromUser() - 1;
- if ((index < _books.Count) && (index >= 0))
- {
- Console.Write("Удаляем книгу: ");
- ShowBook(index);
- _books.RemoveAt(index);
- }
- else
- {
- Console.WriteLine($"Книга с индексом {index} отсутствует");
- }
- }
- public void ShowBooksByTitle()
- {
- Console.WriteLine("Введите название книги, которую хотите найти");
- string title = Console.ReadLine().ToLower();
- int foundBooks = 0;
- for (int i = 0; i < _books.Count; i++)
- {
- if (_books[i].Title.ToLower() == title)
- {
- ShowBook(i);
- foundBooks++;
- }
- }
- if (foundBooks == 0)
- Console.WriteLine($"В хранилище нет с названием {title}");
- }
- public void ShowBooksByAuthor()
- {
- Console.Write("Введите автора книги, которую хотите найти: ");
- string author = Console.ReadLine();
- int foundBooks = 0;
- for (int i = 0; i < _books.Count; i++)
- {
- if (_books[i].Author == author)
- {
- ShowBook(i);
- foundBooks++;
- }
- }
- if (foundBooks == 0)
- Console.WriteLine($"В хранилище нет книг автора {author}");
- }
- public void ShowBooksByReleaseYear()
- {
- Console.Write("Книгу какого года вы хотите найти?");
- int releaseYear = UserUtils.GetNumberFromUser();
- int foundBooks = 0;
- for (int i = 0; i < _books.Count; i++)
- {
- if (_books[i].ReleaseYear == releaseYear)
- {
- ShowBook(i);
- foundBooks++;
- }
- }
- if (foundBooks == 0)
- Console.WriteLine($"В хранилище нет книг выпущенных в {releaseYear} году");
- }
- public void ShowAllBooks()
- {
- for (int i = 0; i < _books.Count; i++)
- ShowBook(i);
- }
- private void ShowBook(int index)
- {
- Console.WriteLine($"{index + 1} {_books[index].GetFullName()}");
- }
- }
- class Book
- {
- public Book(string label, string author, int releaseYear)
- {
- Title = label;
- Author = author;
- ReleaseYear = releaseYear;
- }
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int ReleaseYear { get; private set; }
- public string GetFullName()
- {
- return string.Format($"{Author} '{Title}' {ReleaseYear}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement