1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// driver code
#include "driver.h"
int main()
{
// Display Menu
int option = 0;
const int CREATE = 1;
const int READ = 2;
cout << "\nCS 1410 Project 3";
cout << "\nSelect one of the following two options: ";
cout << "\n 1 - create a test file";
cout << "\n 2 - read the test file and display the results";
cout << "\n>> ";
// run the selected option
cin >> option;
if (option == CREATE)
{
createTestFile();
cout << "\nTest file has been created. ";
}
else if (option == READ)
{
readTestFile();
}
else
{
cout << "\nInvalid option.";
}
system("PAUSE");
return 0;
}
void displayBooks(const vector<Book>& books)
{
// set up cout to display currency
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// display heading
cout << "\nRecommended Reading List\n";
// display each account
for (unsigned i = 0; i < books.size(); i++)
{
Author p = books[i].getAuthor();
cout << books[i].getTitle() << '\n';
cout << p.getName() << '\n';
cout << p.getAddress() << '\n';
cout << books[i].getPages() << " pages\n";
cout << '$' << books[i].getPrice() << "\n\n\n";
}
}
void createTestFile()
{
// create a vector for storing the account objects
vector<Book> myBooks;
// create three Author objects
Author p1("J.K.Rowling", "Edinburgh, Scotland");
Author p2("Suzanne Collins", "Connecticut, USA");
Author p3("J.R.R. Tolkien", "Bournmouth, England");
// Create three Book objects
Book b1(p1, "Harry Potter and the Sorcerer's Stone", 256, 24.95);
Book b2(p2, "Mockingjay", 400, 12.99);
Book b3(p3, "The Hobbit", 322, 14.29);
// add the books to the vector
myBooks.push_back(b1);
myBooks.push_back(b2);
myBooks.push_back(b3);
// write the books to a file
// the file will be in the same folder as the executable file
// assume that the file opens
ofstream outputFile;
outputFile.open("bookData.txt");
for (unsigned i = 0; i < myBooks.size(); ++i)
{
myBooks[i].writeData(outputFile);
}
}
void readTestFile()
{
vector<Book> myBooks;
ifstream inputFile;
try
{
openFile(inputFile, "bookData.txt");
}
catch (Exceptions e)
{
int error = e.getError();
if (error == OPEN_ERROR)
{
cout << "There was a problem opening the file...";
system("PAUSE");
exit(1);
}
}
if (inputFile.good())
{
while (!inputFile.eof())
{
Book b;
try
{
b.readData(inputFile);
}
catch (Exceptions e)
{
int error = e.getError();
if (error == READ_ERROR)
{
cout << "There was a problem reading the file...\n";
system("PAUSE");
exit(1);
}
}
if (inputFile.good())
{
myBooks.push_back(b);
}
}
displayBooks(myBooks);
}
}
void openFile(ifstream& in, const string& _name)
{
// try to open the file
in.open(_name);
// if the fail but is set, teh file won't open ... throw an open error
if (!in)
{
throw Exceptions(OPEN_ERROR);
}
}
|