Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save trayforyou/4dcd4e6d79cbb90085bddc9909d6a05e to your computer and use it in GitHub Desktop.
Save trayforyou/4dcd4e6d79cbb90085bddc9909d6a05e to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
namespace OOP_number5
{
internal class Program
{
static void Main(string[] args)
{
const string CommandAddBook = "1";
const string CommandRemoveBook = "2";
const string CommandShowAllBooks = "3";
const string CommandSearchByTitle = "4";
const string CommandSearchByAuthor = "5";
const string CommandSearchByYearOfRelease = "6";
const string CommandExit = "7";
int indexOfAuthor = 0;
int indexOfTitle = 1;
int indexOfYearOfRelease = 2;
bool isOpen = true;
Archive archive = new Archive();
while (isOpen)
{
Console.WriteLine($"Выберите пункт меню:\n" +
$"\n{1} - Добавить книгу в архив." +
$"\n{2} - Убрать книгу из архива." +
$"\n{3} - Показать все книги из архива." +
$"\n{4} - Поиск книги по названию." +
$"\n{5} - Поиск книги по фамилии автора." +
$"\n{6} - Поиск книги по году выпуска." +
$"\n{7} - Выйти.");
string userInput = Console.ReadLine();
switch (userInput)
{
case CommandAddBook:
Console.Clear();
archive.AddBook();
break;
case CommandRemoveBook:
Console.Clear();
archive.RemoveBook();
break;
case CommandShowAllBooks:
Console.Clear();
archive.ShowAllBook();
break;
case CommandSearchByTitle:
Console.Clear();
archive.SearchByTitle();
archive.KeyClearConsole();
break;
case CommandSearchByAuthor:
Console.Clear();
archive.SearchByAuthor();
archive.KeyClearConsole();
break;
case CommandSearchByYearOfRelease:
Console.Clear();
archive.SearchByYearOfRelease();
archive.KeyClearConsole();
break;
case CommandExit:
isOpen = false;
break;
default:
Console.WriteLine("Такой команды не найдено...");
archive.KeyClearConsole();
break;
}
}
}
}
class Book
{
private string _author;
private string _title;
private string _yearOfRelease;
public Book(string author, string title, string yrarOfRelease)
{
_author = author;
_title = title;
_yearOfRelease = yrarOfRelease;
}
public string[] GetBook()
{
string[] info = new string[3];
info[0] = _author;
info[1] = _title;
info[2] = _yearOfRelease;
return info;
}
}
class Archive
{
private string splitter = "-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_";
private const int indexOfAuthor = 0;
private const int indexOfTitle = 1;
private const int indexOfYearOfRelease = 2;
private List<Book> _books = new List<Book>();
public void SearchByYearOfRelease()
{
Console.Write("Введите год выпуска книги: ");
string userInput = Console.ReadLine();
if (int.TryParse(userInput, out int yearOfRelease))
{
Console.Clear();
Console.WriteLine("Кнги найденны по году выпуска:\n\n" + splitter);
for (int i = 0; i < _books.Count; i++)
{
string[] bookInfo = _books[i].GetBook();
if (bookInfo[indexOfYearOfRelease] == userInput)
{
ShowInfo(i, bookInfo);
}
}
}
else
{
Console.WriteLine("Чтобы ввести год используйте цифры");
}
}
public void SearchByAuthor()
{
Console.Write("Введите фамилию автора: ");
string userInput = Console.ReadLine();
Console.Clear();
Console.WriteLine("Найденные книги по фамилии автора:\n\n" + splitter);
for (int i = 0; i < _books.Count; i++)
{
string[] bookInfo = _books[i].GetBook();
if (bookInfo[indexOfAuthor].ToLower() == userInput.ToLower())
{
ShowInfo(i, bookInfo);
}
}
}
public void SearchByTitle()
{
Console.Write("Введите название книги: ");
string userInput = Console.ReadLine();
Console.Clear();
Console.WriteLine("Найденные книги по названию:\n\n" + splitter);
for (int i = 0; i < _books.Count; i++)
{
string[] bookInfo = _books[i].GetBook();
if (bookInfo[indexOfTitle].ToLower() == userInput.ToLower())
{
ShowInfo(i, bookInfo);
}
}
}
public void ShowInfo(int indexOfBook, string[] bookInfo)
{
Console.WriteLine($"\n Книга {indexOfBook + 1}" +
$"\nАвтор: {bookInfo[indexOfAuthor]}." +
$"\nНазвание: {bookInfo[indexOfTitle]}." +
$"\nГод выпуска: {bookInfo[indexOfYearOfRelease]}.\n");
Console.WriteLine(splitter);
}
public void ShowAllBook()
{
Console.WriteLine("Список всех книг:");
Console.WriteLine(splitter);
for (int i = 0; i < _books.Count; i++)
{
string[] bookInfo = _books[i].GetBook();
ShowInfo(i, bookInfo);
}
KeyClearConsole();
}
public void RemoveBook()
{
Console.Write("Введите номер книги, которую хотите удалить:");
string inputUser = Console.ReadLine();
Console.WriteLine("\n" + splitter);
if (int.TryParse(inputUser, out int numberOfBook) && _books.Count() >= numberOfBook && numberOfBook > 0)
{
_books.RemoveAt(numberOfBook - 1);
Console.Clear();
Console.WriteLine("Книга успешно удалена");
KeyClearConsole();
}
else
{
Console.WriteLine("Вы ввели что-то не так...");
KeyClearConsole();
}
}
public void AddBook()
{
Console.Write("Введите автора книги:");
string author = Console.ReadLine();
Console.Write("\nВведите название книги:");
string title = Console.ReadLine();
Console.Write("\nВведите год выпуска книги(В формате гггг):");
string yearOfRelease = Console.ReadLine();
_books.Add(new Book(author, title, yearOfRelease));
Console.WriteLine("Книга была успешно добавлена в архив...");
KeyClearConsole();
}
public void KeyClearConsole()
{
Console.ReadLine();
Console.Clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment