Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Library library = new Library();
- library.Work();
- }
- }
- class Library
- {
- private List<Book> _books = new List<Book>();
- public Library()
- {
- FillList();
- }
- public void Work()
- {
- const string CommandAddBook = "1";
- const string CommandRemoveBook = "2";
- const string CommandShowBooks = "3";
- const string CommandFindBook = "4";
- const string CommandExit = "5";
- bool isWork = true;
- while (isWork)
- {
- Console.Write($"{CommandAddBook} - добавить книгу" +
- $"\n{CommandRemoveBook} - убрать книгу" +
- $"\n{CommandShowBooks} - посмотреть книги" +
- $"\n{CommandFindBook} - найти книгу по параметру" +
- $"\n{CommandExit} - выйти из программы" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandAddBook:
- AddBook();
- break;
- case CommandRemoveBook:
- RemoveBook();
- break;
- case CommandShowBooks:
- ShowBooks();
- break;
- case CommandFindBook:
- FindBook();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.Write("Некорректный ввод");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void FillList()
- {
- _books.Add(new Book("Александр", "Пушкин", "Евгений Онегин", 1823));
- _books.Add(new Book("Александр", "Пушкин", "Капитанская дочка", 1836));
- _books.Add(new Book("Федор", "Достовский", "Идиот", 1867));
- }
- private void AddBook()
- {
- bool isYear = true;
- Console.Write("Введите имя автора: ");
- string firstAuthorName = Console.ReadLine();
- Console.Write("Введите фамилию автора: ");
- string lastAuthorName = Console.ReadLine();
- Console.Write("Введите название книги: ");
- string bookName = Console.ReadLine();
- Console.Write("Введите год издания книги: ");
- string userInputYear = Console.ReadLine();
- while (isYear)
- {
- if (int.TryParse(userInputYear, out int year))
- {
- _books.Add(new Book(firstAuthorName, lastAuthorName, bookName, year));
- isYear = false;
- }
- else
- {
- Console.Write("Введите год в числовых значениях: ");
- userInputYear = Console.ReadLine();
- }
- }
- }
- private bool IsListFull()
- {
- if (_books.Count > 0)
- {
- return true;
- }
- else
- {
- Console.WriteLine("Список пуст");
- return false;
- }
- }
- private void RemoveBook()
- {
- if (IsListFull())
- {
- ShowBooks();
- Console.Write("Введите номер для удаления: ");
- if (int.TryParse(Console.ReadLine(), out int index))
- {
- if (index > 0 && index <= _books.Count)
- _books.RemoveAt(index - 1);
- else
- Console.WriteLine("Такого номера нет");
- }
- else
- {
- Console.WriteLine("Некорректный ввод");
- }
- }
- }
- private void ShowBooks()
- {
- if (IsListFull())
- {
- for (int i = 0; i < _books.Count; i++)
- {
- Console.Write(i + 1 + ". ");
- _books[i].ShowInfo();
- }
- }
- }
- private void FindBook()
- {
- const string CommandFindAuthor = "1";
- const string CommandFindName = "2";
- const string CommandFindYear = "3";
- if (IsListFull())
- {
- Console.Write($"{CommandFindAuthor} - поиск по автору" +
- $"\n{CommandFindName} - поиск по названию" +
- $"\n{CommandFindYear} - поиск по году" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandFindAuthor:
- FindAuthor();
- break;
- case CommandFindName:
- FindName();
- break;
- case CommandFindYear:
- FindYear();
- break;
- default:
- Console.Write("Некорректный ввод");
- break;
- }
- }
- }
- private void FindAuthor()
- {
- bool isBook = true;
- Console.Write("Введите фамилию автора: ");
- string lastAuthorName = Console.ReadLine();
- foreach (var book in _books)
- {
- if (lastAuthorName == book.Author.LastName)
- {
- book.ShowInfo();
- isBook = false;
- }
- }
- if (isBook)
- Console.Write("Книга не найдена");
- }
- private void FindName()
- {
- bool isBook = true;
- Console.Write("Введите название книги: ");
- string bookName = Console.ReadLine();
- foreach (var book in _books)
- {
- if (bookName == book.Name)
- {
- book.ShowInfo();
- isBook = false;
- }
- }
- if (isBook)
- Console.Write("Книга не найдена");
- }
- private void FindYear()
- {
- bool isBook = true;
- Console.Write("Введите год книги: ");
- if (int.TryParse(Console.ReadLine(), out int year))
- {
- foreach (var book in _books)
- {
- if (year == book.Year)
- {
- book.ShowInfo();
- isBook = false;
- }
- }
- }
- else
- {
- Console.Write("Некорректный ввод");
- }
- if (isBook)
- Console.Write("Книга не найдена");
- }
- }
- class Book
- {
- public Book(string firstAuthorName, string lastAuthorName, string name, int year)
- {
- Author = new Author(firstAuthorName, lastAuthorName);
- Name = name;
- Year = year;
- }
- public string Name { get; }
- public int Year { get; }
- public Author Author { get; }
- public void ShowInfo()
- {
- char separator = '-';
- int separatorLength = 10;
- Console.WriteLine($"Автор: {Author.FirstName} {Author.LastName}" +
- $"\nНазвание: {Name}" +
- $"\nГод издания: {Year}");
- Console.WriteLine(new string(separator, separatorLength));
- }
- }
- class Author
- {
- public Author(string firstName, string lastName)
- {
- FirstName = firstName;
- LastName = lastName;
- }
- public string FirstName { get; }
- public string LastName { get; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment