Question

In: Computer Science

Write a OOP class called BookList that uses a Book class and allows multiple books to...

Write a OOP class called BookList that uses a Book class and allows multiple books to be entered into a BookList object. Include normally useful methods and boolean addBook(Book b), Book searchBook(String title, String author). addBook returns true if b is successfully added to the list. searchBook returns a book matching either the title or author from the current list. You do not need to write Book (assume there are suitable constructors and methods) or any test code (main). You can write them if you wish. Use Java or other language if you wish; use any IDE or compiler.

Solutions

Expert Solution

//C++ programming

#include<iostream>
using namespace std;

class Book{
   private:
       string title;
       string author;
       Book *next;
   public:
       Book(){
           title = "";
           author = "";
           next = NULL;
       }
       Book(string t , string a){
           title = t;
           author = a;
           next = NULL;
       }
       void setTitle(string t){
           title= t;
       }
       void setAuthor(string a){
           author = a;
       }
       void setNext(Book*n){
           next = n;
       }
       string getTitle(){
           return title;
       }
       string getAuthor(){
           return author;
       }
       Book* getNext(){
           return next;
       }
       ~Book(){
          
       }
};

class BookList{
   private:
       Book *head;
  
   public:
       BookList(){
           head = NULL;
       }
       bool addBook(Book *b){
           if(head==NULL){
               head = b;
               return true;
           }
           Book *current = head;
           while(current->getNext()!=NULL)current = current->getNext();
          
           current->setNext(b);
           return true;
       }
      
       Book* searchBook(string title, string author){
           if(head==NULL)return NULL;
           Book *current = head;
          
           while(current){
               if(current->getTitle()==title || current->getAuthor()==author)return current;
               current = current->getNext();
           }
           return NULL;
       }
};

int main(){
   BookList list;
   string title,author;
   int choice;
   do{
       cout<<"1:Add Book\n2:Search Book\n3:Quit\n";
       cin>>choice;
       switch(choice){
           case 1:{
               cout<<"Enter title : ";
               cin.get();
               getline(cin,title);
               cout<<"Enter author : ";
               getline(cin,author);
              
               Book*b = new Book(title,author);
               if(list.addBook(b))cout<<"Book Added\n";
               else cout<<"Book not added\n";
               break;
           }
           case 2:{
               cout<<"Enter title : ";
               cin.get();
               getline(cin,title);
               cout<<"Enter author : ";
               getline(cin,author);
              
               Book*b = list.searchBook(title,author);
               if(!b)cout<<"Book not found\n";
               else cout<<"Book found\n";
               break;
           }
           case 3:{
               cout<<"\nThank you\n";
               break;
           }
           default:{
               cout<<"Invalid Input\n";
               break;
           }
       }
   }while(choice!=3);
   return 0;
}

//sample output


Related Solutions

Write a class called CompoundManager that allows you to save all compounds in memory to a...
Write a class called CompoundManager that allows you to save all compounds in memory to a text or binary file and then read a list of compounds from a text or binary file and append these to those in memory, those that don’t already exist. In other words, if Ammonia is in memory and the system reads a file that contains Ammonia, then it is ignored, and it moves on to the next compound in the file. Please write the...
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
1. PHP OOP Create a complete PHP enabled website that defines and uses a PineApple class...
1. PHP OOP Create a complete PHP enabled website that defines and uses a PineApple class in PHP. The class has following members. Properties $color: string type $taste: string type $weight: number type Standard constructor, with 3 parameters Member functions eat(): no return, no parameter, print a short paragraph in English to describe how to eat a pineapple. grow(): no return, no parameter, print a short paragraph in English to describe how to grow a pineapple plant. display(): no return,...
By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type...
Write a small Java class called StoreAddStuff that uses generics to store two variables of type T passed in during construction, then returns their sum through a non-static method.
In this homework you will implement a Library class that uses your Book and Person class...
In this homework you will implement a Library class that uses your Book and Person class from homework 2, with slight modifications. The Library class will keep track of people with membership and the books that they have checked out. Book.java You will need to modify your Book.java from homework 2 in the following ways: field: dueDate (private)             A String containing the date book is due.  Dates are given in the format "DD MM YYYY", such as "01 02 2017"...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input...
Problem Description: SavingsAccount Develop a C++ class declaration called SavingsAccount class that allows user to input initial values of dollars and cents and then asks for deposits and withdrawals. The class should include the following information: Operations (Member Functions) 1. Default constructor that sets both dollars and cents to 0. 2. The constructor has 2 parameters that set dollars and cents to the indicated values. 3. Open account (with an initial deposit). This is called to put initial values in...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
1- Write a class called MedicalStaff that is a Person (the class that you wrote in...
1- Write a class called MedicalStaff that is a Person (the class that you wrote in last lab). A MedicalStaff has specialty (i.e. Orthopedic, cardiology, etc.). 2- Then write two classes: Doctor class has office visit fee. Nurse class has title (i.e. RN, NP, etc.) Both classes inherit from MedicalStaff. Be sure these are all complete classes, including toString method. 3- Write a tester to test these classes and their methods, by creating an array or ArrayList of Person and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT