Last active
November 14, 2023 07:50
-
-
Save talatari/3da1d199ba0f8c87b3271fa5cb33300f to your computer and use it in GitHub Desktop.
42. Task: Book Storage
This file contains 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
public class Program | |
{ | |
static void Main() | |
{ | |
BookStorage bookStorage = new BookStorage(); | |
bookStorage.Work(); | |
} | |
} | |
public class Book | |
{ | |
public Book(int id, string name, string writerName, int yearRelease, int amounPages) | |
{ | |
Id = id; | |
Name = name; | |
WriterName = writerName; | |
YearRelease = yearRelease; | |
AmountPages = amounPages; | |
} | |
public int Id { get; private set; } | |
public string Name { get; private set; } | |
public string WriterName { get; private set; } | |
public int YearRelease { get; private set; } | |
public int AmountPages { get; private set; } | |
} | |
public class BookStorage | |
{ | |
private List<Book> _books = new List<Book>(); | |
private int _lastId = 0; | |
public void Work() | |
{ | |
const string AddBookCommand = "1"; | |
const string RemoveBookCommand = "2"; | |
const string ShowAllBooksCommand = "3"; | |
const string ShowBooksAtValueCommand = "4"; | |
const string ExitCommand = "5"; | |
bool isWork = true; | |
while (isWork == true) | |
{ | |
Console.Clear(); | |
Console.WriteLine( | |
$"Menu: \n" + | |
$" {AddBookCommand} - Add Book\n" + | |
$" {RemoveBookCommand} - Remove Book\n" + | |
$" {ShowAllBooksCommand} - Show All Books\n" + | |
$" {ShowBooksAtValueCommand} - Show All Books At Value\n" + | |
$" {ExitCommand} - Exit\n"); | |
Console.Write("Enter command: "); | |
string userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case AddBookCommand: | |
AddBook(); | |
break; | |
case RemoveBookCommand: | |
RemoveBook(); | |
break; | |
case ShowAllBooksCommand: | |
ShowAll(_books); | |
break; | |
case ShowBooksAtValueCommand: | |
ShowBooksAtValue(); | |
break; | |
case ExitCommand: | |
isWork = false; | |
break; | |
} | |
} | |
} | |
private void AddBook() | |
{ | |
Console.Write("\nEnter book name: "); | |
string name = Console.ReadLine(); | |
Console.Write("Enter writer book name: "); | |
string writerName = Console.ReadLine(); | |
Console.Write("Enter year release: "); | |
Int32.TryParse(Console.ReadLine(), out int yearRelease); | |
Console.Write("Enter amont book pages: "); | |
Int32.TryParse(Console.ReadLine(), out int amountPages); | |
_lastId++; | |
_books.Add(new Book(_lastId, name, writerName, yearRelease, amountPages)); | |
} | |
private void RemoveBook() | |
{ | |
Console.Write("\nEnter book id for remove: "); | |
if (Int32.TryParse(Console.ReadLine(), out int bookId)) | |
foreach (var _book in _books.ToList()) | |
if (_book.Id == bookId) | |
{ | |
_books.Remove(_book); | |
break; | |
} | |
ShowAll(_books); | |
} | |
private void ShowAll(List<Book> books) | |
{ | |
if (books.Count > 0) | |
{ | |
Console.Write("\n[All books in storage]\n"); | |
foreach (var book in books) | |
{ | |
Console.WriteLine( | |
$"\nBook id: \t\t{book.Id}\n" + | |
$"Book name: \t\t{book.Name}\n" + | |
$"Writer book name: \t{book.WriterName}\n" + | |
$"Year release: \t\t{book.YearRelease}\n" + | |
$"Amount book pages: \t{book.AmountPages}\n"); | |
} | |
} | |
else | |
{ | |
Console.Write("\nBooks not found is storage.\n"); | |
} | |
Console.ReadKey(); | |
} | |
private void ShowBooksAtValue() | |
{ | |
const string ShowOnNameCommand = "1"; | |
const string ShowOnWriterCommand = "2"; | |
const string ShowOnYearReleaseCommand = "3"; | |
const string ShowOnAmountPagesCommand = "4"; | |
const string ExitCommand = "5"; | |
bool isShow = true; | |
while (isShow == true) | |
{ | |
Console.Clear(); | |
Console.WriteLine( | |
$"Select value to show books: \n" + | |
$" {ShowOnNameCommand} - Show book on name\n" + | |
$" {ShowOnWriterCommand} - Show book on writer\n" + | |
$" {ShowOnYearReleaseCommand} - Show book on year release\n" + | |
$" {ShowOnAmountPagesCommand} - Show book on amount pages\n" + | |
$" {ExitCommand} - Exit from search\n"); | |
Console.Write("Enter command: "); | |
string userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case ShowOnNameCommand: | |
ShowOnName(); | |
break; | |
case ShowOnWriterCommand: | |
ShowOnWriter(); | |
break; | |
case ShowOnYearReleaseCommand: | |
ShowOnYearRelease(); | |
break; | |
case ShowOnAmountPagesCommand: | |
ShowOnAmountPages(); | |
break; | |
case ExitCommand: | |
isShow = false; | |
break; | |
} | |
} | |
} | |
private void ShowOnName() | |
{ | |
Console.Write("Enter book name: "); | |
string userInput = Console.ReadLine().ToLower(); | |
List<Book> searchedBooks = new List<Book>(); | |
foreach (var _book in _books) | |
if (_book.Name.ToLower().IndexOf(userInput) >= 0) | |
searchedBooks.Add(_book); | |
if (searchedBooks.Count >= 0) | |
ShowAll(searchedBooks); | |
} | |
private void ShowOnWriter() | |
{ | |
Console.Write("Enter book writer: "); | |
string userInput = Console.ReadLine().ToLower(); | |
List<Book> searchedBooks = new List<Book>(); | |
foreach (var _book in _books) | |
if (_book.WriterName.ToLower().IndexOf(userInput) >= 0) | |
searchedBooks.Add(_book); | |
if (searchedBooks.Count >= 0) | |
ShowAll(searchedBooks); | |
} | |
private void ShowOnYearRelease() | |
{ | |
Console.Write("Enter book > year release: "); | |
string userInput = Console.ReadLine(); | |
if (Int32.TryParse(userInput, out Int32 userInputYearRelease)) | |
{ | |
List<Book> searchedBooks = new List<Book>(); | |
foreach (var _book in _books) | |
if (_book.YearRelease >= userInputYearRelease) | |
searchedBooks.Add(_book); | |
if (searchedBooks.Count >= 0) | |
ShowAll(searchedBooks); | |
} | |
} | |
private void ShowOnAmountPages() | |
{ | |
Console.Write("Enter book > amount pages: "); | |
string userInput = Console.ReadLine(); | |
if (Int32.TryParse(userInput, out int userInputAmountPages)) | |
{ | |
List<Book> searchedBooks = new List<Book>(); | |
foreach (var _book in _books) | |
if (_book.AmountPages >= userInputAmountPages) | |
searchedBooks.Add(_book); | |
if (searchedBooks.Count >= 0) | |
ShowAll(searchedBooks); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment