Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework42_
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Library library = new Library();
- library.IsWork();
- }
- }
- class Book
- {
- public Book(string title, string author, int yearOfRelease, int number)
- {
- Title = title;
- Author = author;
- YearOfRelease = yearOfRelease;
- Number = number;
- }
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int YearOfRelease { get; private set; }
- public int Number { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Number}. Название: {Title} | Автор: {Author} | Год выпуска: {YearOfRelease}");
- }
- }
- class Library
- {
- private List<Book> _books;
- public Library()
- {
- _books = new List<Book>();
- }
- public void IsWork()
- {
- const string CommandAddBook = "add";
- const string CommandRemoveBook = "del";
- const string CommandShowBooks = "show";
- const string CommandFindBook = "find";
- const string CommandExitLibrary = "exit";
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine("Вы находитесь в хранилище книг");
- Console.WriteLine($"Введите {CommandAddBook} чтобы добавить книгу в хранилище.");
- Console.WriteLine($"Введите {CommandRemoveBook} чтобы удалить книгу из хранилища.");
- Console.WriteLine($"Введите {CommandShowBooks} чтобы посмотреть все книги которые находятся в харнилище.");
- Console.WriteLine($"Введите {CommandFindBook} чтобы найти нужную книгу.");
- Console.WriteLine($"Введите {CommandExitLibrary} чтобы выйти из хранилища.");
- Console.Write("Выберите действие: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandAddBook:
- AddBook();
- break;
- case CommandRemoveBook:
- RemoveBook();
- break;
- case CommandShowBooks:
- ShowAllBooks();
- break;
- case CommandFindBook:
- FindBook();
- break;
- case CommandExitLibrary:
- isWork = ExitProgram();
- break;
- default:
- Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void AddBook()
- {
- Console.WriteLine("Введите название книги:");
- string bookTitle = Console.ReadLine();
- Console.WriteLine("Введите имя автора:");
- string bookAuthor = Console.ReadLine();
- int bookYearOfRelease = ReadNumber("Введите год выпуска книги:");
- int bookNumber = _books.Count + 1;
- _books.Add(new Book(bookTitle, bookAuthor, bookYearOfRelease, bookNumber));
- Console.WriteLine("Книга добавлена!");
- }
- private void RemoveBook()
- {
- if (IsEmptyListBooks() == false)
- {
- int bookNumber = ReadNumber("Введите номер книги для удаления");
- if (TryGetBook(bookNumber, out Book book))
- {
- _books.Remove(book);
- }
- else
- {
- Console.WriteLine("Нет книги с таким номером!");
- }
- Console.WriteLine("Книга удалена!");
- }
- }
- private void ShowAllBooks()
- {
- if (IsEmptyListBooks() == false)
- {
- Console.WriteLine("Список книг:");
- foreach (Book book in _books)
- {
- book.ShowInfo();
- }
- }
- }
- private void FindBook()
- {
- if (IsEmptyListBooks() == false)
- {
- const string CommandFindByName = "name";
- const string CommandFindByAuthor = "author";
- const string CommandFindByYearOfRelease = "year";
- Console.WriteLine("Выберите параметр поиска");
- Console.WriteLine($"Введите {CommandFindByName} если хотите найти книгу по названию");
- Console.WriteLine($"Введите {CommandFindByAuthor} если хотите найти книгу по автору");
- Console.WriteLine($"Введите {CommandFindByYearOfRelease} если хотите найти книгу по году её релиза");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandFindByName:
- FindByName();
- break;
- case CommandFindByAuthor:
- FindByAuthor();
- break;
- case CommandFindByYearOfRelease:
- FindByYearOfRelease();
- break;
- default:
- Console.WriteLine("Некорректный ввод, попробуйте еще раз.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void FindByName()
- {
- bool isFoundName = false;
- string searchName = Console.ReadLine();
- foreach (Book book in _books)
- {
- if (searchName == book.Title)
- {
- book.ShowInfo();
- isFoundName = true;
- }
- }
- if (!isFoundName)
- {
- Console.WriteLine("Нет такого названия");
- }
- }
- private void FindByAuthor()
- {
- bool isFoundAuthor = false;
- string searchAuthor = Console.ReadLine();
- foreach (Book book in _books)
- {
- if (searchAuthor == book.Author)
- {
- book.ShowInfo();
- isFoundAuthor = true;
- }
- }
- if (!isFoundAuthor)
- {
- Console.WriteLine("Нет такого автора");
- }
- }
- private void FindByYearOfRelease()
- {
- bool isFoundYearOfRelease = false;
- int searchYear = ReadNumber("Введите год выпуска книги для поиска: ");
- foreach (Book book in _books)
- {
- if (searchYear == book.YearOfRelease)
- {
- book.ShowInfo();
- isFoundYearOfRelease = true;
- }
- }
- if (!isFoundYearOfRelease)
- {
- Console.WriteLine($"Нет книги с таким годом.");
- }
- }
- private bool ExitProgram()
- {
- Console.WriteLine("Вы вышли из программы!");
- return false;
- }
- private bool TryGetBook(int bookNumber, out Book foundBook)
- {
- foundBook = null;
- bool isBookFound = true;
- foreach (Book book in _books)
- {
- if (book.Number == bookNumber)
- {
- foundBook = book;
- return isBookFound;
- }
- }
- return false;
- }
- private bool IsEmptyListBooks()
- {
- bool isEmpty = false;
- if (_books.Count == 0)
- {
- isEmpty = true;
- Console.WriteLine("Список пуст!");
- }
- return isEmpty;
- }
- private int ReadNumber(string message)
- {
- int number = 0;
- bool isNumber = false;
- while (isNumber == false)
- {
- Console.Write(message);
- isNumber = int.TryParse(Console.ReadLine(), out number);
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement