. Write a program that performs the following tasks: a) declares an array of 5 books, prompt the user for the details of the book and load them in the declared array. b) The application should allow the user to find and display all the book(s) published by the same author. c) The user should be able to find the number of copies available for the same book. E.g Absolute Java, 7th Edition by Walter Savitch. You want to know, how many copies does this book has? In the array.
internal class Program
{
class Book
{
public string Title { get; set; }
public string Author { get; set; }
public Book(string title, string authorEdition)
{
Title = title;
Author = authorEdition;
}
public override string ToString()
{
return $"Title: {Title}, Author: {Author}";
}
}
static void Main(string[] args)
{
Book[] books = new Book[5];
SetBooks(books);
Console.WriteLine("Enter the author of the book you want to find ");
FindBooksSameAuthor(books, Console.ReadLine());
Console.WriteLine("Enter the author of the book you want to find ");
string author = Console.ReadLine();
Console.WriteLine("Enter the title of the book you want to find");
string title = Console.ReadLine();
FindTheSameBooks(books, new Book(title, author));
Console.ReadKey();
}
static void FindTheSameBooks(Book[] books, Book seachBook)
{
int i = 0;
foreach (var book in books)
{
if (book.Author == seachBook.Author && book.Title == seachBook.Title)
i++;
}
Console.WriteLine($"Such books:\n {seachBook}. Such a quantity {i}");
}
static void FindBooksSameAuthor(Book[] books, string author)
{
for (int i = 0; i < books.Length; i++)
{
if (books[i].Author == author)
Console.WriteLine(books[i]);
}
}
static void SetBooks(Book[] books)
{
for (int i = 0; i < books.Length; i++)
{
Console.WriteLine("Enter Title");
string title = Console.ReadLine();
Console.WriteLine("Enter Author");
string author = Console.ReadLine();
books[i] = new Book(title, author);
}
}
}
Comments
Leave a comment