Last active
May 13, 2025 11:48
-
-
Save VladislavSavich/77980fa4aa30d05a9c3aff7b46577364 to your computer and use it in GitHub Desktop.
C# Junior Хранилище книг
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace CSLight | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Bookcase bookcase = new Bookcase(); | |
bookcase.Work(); | |
} | |
} | |
class Bookcase | |
{ | |
private List<Book> _books = new List<Book>(); | |
public Bookcase() | |
{ | |
_books = new List<Book>() { new Book("Мы", "Е. Замятин", 1920), new Book("Гроза", "А. Н. Островский", 1859), new Book("Гамлет", "У. Шекспир", 1601) }; | |
} | |
public void Work() | |
{ | |
const string CommandAddBook = "1"; | |
const string CommandDeleteBook = "2"; | |
const string CommandShowBooks = "3"; | |
const string CommandNameSearch = "4"; | |
const string CommandAuthorSearch = "5"; | |
const string CommandYearSearch = "6"; | |
const string CommandExit = "Exit"; | |
string userInput; | |
bool isWork = true; | |
while (isWork) | |
{ | |
Console.WriteLine("-КНИЖНЫЙ ШКАФ-"); | |
Console.WriteLine($"{CommandAddBook} - Положить книгу"); | |
Console.WriteLine($"{CommandDeleteBook} - Убрать книгу"); | |
Console.WriteLine($"{CommandShowBooks} - Посмотреть все книги"); | |
Console.WriteLine($"{CommandNameSearch} - Найти книгу по названию"); | |
Console.WriteLine($"{CommandAuthorSearch} - Найти книгу по автору"); | |
Console.WriteLine($"{CommandYearSearch} - Найти книгу по году выпуска"); | |
Console.WriteLine($"{CommandExit} - выход"); | |
userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case CommandAddBook: | |
AddBook(); | |
break; | |
case CommandDeleteBook: | |
DeleteBook(); | |
break; | |
case CommandShowBooks: | |
ShowBooks(_books); | |
break; | |
case CommandNameSearch: | |
FindBookByName(); | |
break; | |
case CommandAuthorSearch: | |
FindBookByAuthor(); | |
break; | |
case CommandYearSearch: | |
FindBookByYear(); | |
break; | |
case CommandExit: | |
isWork = false; | |
break; | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void AddBook() | |
{ | |
string inputName; | |
string inputAuthor; | |
int inputYear; | |
Console.WriteLine("Введите название книги: "); | |
inputName = Console.ReadLine(); | |
Console.WriteLine("Введите автора книги: "); | |
inputAuthor = Console.ReadLine(); | |
Console.WriteLine("Введите год выпуска "); | |
inputYear = ReadInt(); | |
_books.Add(new Book(inputName, inputAuthor, inputYear)); | |
} | |
private void DeleteBook() | |
{ | |
int userInput; | |
ShowBooks(_books); | |
Console.WriteLine("Введите номер книги, которую хотите удалить:"); | |
userInput = ReadInt(); | |
if (userInput > 0 && userInput <= _books.Count) | |
{ | |
userInput--; | |
_books.RemoveAt(userInput); | |
} | |
else | |
{ | |
Console.WriteLine("Книги с таким номером нет в вашей коллекции!"); | |
} | |
} | |
private void FindBookByName() | |
{ | |
List<Book> booksFound = new List<Book>(); | |
string inputName; | |
Console.WriteLine("Введите название книги которую хотитие найти: "); | |
inputName = Console.ReadLine(); | |
foreach (var book in _books) | |
{ | |
if (book.Name.ToLower() == inputName.ToLower()) | |
booksFound.Add(book); | |
} | |
if (booksFound.Count > 0) | |
ShowBooks(booksFound); | |
else | |
Console.WriteLine("Не найдено ни одной книги с таким названием"); | |
} | |
private void FindBookByAuthor() | |
{ | |
List<Book> booksFound = new List<Book>(); | |
string inputAuthor; | |
Console.WriteLine("Введите имя автора книги которую хотитие найти: "); | |
inputAuthor = Console.ReadLine(); | |
foreach (var book in _books) | |
{ | |
if (book.Author.ToLower() == inputAuthor.ToLower()) | |
booksFound.Add(book); | |
} | |
if (booksFound.Count > 0) | |
ShowBooks(booksFound); | |
else | |
Console.WriteLine("Не найдено ни одной книги с таким автором"); | |
} | |
private void FindBookByYear() | |
{ | |
List<Book> booksFound = new List<Book>(); | |
int inputYear; | |
Console.WriteLine("Введите год выпуска книги которую хотитие найти: "); | |
inputYear = ReadInt(); | |
foreach (var book in _books) | |
{ | |
if (book.YearOfRelease == inputYear) | |
booksFound.Add(book); | |
} | |
if (booksFound.Count > 0) | |
ShowBooks(booksFound); | |
else | |
Console.WriteLine("Не найдено ни одной книги с таким годом выпуска"); | |
} | |
private void ShowBooks(List<Book> books) | |
{ | |
int booksCounter = 1; | |
Console.WriteLine("Список книг:"); | |
for (int i = 0; i < books.Count; i++) | |
{ | |
Console.Write($"{booksCounter}. "); | |
books[i].ShowInfo(); | |
booksCounter++; | |
} | |
} | |
private int ReadInt() | |
{ | |
int number; | |
while (int.TryParse(Console.ReadLine(), out number) == false) | |
{ | |
Console.WriteLine("Некорректный ввод! Попробуйте снова!"); | |
} | |
return number; | |
} | |
} | |
class Book | |
{ | |
public Book(string name, string author, int year) | |
{ | |
Name = name; | |
Author = author; | |
YearOfRelease = year; | |
} | |
public string Name { get; private set; } | |
public string Author { get; private set; } | |
public int YearOfRelease { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Название: {Name}, Автор: {Author} - {YearOfRelease} год."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment