This is a C++ question. I am supposed to write code to solve the following probl
ID: 3734678 • Letter: T
Question
This is a C++ question. I am supposed to write code to solve the following problem.
Im used to code written in Visual Studio, therefor im used to "using namespace std"
"Library"
Design and implement a Book class, to be used as part of software for a library. Class Book should have members for the ISBN, title, author, copyright date.Also store data on whether or not the book is checked out. Create functions for returning those values. Create functions for checking a book in and out. Do simple validation of data entered into a book,that is accept only ISBNs only of the form n-n-n-x where n is an integer and x is a digit or a letter. Store and ISBN as a string.
The add operators for the book class. Have the == operator check whether the ISBN numbers are the same for two books. Have the != also compare the ISBN number. Have a <<print out for the title, author and ISBN numbers on seperate lines.
Create an enumerated type for the Book class called Genre. Have the types be fiction,nonfiction,periodical,biography and children. Give each book a Genre and make appropritate changes to the Book constructor and member functions.
Create a Patron class for the library, the class will have users name, library card numer and library fees (if owed). Gave functions that access this data, as well as function to set the fee of the user. Have a helper function that returns a Boolean depending on wheaterh or not the user owes a fee.
Create a Library class. Include vectors of Books and Patrons. Include a struct called Transaction. Have it include a Book, a Patron and a Date (choose at random). Make a vector of Transactions. Create functions to add boooks to the library, add patrons to the library, and check out books. Whenever a user checks out a book, have the library make sure that both the user and book are in the library, if they are not, report an error. Then check to make sure the user does not owe any fees, if the user does, report an error. If not, create a Transaction and place it in the vector of Transactions. Also write a function that will return a vector that contains the names of all Patrons that owe fees.
I really have no idea where to begin to solve this question.
Explanation / Answer
//Book.cpp
#include<iostream>
#include<string.h>
using namespace std;
class Book
{
private:
string ISBN,title,author,copyrightDate;
bool checkedOut=false;
public:
enum Genre
{
fiction,nonfiction,periodical,biography,children
};
Genre genree;
//constructor
Book(string isbn,string titl,string auth,string copyrdate,Genre gen)
{
if(validate(isbn)){
ISBN=isbn;
title=titl;
author=auth;
copyrightDate=copyrdate;
genree=gen;
}
else{
cout<<"Sorry! enter isbn in correct format"<<endl;
}
}
//validate isbn
bool validate(string isbn)
{
int x=1,i;
//convert string into char array
char * dup = strdup(isbn.c_str());
char * token = strtok(dup, "-");//split before -
while (token != NULL)//iterate until token is null
{
if(x>4) return false;//if we get more than 4 fields the return false
if(x!=4){//if it is either 1,2 or 3 then
for(i=0;token[i]!='';i++)
{
if(token[i]<'0' || token[i]>'9') return false;//each character must be digit
}
}
else{//if it is fourth one then 1st character must be alpha numeric and 2nd chraacter must be end
if(isalnum(token[0]) && token[1]=='')
{
//do nothing
}
else return false;//if not then return false
}
token = strtok(NULL, "-");//get next token array
x++;//increase z
}
}
//print the detailes
void printOut()
{
cout<<"Book"<<endl;
cout<<"Title : "<<title<<endl;
cout<<"Author : "<<author<<endl;
cout<<"ISBN : "<<ISBN<<endl;
cout<<"Copy right date : "<<copyrightDate<<endl;
cout<<"Genre : "<<genree<<endl;
}
//compare isbn of two books
bool compareISBN(Book k)
{
if(ISBN!=k.getISBN())
{
return false;
}return true;
}//return isbn
string getISBN()
{
return ISBN;
}//return genre
Genre getGenre()
{
return genree;
};//return title
string getTitle()
{
return title;
}//return author
string getAuthor()
{
return author;
}
//return copyrightdate
string getcopyrightDate()
{
return copyrightDate;
}
//return value of checkedout
bool getCheckedOut()
{
return checkedOut;
}
//checkedout =true
void checkOutBook()
{
checkedOut=true;
}
//check in book then make checkedOut false
void checkInBook()
{
checkedOut=false;
}
};
//Patron.cpp
#include<iostream>
using namespace std;
class Patron
{
private:
string userName,cardNumber;
int owedFees;
public:
//constructor
Patron(string name,string num,int owedfee)
{
userName=name;
cardNumber=num;
owedFees=owedfee;
}
//constructor which takes only two values and assign zero to owedFees
Patron(string name,string num)
{
userName=name;
cardNumber=num;
owedFees=0;
}
//return true if he owes fee otherwise false
bool isOwed()
{
if(owedFees==0)
return false;
return true;
}
//return user name
string getUserName()
{
return userName;
}
//return card number
string getCardNumber()
{
return cardNumber;
}
//return fees owed
int getOwedFees()
{
return owedFees;
}
//set owed fee
void setOwedFees(int fee)
{
owedFees=fee;
}
};
//Library.cpp
#include<iostream>
#include<vector>
#include "Book.cpp"
#include "Patron.cpp"
using namespace std;
//structure of transaction
struct Transaction
{
Book* bok;
Patron* patr;
string dat;
};
class Library
{
private:
vector <Book> books;
vector <Patron> patrons;
vector <Transaction> transactions;
public:
//add book into books vector
void addBook(Book b)
{
books.push_back(b);
}
//add patron into patrons vector
void addPatron(Patron p)
{
patrons.push_back(p);
}
//check out book b by patron p
void checkOutBooks(Patron p,Book b)
{
int i;
//check whether book exist or not
for(i=0;i<books.size();i++)
{
if(books[i].compareISBN(b))//if present then break out of the loop
{
break;
}
}
if(i==books.size())//if couldnt find then print error message
{
cout<<"Error ! book doesnt exist"<<endl;
return;
}//check whether user is there in library or not
for(i=0;i<patrons.size();i++)
{
if(patrons[i].getUserName()==p.getUserName())
{
break;
}
}
if(i==patrons.size())
{
cout<<"Error ! user is not there is in the library"<<endl;
return;
}
//if he owes fee then report error
if(p.isOwed()==true)
{
cout<<"Error ! user owes fees "<<endl;
}
//create transaction structure
Transaction t;
t.bok=&b;
t.patr=&p;
t.dat="4/2/2018";
transactions.push_back(t);
}
//get all the users having some fees owed
vector <string> getUserOweFee()
{
vector <string > names;
int i;
for(i=0;i<patrons.size();i++)
{
if(patrons[i].isOwed())
{
names.push_back(patrons[i].getUserName());
}
}
return names;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.