Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string CommandAddBook = "1";
- const string CommandRemoveBook = "2";
- const string CommandShowAllBooks = "3";
- const string CommandFindBooksWithParameters = "4";
- const string CommandExit = "5";
- StorageBook storage = new StorageBook();
- bool isWork = true;
- while(isWork)
- {
- Console.WriteLine($"{CommandAddBook} : Добавить книгу в хранилище\n" +
- $"{CommandRemoveBook} : Убрать книгу в хранилище\n" +
- $"{CommandShowAllBooks} : Показать все книги в хранилище\n" +
- $"{CommandFindBooksWithParameters} : Найти книги по указанному параметру (по названию, по автору, по году выпуска)\n" +
- $"{CommandExit} : Выход из программы\n");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandAddBook:
- storage.AddBook();
- break;
- case CommandRemoveBook:
- storage.RemoveBook();
- break;
- case CommandShowAllBooks:
- storage.ShowAllBooks();
- break;
- case CommandFindBooksWithParameters:
- storage.FindBooksWithParameters();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Нет такой комманды!");
- break;
- }
- }
- }
- }
- public enum UserCommandByFind
- {
- FindByName = 1,
- FindByAuthor,
- FindByYearRelease
- }
- class Book
- {
- public Book(string name, string author, int yearRelease)
- {
- Name = name;
- Author = author;
- YearRelease = yearRelease;
- }
- public string Name { get; private set; }
- public string Author{ get; private set; }
- public int YearRelease{ get; private set; }
- }
- class StorageBook
- {
- private List<Book> _books;
- public StorageBook()
- {
- _books = new List<Book>();
- }
- public void AddBook()
- {
- string name;
- string author;
- int yearRelease;
- Console.WriteLine("Введите название книги.");
- name = Console.ReadLine();
- Console.WriteLine("Введите автора книги.");
- author = Console.ReadLine();
- yearRelease = GetYear();
- Book book = new Book(name.Trim(),author.Trim(), yearRelease);
- _books.Add(book);
- Console.WriteLine("Книга добавлена в хранилище");
- }
- public void RemoveBook()
- {
- if(_books.Count > 0)
- {
- ShowBooks(_books);
- Console.WriteLine();
- if (TryGetBook(out Book book))
- {
- _books.Remove(book);
- Console.WriteLine("Книга удалена!");
- }
- }
- else
- {
- Console.WriteLine("В хранилище нет книг!");
- }
- }
- public void FindBooksWithParameters()
- {
- List<Book> findBooks = new List<Book>();
- Console.WriteLine($"{(int)UserCommandByFind.FindByName} : Поиск по названию книги\n" +
- $"{(int)UserCommandByFind.FindByAuthor} : Поиск по автору книги\n" +
- $"{(int)UserCommandByFind.FindByYearRelease} : Поиск по дате релиза\n");
- if(Enum.TryParse<UserCommandByFind>(Console.ReadLine(), out UserCommandByFind userInput))
- {
- Console.WriteLine("Введите название");
- string value = Console.ReadLine();
- foreach (var book in _books)
- {
- if(IsMatch(userInput, book, value))
- {
- findBooks.Add(book);
- }
- }
- }
- ShowBooks(findBooks);
- }
- public void ShowAllBooks()
- {
- ShowBooks(_books);
- }
- private void ShowBooks(List<Book> books)
- {
- if(books.Count > 0)
- {
- for (int i = 0; i < books.Count; i++)
- {
- Console.WriteLine($"{i} - {books[i].Name}, {books[i].Author}, {books[i].YearRelease}");
- }
- }
- else
- {
- Console.WriteLine("Нет такой книги");
- }
- }
- private bool TryGetBook(out Book book)
- {
- book = null;
- Console.WriteLine("Введите id книги из списка");
- if (int.TryParse(Console.ReadLine(), out int id))
- {
- if (_books.Count > id && id >= 0)
- {
- book = _books[id];
- return true;
- }
- }
- return false;
- }
- private bool IsMatch(UserCommandByFind userInput, Book book, string value)
- {
- switch (userInput)
- {
- case UserCommandByFind.FindByName:
- return book.Name.ToLower().Equals(value.ToLower(), StringComparison.OrdinalIgnoreCase);
- case UserCommandByFind.FindByAuthor:
- return book.Author.ToLower().Equals(value.ToLower(), StringComparison.OrdinalIgnoreCase);
- case UserCommandByFind.FindByYearRelease:
- return book.YearRelease.ToString() == value;
- default:
- return false;
- }
- }
- private int GetYear()
- {
- int year;
- Console.WriteLine("Введите год.");
- while (int.TryParse(Console.ReadLine(), out year) == false)
- {
- Console.WriteLine("Неверный ввод");
- }
- return year;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement