Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- const string AddBookCommand = "1";
- const string RemoveBookCommand = "2";
- const string ShowAllBookCommand = "3";
- const string ShowAllBookByParameterCommand = "4";
- const string ExitCommand = "5";
- string userInput;
- bool isWorking = true;
- StorageBook storageBook = new StorageBook();
- while (isWorking)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Хранилище книг.");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine($"{AddBookCommand})Добавить книгу.\n{RemoveBookCommand})Удалить книгу.\n{ShowAllBookCommand})Показать все книги." +
- $"\n{ShowAllBookByParameterCommand})Показать книги по указанному параметру.\n{ExitCommand})Выход из программы.");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case AddBookCommand:
- storageBook.AddBook();
- break;
- case RemoveBookCommand:
- storageBook.RemoveBook();
- break;
- case ShowAllBookCommand:
- storageBook.ShowInfo();
- break;
- case ShowAllBookByParameterCommand:
- storageBook.ShowInfoBooksByParameter();
- break;
- case ExitCommand:
- isWorking = false;
- break;
- }
- }
- }
- }
- class StorageBook
- {
- private List<Book> _books = new List<Book>();
- private int _universalNumber = 1000;
- public void AddBook()
- {
- string userInputName;
- string userInputAuthor;
- int userInputReleaseYear;
- Console.WriteLine("Введите название книги, её автора и год релиза.");
- userInputName = Console.ReadLine();
- userInputAuthor = Console.ReadLine();
- userInputReleaseYear = Utils.ConvertToInt();
- _books.Add(new Book(_universalNumber, userInputName, userInputAuthor, userInputReleaseYear));
- _universalNumber++;
- }
- public void RemoveBook()
- {
- if (TryGetBook(out Book book))
- {
- _books.Remove(book);
- }
- }
- private bool TryGetBook(out Book book)
- {
- ShowInfo();
- Console.WriteLine("Книгу под каким уникальным номером вы хотите удалить?");
- int userInput = Utils.ConvertToInt();
- foreach (Book item in _books)
- {
- if (item.UniversalNumber == userInput)
- {
- book = item;
- return true;
- }
- }
- book = null;
- Console.WriteLine("Книга по такому ID не найдена.");
- return false;
- }
- public void ShowInfo()
- {
- foreach (Book book in _books)
- {
- book.ShowDescription();
- }
- }
- public void ShowInfoBooksByParameter()
- {
- const string SearchByNameCommand = "1";
- const string SearchByAuthorCommand = "2";
- const string SearchByReleaseCommand = "3";
- string userInput;
- Console.WriteLine($"По какому параметру вы бы хотели найти книгу?\n{SearchByNameCommand})По имени.\n{SearchByAuthorCommand})По автору.\n{SearchByReleaseCommand})По году релиза.");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case SearchByNameCommand:
- SearchByName("Введите название книги:", _books);
- break;
- case SearchByAuthorCommand:
- SearchByAuthor("Введите имя автора:", _books);
- break;
- case SearchByReleaseCommand:
- SearchByRelease("Введите год релиза:", _books);
- break;
- }
- }
- private void SearchByName(string text, List<Book> books)
- {
- Console.WriteLine(text);
- string userInput = Console.ReadLine();
- foreach (var book in books)
- {
- if (book.Name == userInput)
- {
- book.ShowDescription();
- }
- }
- }
- private void SearchByAuthor(string text, List<Book> books)
- {
- Console.WriteLine(text);
- string userInput = Console.ReadLine();
- foreach (var book in books)
- {
- if (book.Author == userInput)
- {
- book.ShowDescription();
- }
- }
- }
- private void SearchByRelease(string text, List<Book> books)
- {
- Console.WriteLine(text);
- int userInput = Utils.ConvertToInt();
- foreach (var book in books)
- {
- if (book.ReleaseYear == userInput)
- {
- book.ShowDescription();
- }
- }
- }
- }
- class Book
- {
- public Book(int universalNumber, string name, string author, int releaseYear)
- {
- UniversalNumber = universalNumber;
- Name = name;
- Author = author;
- ReleaseYear = releaseYear;
- }
- public int UniversalNumber { get; private set; }
- public string Name { get;private set; }
- public string Author { get; private set; }
- public int ReleaseYear { get; private set; }
- public void ShowDescription()
- {
- Console.WriteLine($"Уникальный номер:{UniversalNumber}|Название книги:{Name}|Автор:{Author}|Год релиза:{ReleaseYear}");
- }
- }
- class Utils
- {
- public static int ConvertToInt()
- {
- int templateNumber;
- string userInput = string.Empty;
- while (int.TryParse(userInput, out templateNumber) == false)
- {
- userInput = Console.ReadLine();
- }
- return templateNumber;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment