Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- private static List<Book> _books = new List<Book>();
- static void Main(string[] args)
- {
- ShowMenu();
- }
- static void ShowMenu()
- {
- string userInput;
- do
- {
- Console.WriteLine("Menu: 1 - add book, 2 - show books, 3 - delete book, 4 - find by parameter, 5 - exit");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- AddBook();
- break;
- case "2":
- ShowAllBooks();
- break;
- case "3":
- DeleteBookByIndex();
- break;
- case "4":
- FindBooksByParameter();
- break;
- default:
- Console.WriteLine("Invalid input");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- while (userInput != "5");
- }
- static void AddBook()
- {
- string name;
- string author;
- string yearToConvert;
- int year;
- bool isNumber;
- Console.WriteLine("Input name:");
- name = Console.ReadLine();
- Console.WriteLine("Input author:");
- author = Console.ReadLine();
- do
- {
- Console.WriteLine("Input year:");
- yearToConvert = Console.ReadLine();
- isNumber = int.TryParse(yearToConvert, out year);
- }
- while (isNumber == false);
- Book book = new Book(name, author, year);
- _books.Add(book);
- }
- static void ShowAllBooks()
- {
- for (int i = 0; i < _books.Count; i++)
- {
- Console.Write($"{i + 1}. ");
- _books[i].ShowInfo();
- }
- }
- static void DeleteBookByIndex()
- {
- ShowAllBooks();
- int index;
- bool isNumber;
- do
- {
- Console.WriteLine("Input index to delete:");
- string indexToConvert = Console.ReadLine();
- isNumber = int.TryParse(indexToConvert, out index);
- }
- while (isNumber == false);
- if(index < 0 && index >= _books.Count)
- {
- Console.WriteLine("Invalid index");
- }
- else
- {
- _books.RemoveAt(index);
- Console.WriteLine($"Book whith index {index} deleted");
- }
- }
- static void FindBooksByParameter()
- {
- Console.WriteLine("Choose parameter: 1 - name, 2 - author, 3 - year");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "1":
- FindByName();
- break;
- case "2":
- FindByAuthor();
- break;
- case "3":
- FindByYear();
- break;
- default:
- Console.WriteLine("Invalid input");
- break;
- }
- }
- static void FindByName()
- {
- Console.WriteLine("Input name: ");
- string name = Console.ReadLine();
- foreach (var book in _books)
- {
- if (book.Name == name)
- {
- book.ShowInfo();
- }
- }
- }
- static void FindByAuthor()
- {
- Console.WriteLine("Input author: ");
- string author = Console.ReadLine();
- foreach (var book in _books)
- {
- if (book.Author == author)
- {
- book.ShowInfo();
- }
- }
- }
- static void FindByYear()
- {
- bool isNumber;
- int year;
- do
- {
- Console.WriteLine("Input year: ");
- string inputToConvert = Console.ReadLine();
- isNumber = int.TryParse(inputToConvert, out year);
- }
- while (isNumber == false);
- foreach (var book in _books)
- {
- if (book.YearOfPublication == year)
- {
- book.ShowInfo();
- }
- }
- }
- }
- class Book
- {
- public string Name { get; private set; }
- public string Author { get; private set; }
- public int YearOfPublication { get; private set; }
- public Book(string name, string author, int year)
- {
- Name = name;
- Author = author;
- YearOfPublication = year;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Name - {Name}, Author - {Author}, Year - {YearOfPublication}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement