Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static void Main(string[] args)
- {
- Random random = new Random();
- Library library = new Library(random);
- library.Enter();
- }
- }
- public class Book
- {
- public string Title { get; private set; }
- public string Author { get; private set; }
- public int Year { get; private set; }
- public Book(string title, string author, int year)
- {
- Title = title;
- Author = author;
- Year = year;
- }
- public bool Equals(Book book)
- {
- if(book.Title == Title ||
- book.Author == Author ||
- book.Year == Year)
- return true;
- else
- return false;
- }
- }
- public class Library
- {
- private List<Book> _books;
- private Random _random;
- public Library(Random random)
- {
- _books = new List<Book>();
- _random = random;
- }
- public void Enter()
- {
- bool isChoosed = true;
- int answerShowAllBooks = 1;
- int answerSearchBooks = 2;
- int answerAddBook = 3;
- int answerRemoveBook = 4;
- int answerExit = 5;
- int answer;
- Console.WriteLine("Welcome to the Library");
- while(isChoosed)
- {
- Console.Clear();
- Console.WriteLine("\nWhat you want to do?");
- Console.WriteLine("{0}. Show all books.", answerShowAllBooks);
- Console.WriteLine("{0}. Search books.", answerSearchBooks);
- Console.WriteLine("{0}. Add book.", answerAddBook);
- Console.WriteLine("{0}. Remove book.", answerRemoveBook);
- Console.WriteLine("{0}. Exit.", answerExit);
- bool isDigit = Int32.TryParse(Console.ReadLine(), out answer);
- if(isDigit)
- {
- if(answer == answerShowAllBooks)
- {
- ShowAllBooks();
- Console.ReadLine();
- }
- else if(answer == answerSearchBooks)
- {
- SearchBooks();
- }
- else if(answer == answerAddBook)
- {
- CreateBook();
- }
- else if(answer == answerRemoveBook)
- {
- RemoveBook();
- }
- else if(answer == answerExit)
- {
- isChoosed = false;
- }
- }
- }
- Console.WriteLine("You exit library");
- }
- private void CreateBook()
- {
- bool isChoosed = true;
- int answerCreateRandom = 1;
- int answerCreateManual = 2;
- int answerBack = 3;
- int answer;
- while(isChoosed)
- {
- Console.WriteLine("\nWhat you want to do?");
- Console.WriteLine("{0}. Create book by random.", answerCreateRandom);
- Console.WriteLine("{0}. Create book by manual.", answerCreateManual);
- Console.WriteLine("{0}. Back.", answerBack);
- bool isDigit = Int32.TryParse(Console.ReadLine(), out answer);
- if(isDigit)
- {
- if(answer == answerCreateRandom)
- {
- _books.Add(CreateBookRandom());
- isChoosed = false;
- }
- else if(answer == answerCreateManual)
- {
- _books.Add(CreateBookManual());
- isChoosed = false;
- }
- else if(answer == answerBack)
- {
- isChoosed = false;
- }
- }
- }
- }
- private Book CreateBookRandom()
- {
- string[] titles = {"Thus Spoke Zarathustra", "Moby Dick", "Organon", "Republic"};
- string[] authors = {"Spinoza", "Aristotle", "Plato", "Socrates"};
- int yearStart = 1800;
- int yearEnd = 2022;
- string title = titles[_random.Next(titles.Length)];
- string author = authors[_random.Next(authors.Length)];
- int year = _random.Next(yearStart, yearEnd);
- Book book = new Book(title, author, year);
- return book;
- }
- private Book CreateBookManual()
- {
- bool isInput = true;
- string title = "";
- string author = "";
- int year = 0;
- while(isInput)
- {
- Console.WriteLine("Input a title of book : ");
- title = Console.ReadLine();
- Console.WriteLine("Input an author of book : ");
- author = Console.ReadLine();
- Console.WriteLine("Input a year of book : ");
- bool isDigit = Int32.TryParse(Console.ReadLine(), out year);
- if(isDigit)
- isInput = false;
- }
- Book book = new Book(title, author, year);
- return book;
- }
- private void RemoveBook()
- {
- Book book = CreateBookManual();
- int booksRemoved = 0;
- for (int start = 0; start < _books.Count;)
- {
- if(_books[start].Equals(book))
- {
- ++booksRemoved;
- _books.RemoveAt(start);
- }
- else
- {
- ++start;
- }
- }
- Console.WriteLine("{0} books removed.", booksRemoved);
- Console.ReadLine();
- }
- private void ShowAllBooks()
- {
- Console.WriteLine("\nList of all books : ");
- for(int start = 0; start < _books.Count; start++)
- Console.WriteLine("{0}. {1} {2} {3}",
- start, _books[start].Title, _books[start].Author, _books[start].Year);
- }
- private void SearchBooks()
- {
- Book book = CreateBookManual();
- PrintSearchedBooks(book);
- }
- private void PrintSearchedBooks(Book book)
- {
- Console.WriteLine("\nList of searched books : ");
- for(int start = 0; start < _books.Count; start++)
- if(_books[start].Equals(book))
- Console.WriteLine("{0}. {1} {2} {3}",
- start, _books[start].Title, _books[start].Author, _books[start].Year);
- Console.ReadLine();
- }
- }
Add Comment
Please, Sign In to add comment