Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Project 6(C++) The book solution provided is incorrect. Can someone

ID: 3835161 • Letter: P

Question

Programming Project 6(C++)

The book solution provided is incorrect. Can someone help me please?

In this project you are to create a database of books that are stored using a vector. Keep track of the author, title, and publication date of each book. You program should have a main menu that allows the user to select from the following: (1) Add a book’s author, title, and date; (2) Print an alphabetical list of the books sorted by author; and (3) Quit.

You must use a class to hold the data for each book. This class must hold three string fields: one to hold the author’s name, one for the publication date, and another to hold the book’s title. Store the entire database of books in a vector in which each vector element is a book class object.

To sort the data, use the generic sort function from the <algorithm> library. Note that this requires you to define the < operator to compare two objects of type Book so that the author field from the two books are compared.

A sample of the input/output behavior might look as follows. Your I/O need not look identical, this is just to give you an idea of the functionality.

Select from the following choices:

1. Add new book

2. Print listing sorted by author

3. Quit

1

Enter title:

More Than Human

Enter author:

Sturgeon, Theodore

Enter data:

1953

Select from the following choices:

1. Add new book

2. Print listing sorted by author

3. Quit

1

Enter title:

Problem Solving with C++

Enter author:

Savitch, Walter

Enter date:

2015

Select from the following choices:

1. Add new book

2. Print listing sorted by author

3. Quit

2

The books entered so far, sorted alphabetically by author are:

Savitch, Walter. Problem Solving with C++. 2015.

Sturgeon, Theodore. More Than Human. 1953.

Select from the following choices:

1. Add new book

2. Print listing sorted by author

3. Quit

1

Enter title:

At Home in the Universe

Enter author:

Kauffman

Enter date

1996

Select from the following choices:

1. Add new book

2. Print listing sorted by author

3. Quit

2


The books entered so far, sorted alphabetically by author are:

Kauffman. At Home in the Universe. 1996.

Savitch, Walter. Problem Solving with C++. 2015.

Sturgeon, Theodore. More Than Human. 1953.

Explanation / Answer

book information store using c++

BookNode.h

class BookNode {
public:
string title;
BookNode *next;

BookNode(string cat) {
title = cat;
next = NULL;
}

void setNext(BookNode *nn) {
next = nn;
}

BookNode* getNext() {
return next;
}

string getTitle() {
return title;
}
};

BooksList.h

class BooksList {
CategoryNode *head;
public:
BooksList() {
head = NULL;
}

bool isEmpty() {
return (head==NULL);
}

bool Search(string query, int val, string save) { // search a book / category..
bool retFound = false;
if(save == "yes")
fileWrite.open("Books_Info.txt",ios::app);
if(!isEmpty()){
CategoryNode *curr=head,*ret=NULL;
bool found=false;

while(curr!=NULL && !found){
if(curr->category == query && val==2) { // search in category
if(save == "yes")
fileWrite<<" Category : "<<curr->category;
cout<<" Category : "<<curr->category;
found=true;
BookNode* sBooks = curr->books;
while(sBooks != NULL && !found) {
if(save == "yes")
fileWrite<<" Book Title : "<<sBooks->title;
cout<<" Book Title : "<<sBooks->title;
sBooks = sBooks->next;
}
}
else if(val==1) { // search in books..
BookNode* sBooks = curr->books;
bool bookF = false;
while(sBooks != NULL && !bookF) {
if(sBooks->title == query) {
bookF = true;
if(save == "yes")
fileWrite<<" Book Title : "<<sBooks->title;
cout<<" Book Title : "<<sBooks->title;
}
else {
sBooks = sBooks->next;
}
}
if(bookF) {
if(save == "yes")
fileWrite<<" Category : "<<curr->category;
cout<<" Category : "<<curr->category;
found = retFound = true;
}
}
curr=curr->next;
}
retFound = found;
}
if(save == "yes") fileWrite.close();
return retFound;
}

bool DeleteBookByTitle(string bookName) {
bool retResult = false;
if(!isEmpty()){
CategoryNode *curr=head,*ret=NULL;
bool deleted=false;
while(curr!=NULL && !deleted) {
bool found = false;
BookNode* currBook = curr->books;
BookNode* prevBook = NULL;
while(currBook != NULL && !found) {
if(currBook->title == bookName) {
found = true;
}
else {
prevBook = currBook;
currBook = currBook->next;
}
}
if(found) {
BookNode* node = currBook;
if(prevBook == NULL) {
curr->books = currBook->next;
}
else {
prevBook->next = currBook->next;
}
delete (node);
deleted = true;
}
else // move to next category
curr=curr->next;
}
retResult = deleted;
}
return retResult;
}

bool InsertCategory(string cat) {
bool retInserted = false;
CategoryNode *newnode = new CategoryNode(cat);
if(isEmpty()) {
head = newnode;
retInserted = true;
}
else {
newnode->next = head;
head = newnode;
retInserted = true;
}
return retInserted;
}

bool InsertBook(string bok, int catID) {
bool retInserted = false;
BookNode *newBook = new BookNode(bok);
if(!isEmpty()) { // categories available
int i=1;
CategoryNode *curr = head,*prev = NULL;
bool CanInsert=false;
i = 1;
while(i<= catID && curr!=NULL && !CanInsert) {
if(i == catID) {
if(curr->books == NULL) {
CanInsert = true;
}
else{
BookNode *sBooks = curr->books;
bool foundBook = false;
while(sBooks!=NULL && !foundBook) {
if(sBooks->title == bok) { // book found
foundBook = true;
}
else {
sBooks = sBooks->next;
}
}
if(!foundBook) {
CanInsert = true;
}
}
}
else{
curr = curr->next;
i++;
}
}
if( CanInsert && curr != NULL ) {
if(curr->books == NULL){
curr->books = newBook;
retInserted = true;
}
else {
newBook->next = curr->books;
curr->books = newBook;
retInserted = true;
}
}
}
return retInserted;
}

bool DeleteCategoryByID(int catID) {
bool retDeleted = false;
if(!isEmpty()){
CategoryNode* curr = head;
CategoryNode* prev = NULL;
int i=1;
bool canDelete = false;
while(curr!=NULL && i<=catID && !canDelete) {
if(i == catID){
canDelete = true;
}
else{
prev = curr;
curr = curr->next;
i++;
}
}
if(canDelete) {
if(prev == NULL) { // delete First
CategoryNode *node = curr;
head = curr->next;
delete (node);
retDeleted = true;
}
else {
CategoryNode *node = curr;
prev->next = curr->next;
delete (node);
retDeleted = true;
}
}
}
return retDeleted;
}

void InsertNewCategory() {
string categ;
cout<<"Category Name : ";
fflush(stdin);
getline(cin,categ);
convertToLower(categ);
if(InsertCategory(categ)) {
cout<<"Inserted.. ";
}
else {
cout<<"Already Exist.! ";
}
}

void InsertNewBook() {
if(!isEmpty()){
string bookTitle = "";
int categ = -1, i = 1;
CategoryNode *ptr = head;
cout<<"Book Title : ";
fflush(stdin);
getline(cin,bookTitle);
convertToLower(bookTitle);
cout<<" Available Books Categories ";
while(ptr!=NULL) {
cout<<i<<". "<<ptr->category<<endl;
ptr = ptr->next;
i++;
}
cout<<"Choose Category :"; cin>>categ;

if( InsertBook(bookTitle,categ) ) {
cout<<"Inserted.. ";
}
else {
cout<<"Book Already Exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}

void DeleteCategory() {
if(!isEmpty()) {
int categ = -1, i = 1;
CategoryNode *ptr = head;
cout<<" Available Books Categories ";
while(ptr!=NULL) {
cout<<i<<". "<<ptr->category<<endl;
ptr = ptr->next;
i++;
}
cout<<"Choose Category to delete :"; cin>>categ;
if( DeleteCategoryByID(categ) ) {
cout<<"Deleted.. ";
}
else {
cout<<"Category not exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}

void DeleteBook() {
if(!isEmpty()) {
string bookTitle = "";
cout<<"BookTitle to delete :";
fflush(stdin);
getline(cin,bookTitle);
convertToLower(bookTitle);
if( DeleteBookByTitle(bookTitle) ) {
cout<<"Deleted.. ";
}
else {
cout<<"No Category exist.! ";
}
}
else {
cout<<"No Categories Available! ";
}
}

void SearchBook() {
if(!isEmpty()) {
string bookTitle = "", saveResult="no";
cout<<"BookTitle to search :";
fflush(stdin);
getline(cin,bookTitle);
cout<<"Do you want to save results too(yes/no): ";
cin>>saveResult;
convertToLower(bookTitle);
convertToLower(saveResult);
if(!Search(bookTitle,1,saveResult))
cout<<"Book Not Found! ";
}
else {
cout<<"No Categories Available! ";
}
}

void SearchCategory() {
if(!isEmpty()) {
string Categ = "", saveResult="no";
cout<<"Category to search :";
fflush(stdin);
getline(cin,Categ);
cout<<"Do you want to save results too(yes/no): ";
cin>>saveResult;
convertToLower(saveResult);
convertToLower(Categ);
if(!Search(Categ,2,saveResult))
cout<<"Category Not Found! ";
}
else {
cout<<"No Categories Available! ";
}
}

void PrintAll() {
if(!isEmpty()) {
CategoryNode* currCateg = head;
int i=1,j=1;
while(currCateg!=NULL) {
cout<<" Category "<<i<<": "<<currCateg->category<<" ";
BookNode *currBook = currCateg->books;
j=1;
while(currBook!=NULL) {
cout<<" Book Title"<<j<<" : "<<currBook->title<<" ";
currBook = currBook->next;
j++;
}
currCateg = currCateg->next;
i++;
}
}
else {
cout<<"No Categories Available! ";
}
}
};

CategoryNode.h

class CategoryNode {
public:
string category;
CategoryNode *next;
BookNode *books; // all books of that particular category

CategoryNode(string cat) {
category = cat;
next = NULL;
books = NULL;
}

void setNext(CategoryNode *nn) {
next = nn;
}

void setBooks(BookNode *bb) {
books = bb;
}

CategoryNode* getNext() {
return next;
}

BookNode* getBooks() {
return books;
}

string getCategory() {
return category;
}
};

globel.h

void convertToLower(string & str) {
for(int i=0; str[i]!=''; i++) {
str[i] = static_cast<char>(tolower(str[i]));
}
return;
}

#include "BookNode.h"
#include "CategoryNode.h"
#include "BookList.h"

Main.cpp

#include <iostream>
#include <fstream>
using namespace std;
ofstream fileWrite;
#include "Globel.h"


int main() {
BooksList* total = new BooksList();
char again='n';
do {
system("cls");
char ch;
cout<<" Welcome To MY-BOOKSTORE ";
cout<<"Select Option. ";
cout<<" 1. Add New Category ";
cout<<" 2. Add New Book ";
cout<<" 3. Delete a Category ";
cout<<" 4. Delete a Book ";
cout<<" 5. Search a Category ";
cout<<" 6. Search a Book ";
cout<<" 7. Print All ";
cout<<"Input : ";
cin>>ch;
switch(ch) {
case '1':
total->InsertNewCategory();
break;
case '2':
total->InsertNewBook();
break;
case '3':
total->DeleteCategory();
break;
case '4':
total->DeleteBook();
break;
case '5':
total->SearchCategory();
break;
case '6':
total->SearchBook();
break;
case '7':
total->PrintAll();
break;
default:
cout<<"Invalid Option. ";
break;
}
cout<<" Do you want to run again(y/n) : "; cin>>again;
}while(again!='n');
system("pause");
}