Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this task you have been provided with a simple 'Book' class, which represents information about a book including its title and topics. Your task

For this task you have been provided with a simple 'Book' class, which represents information about a book including its title and topics. Your task in this exercise is to create a 'BookCollection' class that represents a collection of books and allows the collection to be searched based on the topics of the books.

The code for the 'Book' class is provided as part of the sample code. You should not touch this code; instead, write your own 'BookCollection' class according to the specification below.

The 'BookCollection' class must have 2 constructors:

public BookCollection()

This constructor creates an empty BookCollection.

public BookCollection(List books)

This constructor creates a collection of books with the provided list of books included.

The class must also have several other methods which allow for adding books and finding books with topics:

public void AddBook(Book book)

This method should add a book to the collection. Note - usually this method would check whether the book was already in the collection, however this method has been simplified for this exercise.

public List GetBooks()

This method should return the collection of books.

public List BooksWithTopic(string topic)

This method should find books which contain the topic topic. It should return a list of the titles of the books which contain the topic. It is recommended that you implement this method before the following two.

public List BooksWithAnyTopic(string[] topics)

This method should find books which contain any of the topics in the list topics. For example, if the list of topics is ["fantasy", "science"] it should return any book with the topic fantasy OR science. It should return a list of the titles of the books which contain any of the topics. You may want to make use of the BooksWithTopic method here.

public List BooksWithAllTopics(string[] topics)

This method should find books which contain all of the topics in the list topics. For example, if the list of topics is ["fantasy", "science"] it should return any book with the topic fantasy AND science. It should return a list of the titles of the books which contain all of the topics. You may want to make use of the BooksWithTopic method here.

Sample Code:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace BookFinder { ///

/// Simple representation of a book with its title and topics. /// - Do not change this code - /// public class Book { public string Title { get; set; } private List topics;

///

/// Constructs a book object with the provided title and topics. /// /// String representing the title of the book. /// List of significant topics in the book public Book(string title, List topics) { Title = title; this.topics = topics; }

///

/// Returns whether the book contains the provided topic. /// /// Topic to check for /// True if the book contains the topic, false otherwise public bool ContainsTopic(string topic) { return topics.Contains(topic); } }

public class BookCollection { private List books;

/// Implement your BookCollection class here

}

///

/// Simple tester for the program. /// class Program { static void Main(string[] args) { // Create list of books for the collection List books = new List() { new Book("Game of Thrones", new List() { "fantasy", "dragons", "drama", "fiction", "king", "queen", "medieval", "epic"}), new Book("The Fellowship of the Ring", new List() {"fantasy", "fiction", "epic", "wizard", "elves"}), new Book("Small Gods", new List() {"fantasy", "fiction", "religion", "philosophy", "satire"}), new Book("The Gene: An Intimate History", new List() {"non-fiction", "science", "genetics", "medicine" }), new Book("Sapiens: A brief History of Humankind", new List() {"non-fiction", "anthropology", "evolution", "history"}), new Book("The Martian", new List() {"fiction", "science", "science fiction", "space" }) }; BookCollection library = new BookCollection(books);

// Some simple test cases are included here - you may want to test more. // The AMS will not use the same test cases. // Check for single topic: string topic = "fantasy"; List results = library.BooksWithTopic(topic); // Display results

// Check for any of several topics string[] topics = new string[] { "science", "fiction" }; results = library.BooksWithAnyTopic(topics); // Display results

// Check for all of several topics results = library.BooksWithAllTopics(topics); // Display results

// Exit Console.WriteLine(" Press enter to exit."); Console.ReadLine(); }

// You might want to write some simple helper methods for displaying lists here // (Remember DRY [Don't Repeat Yourself]- if you're writing the same chunk of code // over and over, it should probably be a method!)

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions