I visual studio Create LibraryMain.cpp that includes BookClass.h 2. Declare an a
ID: 3737829 • Letter: I
Question
I visual studio
Create LibraryMain.cpp that includes BookClass.h
2. Declare an array of BookClass items: BookClass books[MAX_BOOKS];
3. Use the following function from Assignment 10 main program
a. void getBookInfo (BookClass &);
4. Write the following functions for BookMain.cpp
a. void getBooks (BookClass[], int &); Uses input data file books.txt to load the data structure. Similar to getBookInfo from Assignment 10, except uses a loop to read the data from the file. Calls storeBook to store the book information in an array location. Hint: bookList[i].storeBook ( ….. ) where booklist is the name of the array of class items and …. is the data for the book you read in from the file.
b. void showBooks (BookClass[], int); Lops through all the books in the array, calling member function displayBookInfo each pass to display the book in the current array location.
c. void showTitles (BookClass[], int); Loops through all the books in the array calling member function getTitle each pass to display the title of the book in the current array location.
d. int findBook (string, BookClass[], int); A search that loops through the book array, calling member function getTitle then compares the title to the search item passed in as an input argument.
e. int getMenuChoice(); Displays the following menu and returns the users choice: i.
1: Display all books ii.
2: Display book titles iii.
3: Find book iv.
4: Check out v.
5: Check in vi.
6: Exit program
f. Menu commands “Check In” and “Check Out” ask the user to enter the name of a book, invoke findBook to determine if the book is in the library, then if the book is in the Library (array of books), sends the location of the book in the array to either checkOutBook or returnBook to increment or decrement numInStock for the particular book.
5. BookMain.cpp program
a. Calls getBooks to load the input file into the array class structure
b. Uses a switch statement to call getMenuChoice and process the users’ choice.
c. Since you are mixing cin and getline, you will need to use cin.ignore to avoid an infinite loop situation.
6. Output must be labelled, organized and easy to read.
7. Program must be documented with Name, Date, Program Name and Description.
8. Submit zipped file with project, cpp file and screen shots of output.
and here is the book.txt info to use
Starting Out with C++
Gaddis, Tony
Pearson
978-0-13-257625-3
129.98
2014
25
The World is Flat
Friedman, Thomas
Farrar, Straus and Giroux
0-374-29279-5
30.00
2006
12
Good to Great
Collins, Jim
Collins Business
978-0-06-662099-2
29.99
2001
10
You Cant Make this Stuff Up
Caputo, Theresa
Atrai Books
978-1-4767-6443-6
25.00
2014
8
Winners
Steel, Danielle
Dell
978-0-440-24525-4
7.99
2013
6
The Success Principles
Canfield, Jack
Harper Collins
0-06-059488-8
24.95
2005
4
as well as the code used to create bookclass.h
#ifndef BOOKCLASS_H_
#define BOOKCLASS_H_
#include <iostream>
#include <string>
using namespace std;
class BookClass {
string title;
string author;
string publisher;
string isbn;
double price;
int year;
int numInStock;
public:
//Stores the parameters into the BookClass member variables
void storeBook(string bookTitle, string authorName, string bookPublisher, string bookISBN, double bookPrice, int bookYear, int booksInStock)
{
title = bookTitle;
author = authorName;
publisher = bookPublisher;
isbn = bookISBN;
price = bookPrice;
year = bookYear;
numInStock = booksInStock;
}
// Displays the contents of the BookClass member variables
void displayBookInfo()
{
cout << " Book details :" << endl;
cout << " Title : " << title << endl;
cout << " Author : " << author << endl;
cout << " Publisher : " << publisher << endl;
cout << " ISBN : " << isbn << endl;
cout << " Price : " << price << endl;
cout << " Year : " << year << endl;
cout << " Number in books in stock : " << numInStock << endl;
}
//Subtracts 1 from the numInStock member variable; tests to make sure numInStock is not 0
void checkOutBook()
{
if (numInStock > 0)
numInStock--;
}
//Adds 1 to the numInStock member variable
void returnBook()
{
numInStock++;
}
// Returns the value in title
string getTitle()
{
return title;
}
//Returns the value in numInStock
int getNumInStock()
{
return numInStock;
}
};
#endif /* BOOKCLASS_H_ */
// end of BookClass.h
//Implementation file BookClass.cpp testing the BookClass defined in BookClass.h
# include <iostream>
?
?
using namespace std;
// function declaration
void getBookInfo(BookClass &);
int main()
{
BookClass book;
getBookInfo(book);
book.checkOutBook();
cout << " Title : " << book.getTitle();
cout << " Number of books in stock : " << book.getNumInStock();
book.returnBook();
cout << " Title : " << book.getTitle();
cout << " Number of books in stock : " << book.getNumInStock();
system("pause");
return 0;
}
// Asks the user to enter information for one book,
//then invokes member function storeBook to store the information in the BookClass variable.
void getBookInfo(BookClass &bookClass)
{
string title;
string author;
string publisher;
string isbn;
double price;
int year;
int numInStock;
cout << " Enter the details of the book : ";
cout << " Title: ";
getline(cin, title);
cout << " Author : ";
getline(cin, author);
cout << " Publisher : ";
getline(cin, publisher);
cout << " ISBN : ";
getline(cin, isbn);
cout << " Price : $";
cin >> price;
cout << " Year : ";
cin >> year;
cout << " Number of books in stock: ";
cin >> numInStock;
bookClass.storeBook(title, author, publisher, isbn, price, year, numInStock);
?
}
Explanation / Answer
Enter all the function definations and the class defination in a header file and the rest in the main cpp file.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define MAX_BOOKS 200
int getMenuChoice();
class BookClass
{
private:
// 0-title,1- author, 2-publisher,3- ISBN,4- price, 5-yearPublished,6- numberOfCopies;
string info[7];
public:
string getTitle(); // get title of book
void storeBook(string *book); // store the book
void displayBookInfo(); // dispaly all the book info
string getCopiesLeft(); // display the copies left
void IncreaseDecreaseCopies(int flag); // manipulate the number of copies left
};
void BookClass::IncreaseDecreaseCopies(int flag)
{ // 0 for increment and 1 for decrement
int newNumber;
if (flag == 0)
{
newNumber = stoi(this->info[6]);
newNumber++;
this->info[6] = to_string(newNumber);
}
if (flag == 1)
{
newNumber = stoi(this->info[6]);
newNumber--;
this->info[6] = to_string(newNumber);
}
}
string BookClass::getCopiesLeft()
{
return this->info[6];
}
string BookClass::getTitle()
{
return this->info[0];
}
void BookClass::displayBookInfo()
{
for (int i = 0; i < 7; i++)
{
cout << this->info[i]<< endl;
}
}
void BookClass::storeBook(string *book)
{
for (int i = 0; i < 7; i++)
{
this->info[i] = book[i];
}
}
int getMenuChoice()
{
int choice;
cout << "i. 1: Display all books ii. 2: Display book titles iii. 3 : Find book iv. 4 : Check out v. 5 : Check in vi. 6 : Exit program" << endl;
cin >> choice;
return choice;
}
void getBooks(BookClass *books, int &totalBooks)
{
string fileName, line, tempInfo[7];
int info;
totalBooks = 0;
cout << "Enter the file name to load book information (with extension): ";
cin >> fileName;
ifstream file(fileName);
if (file.is_open())
{
info = 0;
while (getline(file, line))
{
if (line != "")
{
tempInfo[info++] = line;
if (info == 7)
{
books[totalBooks++].storeBook(tempInfo);
info = 0;
tempInfo->clear();
}
}
}
}
}
void showBooks(BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
books[i].displayBookInfo();
}
}
void showTitles(BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
cout << books[i].getTitle() << endl;
}
}
int findBook(string book, BookClass *books, int totalBooks)
{
for (int i = 0; i < totalBooks; i++)
{
if (strcmp(books[i].getTitle().c_str(), book.c_str()) == 0)
{
return i;
}
}
return -1;
}
int main()
{
BookClass books[MAX_BOOKS];
string book;
bool exit = false;
int totalBooks,idx;
getBooks(books, totalBooks);
while (!exit)
{
switch (getMenuChoice())
{
case 1:
showBooks(books, totalBooks);
break;
case 2:
showTitles(books, totalBooks);
break;
case 3:
cout << "enter the name of the book to find : ";
cin >> book;
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "This book is not available ";
}
else
{
cout <<books[idx].getCopiesLeft()<< " copies of " << books[idx].getTitle() << " are in stock " << endl;
}
break;
case 4:
cout << "Enter tittle of book to check out ";
cin >> book;
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "Sorry we do not stock that book ";
}
else
{
cout << "Book is available to check out ";
books[idx].IncreaseDecreaseCopies(1);
cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock after checkout" << endl;
}
break;
case 5:
cout << "Enter tittle of book to check in ";
cin >> book;
idx = findBook(book, books, totalBooks);
if (idx == -1)
{
cout << "Sorry we do not stock that book ";
}
else
{
cout << books[idx].getTitle() << " returned ";
books[idx].IncreaseDecreaseCopies(0);
}
break;
case 6:
return 0;
default:
cout << "Enter correct choice : ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.