Advertisement
Not a member of Pastebin yet?Sign Up, it unlocks many cool features!
- usingSystem;
- usingSystem.Collections.Generic;
- namespace Homework42_
- {
- internalclass Program
- {
- staticvoid 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;
- }
- publicstring Title {get;privateset;}
- publicstring Author {get;privateset;}
- publicint YearOfRelease {get;privateset;}
- publicint Number {get;privateset;}
- publicvoid ShowInfo()
- {
- Console.WriteLine($"{Number}. Название: {Title} | Автор: {Author} | Год выпуска: {YearOfRelease}");
- }
- }
- class Library
- {
- private List<Book> _books;
- public Library()
- {
- _books =new List<Book>();
- }
- publicvoid IsWork()
- {
- conststring CommandAddBook ="add";
- conststring CommandRemoveBook ="del";
- conststring CommandShowBooks ="show";
- conststring CommandFindBook ="find";
- conststring 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();
- }
- }
- privatevoid 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("Книга добавлена!");
- }
- privatevoid RemoveBook()
- {
- if(IsEmptyListBooks()==false)
- {
- int bookNumber = ReadNumber("Введите номер книги для удаления");
- if(TryGetBook(bookNumber, out Book book))
- {
- _books.Remove(book);
- }
- else
- {
- Console.WriteLine("Нет книги с таким номером!");
- }
- Console.WriteLine("Книга удалена!");
- }
- }
- privatevoid ShowAllBooks()
- {
- if(IsEmptyListBooks()==false)
- {
- Console.WriteLine("Список книг:");
- foreach(Book book in _books)
- {
- book.ShowInfo();
- }
- }
- }
- privatevoid FindBook()
- {
- if(IsEmptyListBooks()==false)
- {
- conststring CommandFindByName ="name";
- conststring CommandFindByAuthor ="author";
- conststring 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();
- }
- }
- privatevoid 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("Нет такого названия");
- }
- }
- privatevoid 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("Нет такого автора");
- }
- }
- privatevoid FindByYearOfRelease()
- {
- bool isFoundYearOfRelease =false;
- int searchYear = ReadNumber("Введите год выпуска книги для поиска: ");
- foreach(Book book in _books)
- {
- if(searchYear == book.YearOfRelease)
- {
- book.ShowInfo();
- isFoundYearOfRelease =true;
- }
- }
- if(!isFoundYearOfRelease)
- {
- Console.WriteLine($"Нет книги с таким годом.");
- }
- }
- privatebool ExitProgram()
- {
- Console.WriteLine("Вы вышли из программы!");
- returnfalse;
- }
- privatebool TryGetBook(int bookNumber, out Book foundBook)
- {
- foundBook =null;
- bool isBookFound =true;
- foreach(Book book in _books)
- {
- if(book.Number== bookNumber)
- {
- foundBook = book;
- return isBookFound;
- }
- }
- returnfalse;
- }
- privatebool IsEmptyListBooks()
- {
- bool isEmpty =false;
- if(_books.Count==0)
- {
- isEmpty =true;
- Console.WriteLine("Список пуст!");
- }
- return isEmpty;
- }
- privateint 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