Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can someone write comments to this code bool checkIn(const int ID, BooksPtr inventory, const int numBooks) { int index = findBook(ID, inventory, numBooks); if (index

can someone write comments to this code

bool checkIn(const int ID, BooksPtr inventory, const int numBooks)

{

int index = findBook(ID, inventory, numBooks);

if (index != -1 && inventory.heap[index].checkedOut)

{

inventory.heap[index].checkedOut = false;

return true;

}

return false;

}

void checkIn(BooksPtr inventory, const int numBooks)

{

cout << "Book Check in ";

cout << "Book ID: ";

int ID;

cin >> ID;

if (checkIn(ID, inventory, numBooks))

{

cout << "Book successfully checked in! ";

}

else

{

cout << "Book ID not found or book not checked out! No book checked in! ";

}

system("PAUSE");

}

bool checkOut(const int ID, BooksPtr inventory, const int numBooks)

{

int index = findBook(ID, inventory, numBooks);

if (index != -1 && !inventory.heap[index].checkedOut)

{

inventory.heap[index].checkedOut = true;

return true;

}

return false;

}

void checkOut(BooksPtr inventory, const int numBooks)

{

cout << "Book check out ";

cout << "Book ID: ";

int ID;

cin >> ID;

if (checkOut(ID, inventory, numBooks))

{

cout << "Book successfully checked out! ";

}

else

{

cout << "Book ID not found or book already checked out! No book checked out! ";

}

system("PAUSE");

}

bool addBook(const std::string & author, const std::string & title, const int ID, BooksPtr& inventory, int & numBooks)

{

if (numBooks < MAX_BOOKS)

{

if (numBooks == inventory.new_books)

{

occupyHeap(inventory, numBooks);

}

inventory.heap[numBooks].author = author;

inventory.heap[numBooks].title = title;

inventory.heap[numBooks].ID = ID;

numBooks++;

return true;

}

else

{

return false;

}

}

void addNewBook(int & currID, BooksPtr& inventory, int & numBooks)

{

cout << "Adding new book ";

cout << " Author: ";

string author;

cin >> author;

cout << " Title: ";

string title;

cin >> title;

if (addBook(author, title, currID, inventory, numBooks))

{

cout << "Book successfully added! ";

system("PAUSE");

currID++;

}

else

{

cout << "Book not added, too many books in system! ";

system("PAUSE");

}

}

void removeBook(BooksPtr inventory, int & numBooks)

{

cout << "Remove book from inventory ";

cout << "Please enter a book ID: ";

int ID;

cin >> ID;

if (removeBook(ID, inventory, numBooks))

{

cout << "Book successfully removed from inventory! ";

}

else

{

cout << "ID not found, no books removed! ";

}

system("PAUSE");

}

bool removeBook(const int ID, BooksPtr inventory, int & numBooks)

{

int bookIndex = findBook(ID, inventory, numBooks);

if (bookIndex != -1)

{

for (int i = bookIndex; i < numBooks - 1; i++)

{

inventory.heap[i] = inventory.heap[i + 1];

}

numBooks--;

return true;

}

else

{

return false;

}

}

bool loadBooks(const std::string & fileName, BooksPtr& inventory, int& numBooks, int& lastID)

{

ifstream fin(fileName);

if (fin.is_open())

{

fin >> lastID;

string title;

string author;

int ID;

char checkedOut;

releaseHeap(inventory, numBooks);

fin >> title >> author >> ID >> checkedOut;

while (!fin.eof())

{

if (!addBook(author, title, ID, inventory, numBooks))

{

return false; // only if too many books!

}

else if (checkedOut == 't')

{

inventory.heap[numBooks - 1].checkedOut = true;

}

fin >> title >> author >> ID >> checkedOut;

}

return true;

}

else

{

return false;

}

}

bool saveBooks(const std::string & fileName, const BooksPtr inventory, const int numBooks, const int lastID)

{

ofstream fout(fileName);

if (fout.is_open())

{

fout << lastID << endl;

for (int i = 0; i < numBooks; i++)

{

fout << inventory.heap[i].title << " " << inventory.heap[i].author << " " << inventory.heap[i].ID << " ";

if (inventory.heap[i].checkedOut)

{

fout << 't';

}

else

{

fout << 'f';

}

if (i < numBooks - 1)

{

fout << endl;

}

}

return true;

}

else

{

return false;

}

}

int findBook(const int ID, const BooksPtr inventory, const int numBooks)

{

int inventoryIndex = -1;

{

int i = 0;

while (inventoryIndex == -1 && i < numBooks)

{

if (inventory.heap[i].ID == ID)

{

inventoryIndex = i;

cout << inventory.heap[i].title << " by " << inventory.heap[i].author << " (checked out: ";

if (inventory.heap[i].checkedOut)

{

cout << "TRUE) ";

}

else

{

cout << "FALSE) ";

}

}

i++;

}

}

return inventoryIndex;

}

void initialiseHeap(BooksPtr& inventory)

{

inventory.heap = nullptr;

inventory.new_books = 0;

}

void occupyHeap(BooksPtr& inventory, const int numBooks)

{

if (numBooks == inventory.new_books && MAX_BOOKS > inventory.new_books)

{

inventory.new_books += MAX_BOOKS / 200;

if (inventory.new_books > MAX_BOOKS)

inventory.new_books = MAX_BOOKS;

Book* new_heap = new Book[inventory.new_books];

for (int i = 0; i < numBooks; i++)

new_heap[i] = inventory.heap[i];

if (inventory.heap)

delete[] inventory.heap;

inventory.heap = new_heap;

}

}

void releaseHeap(BooksPtr& inventory, int& numBooks)

{

delete[] inventory.heap;

inventory.heap = nullptr;

inventory.new_books = 0;

numBooks = 0;

}

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

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

d. How will lack of trust be handled?

Answered: 1 week ago

Question

Are the rules readily available?

Answered: 1 week ago