Question
c++ additional code to function that add new user to db Library.cpp #include User.h #include Book.h #include Library.h #include #include #include #include using namespace std;
c++ additional code to function that add new user to db
Library.cpp #include "User.h" #include "Book.h" #include "Library.h" #include #include #include #include
using namespace std;
int Library::readBooks(string bFileName) { int chklinesize; string lines; string words[200]; ifstream fileBooks; fileBooks.open(bFileName, ios::in);
if (fileBooks.is_open()) { while (getline(fileBooks, lines)) { //Get the length of the lines chklinesize = lines.length(); //lines = lines.substr(0, chklinesize - 1); lines = lines.substr(0, chklinesize);
Split(lines, ',', words, 5);
//Update the author and title data members for a Book oject Book b(words[1], words[0]); cout << words[1] << " by " << words[0] << endl;
books[numBooks] = b;
//Update the total number of books in the system (from all the files read so far) numBooks++; } return numBooks; } else { return -1; } }
int Library::readRatings(string rFileName) { string lines; string words[200]; int ratings[200];
ifstream fileRate; fileRate.open(rFileName);
while (getline(fileRate, lines)) { Split(lines, ',', words, 5); string username = words[0];
cout << username << "..." << endl;
int numRatings = Split(words[1], ' ', words, 200); for (int i = 0; i < numRatings - 1; i++) { ratings[i] = stoi(words[i]); //cout << ratings[i] << endl; }
User u(username, ratings, numRatings - 1); users[numUsers] = u; numUsers++;
} return numUsers; }
int Library::Split(string bookline, char delimiter, string words[], int max) { int wordCount = 0; int num = 0; int listSize; char ch; ch = delimiter; string temp;
temp = ""; listSize = bookline.length();
if (listSize == 0) return 0; else { //for (int i = 0; i < listSize; i++) for (int i = 0; i < listSize; i++) { if (bookline[i] != ch) { temp = temp + bookline[i]; } else { words[wordCount] = temp; temp = ""; wordCount += 1; }
}
words[wordCount] = temp; //
}
return wordCount; }
Library::Library() {
numBooks = 0; numUsers = 0;
}
void Library::viewRatings(string viewUser) { int vindex = 0; int count = 0; vindex = Library::getIndexId(viewUser); for (int i = 0; i < 86; i++) { cout << books[i].getTitle() << " " << endl; } int j = 0; j = numBooks; for (int i = 0; i < numBooks; i++) { if (users[vindex].getRatingAt(i) > 0) { if (count == 0) { cout << "Here are the books that you have rated:" << endl; } //if () cout << "Title : " << books[i].getTitle() << endl; cout << "Rating : " << users[vindex].getRatingAt(i) << endl; cout << "------------------------------" << endl; count++; } } if (count == 0) { cout << "You have not rated any books as of yet: " << endl; } }
void Library::printAllBooks() { if (numBooks == -1) { cout << "No books are stored" << endl; cout << endl; } else { for (int i = 0; i < numBooks; ++i) {
cout << books[i].getTitle() << " by " << books[i].getAuthor() << endl; } cout << endl; } }
int Library::getIndexId(string findName) { int i = 0; while ((i < numUsers) && (users[i].getUsername() != findName)) { if (users[i].getUsername() != findName) i++; } if (users[i].getUsername() == findName) {
return i; } else { return -1; } }
int Library::getCountReadBooks(string reader) {
int index = 0; int userBooksRead = 0; int rateValue = 0;
if ((numUsers == -1) || (numBooks == -1)) { cout << reader << " does not exist in our database" << endl;
return -1; }
index = Library::getIndexId(reader);
if (index == -1) { cout << reader << " does not exist in the database" << endl;
return -1; } else { for (int i = 0; i < numBooks; ++i) { rateValue = users[index].getRatingAt(i); if (rateValue != 0) { userBooksRead++; } } }
return userBooksRead; }
int Library::getBookIndexId(string findBook) { int i = 0;
while ((i < numBooks) && (books[i].getTitle() != findBook)) { cout << books[i].getTitle() << endl; i = i++; } if (books[i].getTitle() == findBook) { //return i + 1; return i; } else { return -1; } } double Library::calcAvgRating(string book ) { int index = 0; int i = 0; int j = 0; double bookMean = 0; int reviewCnt = 0;
if ((numUsers == 0) || (numBooks == 0)) { cout << book << " does not exists in the database" << endl; return -1; } else { index = getBookIndexId(book); if (index == -1) { cout << book << " does not exists in the database" << endl; return 0; } else { //find the number of books read/reviwed by a specific user for (int i = 0; i < numUsers; ++i) { if (users[i].getRatingAt(index) > 0) { bookMean = bookMean + users[i].getRatingAt(index); reviewCnt++; } } return bookMean / reviewCnt; } } }
Library.h
#include #include #include
using namespace std;
#ifndef Library_H //Library header #define Library_H //define the Library header
class Library {
private:
//Declare the number of books in the database (library) int numBooks = 0;
//Declare the number of users in the database (library) int numUsers = 0;
//Declare the number of books read; number is constant at 200 static const int sizeBook = 200;
//Declare the number of users; number is constant at 200 static const int sizeUser = 200;
//Declare an array of Book objects Book books[sizeBook];
//Declare an array of User objects User users[sizeUser];
public:
//The default constructor will set numBooks and Numbers to zero Library();
//Define readBooks to take in a string (the name of the file to be read) and //populates the books array. Returns the total number of books in the books array as integer
int readBooks(string bFile);
//Define readRatings to take in a string (the name of the file to be read) and //populates the users array. Returns the total number of users in the user array as integer
int readRatings(string rFile);
//The printAllBooks function prints all the stored in the books array and return nothing
void printAllBooks();
//The getCountReadBooks function will take in a string (username) and return the number //of books read by that user as an integer
int getCountReadBooks(string allbooks);
//The calAvgRating function takes a string (title of a book) and returns the average rating of the specified //book as a double
double calcAvgRating(string avgBook);
//The addUser function takes a string (username) and returns True if the username is sucessfully added to the database //otherwise return fales
bool addUser(string newUser);
//The checkOutBook function takes in two strings and an integer for the username, title of the book, //and a new rating, respectively (in this order). Returns true if the //rating is successfully updated. Otherwise, return false
bool checkOutBook(string userChkName, string titleChk, int newRate);
//The viewRatings function takes a string (username) and prints all the books //a user has provided for ratings
void viewRatings(string chkView);
//The getRecommendations function takes a string (username) and prints the first 5 book //recommendations from the most similar (other) user
void getRecommendations(string recUser);
int Split(string chkList, char delimiter, string chkWords[], int chkMax);
int getIndexId(string name);
int getBookIndexId(string book); }; //End of class
#endif //Library
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started