Você está na página 1de 6

class bookType

{
private:

string author[4]; //up to four authors

public:
bookType();
//Default constructor
//Initializes data members

bookType(string, string, string, double, int, int, string[]);


//Parameter constructor
//Initializes data members to user-defined values

//*************Begin Function Getters/Accessors********************

void getAuthors(string a[]);


//Function to return the authors of the book
//Postcondition: The values of author array are returned

//*************Begin Function Setters/Mutators********************

void setAuthors(string a[], int size);


//Function to set the array data member author
//Postcondition: author[i] = a; Max no. of authors is four

//*************Begin Update Functions********************

void updateCopies(int a);


//Function to update the no. of book copies by specified amount
//Postcondition: copies += a

bool updateAuthorCount();
//Function to update the author count by one
//Postcondition: authorCount++; true if successful, false otherwise

//*************Begin Equals Functions********************

bool equalsTitle(string t);


//Function to check book titles for equality
//Postcondition: title == t; True if equal, false otherwise

bool equalsISBN(string isbn);


//Function to check book ISBN for equality
//Postcondition: ISBN == isbn; True if equal, false otherwise
//*************Begin Print/Display Functions********************

void showAuthors() const;


//Function to show the authors of the book

};

----------------------------------------------------------------------------------------------------------------
---

//bookType.cpp

bookType::bookType()
{
//Initialize variables to default values
setTitle();
setPublisher();
setISBN();
setPrice();
setCopies();
setAuthorCount();

}//End bookType

//Parameter constructor
bookType::bookType(string t, string pub, string isbn, double p, int c, int ac, string a[])
{
//Initialize variables to user-defined values
setTitle(t);
setPublisher(pub);
setISBN(isbn);
setPrice(p);
setCopies(c);
setAuthorCount(ac);
setAuthors(a, getAuthorCount());

}//End bookType

//*************Begin Function Getters/Accessors********************

void bookType::getAuthors(string a[])


{
for(int i=0;i < authorCount;i++)
{
if(author[i] != "" || author[i] != "\0")
a[i] = author[i];
}
}
//*************Begin Function Setters/Mutators********************

void bookType::setAuthors(string a[], int size)


{
for(int i=0; i < size; i++)
{
author[i] = a[i];
}
}
//Function to set the array data member author
//Postcondition: author[i] = a; Max no. of authors is four

//*************Begin Update Functions********************

void bookType::incrementCopies()
{
copies++;
}
//Function to increment the no. of book copies by one
//Postcondition: copies++

bool bookType::updateAuthorCount()
{
bool rslt = false;

if(authorCount < 4)
{
authorCount++;
rslt = true;
}
return rslt;
}
//Function to update the author count by one
//Postcondition: authorCount++; true if successful, false otherwise

//*************Begin Print/Display Functions********************

void bookType::showAuthors() const


{
for(int i=0; i < authorCount; i++)
{
if(author[i] != "" || author[i] != "\0")
cout << "Author: " << author[i] << endl;
}
}

-------------------------------------------------------------------------------------------------------
------------

//main.cpp

int main()
{
int noOfBooks = 0;

//Declare 20-component array Programming of class bookType


bookType Programming[20];

//Populate first object of array


Programming[0].setTitle("C++ Programming");
Programming[0].setPublisher("Thomson");
Programming[0].setISBN("0-619-16042-X");
Programming[0].setPrice(49.95);
Programming[0].setCopies(10);
Programming[0].setAuthorCount(1);
string cppAuthors[] = {"D.S. Malik"};
Programming[0].setAuthors(cppAuthors, Programming[0].getAuthorCount());

noOfBooks++; //Increment no. of Books

//Populate second object of array


Programming[1].setTitle("VB.Net Programming");
Programming[1].setPublisher("Thomson");
Programming[1].setISBN("0-7895-6549-8");
Programming[1].setPrice(39.95);
Programming[1].setCopies(20);
Programming[1].setAuthorCount(3);
string vbAuthors[] = {"Gary B. Shelly", "Thomas J. Cashman", "Jeffrey J.
Quasney"};
Programming[1].setAuthors(vbAuthors, Programming[1].getAuthorCount());

noOfBooks++; //Increment no. of books

//Declare an object of bookType and copy it to the array


string gameAuthors[] = {"Michael Morrison", "David Morrison"};
bookType GameProg("Game Programming", "SAMS", "0-672-32461-X", 29.95, 30,
2, gameAuthors);

Programming[2].setTitle(GameProg.getTitle());
Programming[2].setPublisher(GameProg.getPublisher());
Programming[2].setISBN(GameProg.getISBN());
Programming[2].setPrice(GameProg.getPrice());
Programming[2].setCopies(GameProg.getCopies());
Programming[2].setAuthorCount(GameProg.getAuthorCount());
Programming[2].setAuthors(gameAuthors, Programming[2].getAuthorCount());

noOfBooks++; //Increment no. of books


//Use search functions
//1. Search by Title
cout << "\nSearch by Title" << endl;
cout << "--------------------" << endl;

int index = 0;
int matches = SearchTitle(Programming, noOfBooks, "Game Programming", index);
if(matches)
{
cout << "\nBook(s) Found by Title! No. of matches: " << matches << endl;
Programming[index].showBookInfo();
}
else
cout << "\nBook(s) not found by Title!" << endl;

//2. Search by ISBN


cout << "\nSearch by ISBN" << endl;
cout << "--------------------" << endl;

index = 0;
matches = SearchISBN(Programming, noOfBooks, "0-619-16042-X", index);
if(matches)
{
cout << "\nBook(s) Found by ISBN! No. of matches: " << matches << endl;
Programming[index].showBookInfo();
}
else
cout << "\nBook(s) not found by ISBN!" << endl;

//Update the number of copies of Programming book at index 1 by 20


Programming[1].updateCopies(20);

cout << "\nNo. of copies after update for book at index 1: " <<
Programming[1].getCopies() << endl;

//Update the author count of Programming book at index 0


if(Programming[0].updateAuthorCount())
{
cout << "\nUpdate author count successfull! Count is: " <<
Programming[0].getAuthorCount() << endl;
}
else
cout << "\nUpdate author count failure!" << endl;

//Set new author name for Programming book at index 0


string newAuthors[] = {"D.S. Malik", "Emmanuel Morales"};
Programming[0].setAuthors(newAuthors, 2);
//Show book info for all registered books
cout << "\nDisplay all books registered in system" << endl;
cout << "-------------------------------------------------" << endl;
for(int i = 0; i < noOfBooks; i++)
{
cout << "\nBook No. : " << i + 1 << endl;
cout << "-------------------" << endl;
Programming[i].showBookInfo();
cout << endl;
}

cin.get();

return 0;
}

int SearchTitle(bookType b[], int numBooks, string t, int &index)


{
int count = 0;

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


{
if(b[i].equalsTitle(t))
{
count++;
index = i;
}
}

return count;
}

int SearchISBN(bookType b[], int numBooks, string isbn, int &index)


{
int count = 0;

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


{
if(b[i].equalsISBN(isbn))
{
count++;
index = i;
}
}

return count;
}

Você também pode gostar