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< Book> 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< Book> GetBooks()
This method should return the collection of books.

public List< string> 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< string> 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< string> 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.

There are several methods that you might find helpful for tackling these methods. In particular Union (https://msdn.microsoft.com/en-us/library/bb341731(v=vs.110).aspx), and Intersect (https://msdn.microsoft.com/en-us/library/bb460136(v=vs.110).aspx) may be helpful. You do not need to understand the IEnumerable or < TSource> parts of the documentation at this point in the semester, just that these methods will work on Lists (or most other collections). For example, Union can be used like this - List< string> unionList = ListA.Union(ListB).ToList() assuming ListA and ListB are already lists of strings.

A simple Main() function is provided to assist with testing your implementation of the class. This method will not be called by the AMS, and the test cases in the AMS may be different to the provided tests in Main. Your BookCollection class will be instantiated and its methods called by the AMS in order to test it.

Sample code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookFinder {
/// < summary>
/// Simple representation of a book with its title and topics.
/// - Do not change this code -
/// < /summary>
public class Book {
public string Title { get; set; }
private List< string> topics;
/// < summary>
/// Constructs a book object with the provided title and topics.
/// < /summary>
/// < param name="title">String representing the title of the book.< /param>
/// < param name="topics">List of significant topics in the book< /param>
public Book(string title, List< string> topics) {
Title = title;
this.topics = topics;
}
/// < summary>
/// Returns whether the book contains the provided topic.
/// < /summary>
/// < param name="topic">Topic to check for< /param>
/// < returns>True if the book contains the topic, false otherwise< /returns>
public bool ContainsTopic(string topic) {
return topics.Contains(topic);
}
}
public class BookCollection {
private List< Book> books;
/// Implement your BookCollection class here
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.