Problem Statement: Write a program that maintains a list of books in stock for W
ID: 3692177 • Letter: P
Question
Problem Statement: Write a program that maintains a list of books in stock for Wildcat bookstore. The program should repeatedly print a menu of options to allow a user to select from the following operations, until the user decides to exit.
• Insert a book record • Delete a book record • Print the book list • Search the book list • Quit
Each book record in the list consists of book title (string), number of copies in stock (int), and price (double). The book list should be maintained in the lexicographical order (also called dictionary order) of the book titles. Option 1 should read a book record (title, quantity, and price) from the user and insert the record into the current book list. Option 2 should ask the user for a book title and delete the record that exactly matches the title from the book list. If no such book exists, then report the book is not in stock and do nothing. Option 3 should display all the books in stock, in the lexicographical order of the book titles. Option 4 should allow the user to type a search key (a string), then search all the books in stock and display all matched books which contain the key as the substring of the book titles. For example, if the search key is “Computer”, the book title of “Introduction to Computer Science” matches, but the book title of “Great Principles of Computing” does not match. If one or more matched book titles have been found, display the book records in lexicographical order otherwise, report no such book is in stock. Option 5 should quit the program. To make it simple, for this program, assume the book title is case sensitive. (e.g., “computer networks” and “Computer networks” are considered as two different books)
Complete the class definition of the following two classes Book and BookList and use them in your program. Note you can add other member functions to the class Book and/or the class BookList, if you need them for your program, but you should not remove or modify, either data members or member functions, from the following declarations. class Book {
public: Book(); // default constructor
Book(string bt, int bq, double bp); // constructor
private: string title; // book title
int quantity; // number of copies in stock
double price; // book price
// Assign friendship to class BookList // so that BookList object can directly access private data of Book
friend class BookList; // Assign friendship to class BookList
};
class BookList
{ public: BookList(); // Constructor
~BookList(); // Destructor
// Insert a new book record into the book list. The book has title bt, number of copies in stock bq and the price bp. Note that the vector of books should be maintained in the lexicographical order of the book titles. If the book title already exists in the list, then upgrade the quantity by adding bq to current quantity and upgrade the price with bp
void insert(string bt, int bq, double bp);
// Search for matched book titles with the given key. The book title which contains key as substring matches. If a book is found, print its title, quantity and price; If more than one, display them in lexicographical order otherwise report No Matched Book is in stock
void search(string key);
// Delete a book record with title exactly matching t, If the book is not in stock, report it then do nothing.
void remove(string t);
// First display totally how many books in stock; then display the book records in stock in the lexicographical order of book titles
void print();
private: vector<Book> BL; // a vector to store Book objects in stock
int count; // total number of books in stock
};
I have a Main cpp file, a Book cpp and header file, and a BookList cpp and header file. I am struggling with inserting the information into book, and then adding that to the larger BookList. Any help is much appriciated.
Explanation / Answer
//Wildcat_BookStore.cpp
#include <iostream>
#include "Book.cpp"
using namespace std;
int main()
{
int choice;
BookList booklist;
string title;
int quantity;
double price;
string key;
while(true){
cout<<endl<<"MENU";
cout<<endl<<"1. Insert a book record";
cout<<endl<<"2. Delete a book record";
cout<<endl<<"3. Print the book list";
cout<<endl<<"4. Search the book list";
cout<<endl<<"5. Quit";
cout<<endl<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<endl<<"Enter the details of book to be
inserted: ";
cout<<endl<<"Title: ";
cin>>title;
cout<<endl<<"Quantity: ";
cin>>quantity;
cout<<endl<<"Price: ";
cin>>price;
booklist.insertBook(title,quantity,price);
break;
case 2:
cout<<endl<<"Enter the title of the book to be
removed: ";
cin>>title;
booklist.remove(title);
break;
case 3: booklist.print();
break;
case 4:
cout<<endl<<"Enter the keyword to search: ";
cin>>key;
booklist.search(key);
case 5: exit(0);
}
}
return 0;
}
//Book.h
#include <iostream>
#include <vector>
using namespace std;
class Book {
public:
Book(); // default constructor
Book(string , int , double ); // constructor
private: string title; // book title
int quantity; // number of copies in stock
double price; // book price
// Assign friendship to class BookList // so that BookList object can
directly access private data of Book
friend class BookList; // Assign friendship to class BookList
};
Book::Book(string bt, int bq, double bp)
{
title=bt;
quantity=bq;
price=bp;
}
class BookList
{ public: BookList(); // Constructor
~BookList(); // Destructor
// Insert a new book record into the book list. The book has title bt,
number of copies in stock bq and the price bp. Note that the vector of
books should be maintained in the lexicographical order of the book
titles. If the book title already exists in the list, then upgrade the
quantity by adding bq to current quantity and upgrade the price with bp
void insertBook(string bt, int bq, double bp);
// Search for matched book titles with the given key. The book title
which contains key as substring matches. If a book is found, print its
title, quantity and price; If more than one, display them in
lexicographical order otherwise report No Matched Book is in stock
void search(string key);
// Delete a book record with title exactly matching t, If the book is not
in stock, report it then do nothing.
void remove(string t);
// First display totally how many books in stock; then display the book
records in stock in the lexicographical order of book titles
void print();
private: vector<Book> BL; // a vector to store Book objects in stock
int count; // total number of books in stock
};
//Book.cpp
#include "Book.h"
using namespace std;
BookList::BookList() // Constructor
{
count=0;
}
BookList::~BookList()
{
}
void BookList::insertBook(string bt, int bq, double bp)
{
int flag=0;
int pos=-1;
Book book(bt,bq,bp);
if(count==0)
{
BL.push_back(book);
}
else
{
for(int i=0;i<count;i++)
{
if(BL.at(i).title.compare(bt)==0)
{
flag=1;
BL.at(i).quantity+=bq;
BL.at(i).price=bp;
break;
}
}
if(flag==0)
{
for(int i=0;i<count;i++)
{
if(BL.at(i).title.compare(bt)>0)
{
flag=2;
break;
}
}
}
if(flag==2)
{
for(int i=count;i>pos;i--)
{
BL.insert(BL.begin()+i,BL.at(i-1));
}
BL.insert(BL.begin()+pos,book);
}
}
}
void BookList::search(string key)
{
int flag=0;
for(int i=0;i<count;i++)
{
if(BL.at(i).title.find(key)==0)// change
{
flag=1;
cout<<endl<<"Book "<<BL.at(i).title;
}
}
if(flag==0)
{
cout<<"No matched book in stock";
}
}
void BookList::remove(string t)
{
int flag=0;
for(int i=0;i<count;i++)
{
if(BL.at(i).title.compare(t)==0)
{
flag=1;
cout<<endl<<"Book "<<t<<" is removed";
BL.erase(BL.begin()+i,BL.begin()+i);
break;
}
}
if(flag==0)
{
cout<<"Book "<<t<<" is not in stock";
cout<<endl<<"So can't perform remove operation";
}
if(flag==1)
{
count--;
}
}
void BookList::print()
{
cout<<endl<<"Number of books in stock: "<<count;
cout<<endl<<"List of books "<<endl;
for(int i=0;i<count;i++)
{
cout<<endl<<BL.at(i).title;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.