Vapio

task16

Jul 28th, 2022 (edited)
1,359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         Random random = new Random();
  8.  
  9.         Library library = new Library(random);
  10.         library.Enter();
  11.     }
  12. }
  13.  
  14. public class Book
  15. {    
  16.     public string Title { get; private set; }
  17.     public string Author { get; private set; }
  18.     public int Year { get; private set; }
  19.  
  20.     public Book(string title, string author, int year)
  21.     {
  22.         Title = title;
  23.         Author = author;
  24.         Year = year;
  25.     }
  26.  
  27.     public bool Equals(Book book)
  28.     {
  29.         if(book.Title == Title ||
  30.            book.Author == Author ||
  31.            book.Year == Year)
  32.             return true;
  33.         else
  34.             return false;
  35.     }
  36. }
  37.  
  38. public class Library
  39. {
  40.     private List<Book> _books;
  41.     private Random _random;
  42.  
  43.     public Library(Random random)
  44.     {
  45.         _books = new List<Book>();
  46.         _random = random;
  47.     }
  48.  
  49.     public void Enter()
  50.     {
  51.         bool isChoosed = true;
  52.    
  53.         int answerShowAllBooks = 1;
  54.         int answerSearchBooks = 2;
  55.         int answerAddBook = 3;
  56.         int answerRemoveBook = 4;
  57.         int answerExit = 5;
  58.  
  59.         int answer;
  60.  
  61.         Console.WriteLine("Welcome to the Library");
  62.  
  63.         while(isChoosed)
  64.         {
  65.             Console.Clear();
  66.             Console.WriteLine("\nWhat you want to do?");
  67.             Console.WriteLine("{0}. Show all books.", answerShowAllBooks);
  68.             Console.WriteLine("{0}. Search books.", answerSearchBooks);
  69.             Console.WriteLine("{0}. Add book.", answerAddBook);
  70.             Console.WriteLine("{0}. Remove book.", answerRemoveBook);
  71.             Console.WriteLine("{0}. Exit.", answerExit);
  72.  
  73.             bool isDigit = Int32.TryParse(Console.ReadLine(), out answer);
  74.            
  75.             if(isDigit)
  76.             {
  77.                 if(answer == answerShowAllBooks)
  78.                 {
  79.                     ShowAllBooks();
  80.                     Console.ReadLine();
  81.                 }
  82.                 else if(answer == answerSearchBooks)
  83.                 {
  84.                     SearchBooks();
  85.                 }
  86.                 else if(answer == answerAddBook)
  87.                 {
  88.                     CreateBook();
  89.                 }
  90.                 else if(answer == answerRemoveBook)
  91.                 {
  92.                     RemoveBook();
  93.                 }
  94.                 else if(answer == answerExit)
  95.                 {
  96.                     isChoosed = false;
  97.                 }
  98.             }
  99.         }
  100.  
  101.         Console.WriteLine("You exit library");
  102.     }
  103.  
  104.     private void CreateBook()
  105.     {
  106.         bool isChoosed = true;
  107.  
  108.         int answerCreateRandom = 1;
  109.         int answerCreateManual = 2;
  110.         int answerBack = 3;
  111.  
  112.         int answer;
  113.  
  114.         while(isChoosed)
  115.         {
  116.             Console.WriteLine("\nWhat you want to do?");
  117.             Console.WriteLine("{0}. Create book by random.", answerCreateRandom);
  118.             Console.WriteLine("{0}. Create book by manual.", answerCreateManual);
  119.             Console.WriteLine("{0}. Back.", answerBack);
  120.  
  121.             bool isDigit = Int32.TryParse(Console.ReadLine(), out answer);
  122.  
  123.             if(isDigit)
  124.             {
  125.                 if(answer == answerCreateRandom)
  126.                 {
  127.                     _books.Add(CreateBookRandom());
  128.                     isChoosed = false;
  129.                 }
  130.                 else if(answer == answerCreateManual)
  131.                 {
  132.                     _books.Add(CreateBookManual());
  133.                     isChoosed = false;
  134.                 }
  135.                 else if(answer == answerBack)
  136.                 {
  137.                     isChoosed = false;
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     private Book CreateBookRandom()
  144.     {
  145.         string[] titles = {"Thus Spoke Zarathustra", "Moby Dick", "Organon", "Republic"};
  146.         string[] authors = {"Spinoza", "Aristotle", "Plato", "Socrates"};
  147.         int yearStart = 1800;
  148.         int yearEnd = 2022;
  149.  
  150.         string title = titles[_random.Next(titles.Length)];
  151.         string author = authors[_random.Next(authors.Length)];
  152.         int year = _random.Next(yearStart, yearEnd);
  153.  
  154.         Book book = new Book(title, author, year);
  155.         return book;
  156.     }
  157.  
  158.     private Book CreateBookManual()
  159.     {
  160.         bool isInput = true;
  161.  
  162.         string title = "";
  163.         string author = "";
  164.         int year = 0;
  165.  
  166.         while(isInput)
  167.         {
  168.             Console.WriteLine("Input a title of book : ");
  169.             title = Console.ReadLine();
  170.  
  171.             Console.WriteLine("Input an author of book : ");
  172.             author = Console.ReadLine();
  173.  
  174.             Console.WriteLine("Input a year of book : ");
  175.             bool isDigit = Int32.TryParse(Console.ReadLine(), out year);
  176.  
  177.             if(isDigit)
  178.                 isInput = false;
  179.         }
  180.        
  181.         Book book = new Book(title, author, year);
  182.         return book;
  183.     }
  184.  
  185.     private void RemoveBook()
  186.     {
  187.         Book book = CreateBookManual();
  188.         int booksRemoved = 0;
  189.  
  190.         for (int start = 0; start < _books.Count;)
  191.         {
  192.             if(_books[start].Equals(book))
  193.             {
  194.                 ++booksRemoved;
  195.                 _books.RemoveAt(start);
  196.             }  
  197.             else
  198.             {
  199.                 ++start;
  200.             }    
  201.         }
  202.  
  203.         Console.WriteLine("{0} books removed.", booksRemoved);
  204.         Console.ReadLine();
  205.     }
  206.  
  207.     private void ShowAllBooks()
  208.     {
  209.         Console.WriteLine("\nList of all books : ");    
  210.  
  211.         for(int start = 0; start < _books.Count; start++)
  212.             Console.WriteLine("{0}. {1} {2} {3}",
  213.                               start, _books[start].Title, _books[start].Author, _books[start].Year);
  214.     }
  215.  
  216.     private void SearchBooks()
  217.     {
  218.         Book book = CreateBookManual();
  219.         PrintSearchedBooks(book);
  220.     }
  221.  
  222.     private void PrintSearchedBooks(Book book)
  223.     {
  224.         Console.WriteLine("\nList of searched books : ");
  225.  
  226.         for(int start = 0; start < _books.Count; start++)
  227.             if(_books[start].Equals(book))
  228.                 Console.WriteLine("{0}. {1} {2} {3}",
  229.                               start, _books[start].Title, _books[start].Author, _books[start].Year);
  230.  
  231.         Console.ReadLine();
  232.     }
  233. }
  234.  
Add Comment
Please, Sign In to add comment