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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
void test()
{
// test1: test for invalid date
// should fail and give error message about invalid copyright date
// checked a "0" month, a negative day, and also a negative year
// all failed with expected error message
try
{
Book book{ "MyBook", "Some Author", "5-9-8-X",
{ -2017, Month::apr, 4 }, false, Genre::fiction };
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.1: error: " << e.what() << '\n';
cin.clear();
}
catch (Date::Invalid)
{
cerr << "try-catch block no.1 error: copyright date for one or more books is invalid\n";
}
// test 2: test for invalid ISBN
// should fail and give error message about invalid ISBN
// have tested letters, spaces, and '.' characters
// all failed with error message as they should have
try
{
Book book2{ "MyBook", "Some Author", "b-a-d-7",
{ 2017, Month::jan, 4 }, false, Genre::fiction };
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.2 error: " << e.what() << '\n';
cin.clear();
}
catch (Date::Invalid)
{
cerr << "try-catch block no.2 error: copyright date for one or more books is invalid\n";
}
// test 3: this is a test for two or more books with the same ISBN,
// so everything has to be good except for two ISBNs being the same
try
{
Book book3{ "Dummy", "William Copperfield", "5-6-9-4",
{2015, Month::apr, 25}, false, Genre::biography };
Library library;
library.add_book(book3);
book3.set_checked_in(true);
cout << "In test-case 3:\n";
if (library.book_in_library(book3))
{
for (unsigned i = 0; i < library.get_books_v().size(); ++i)
{
cout << "Book " << library.get_books_v()[i].get_title()
<< " is in library.\n";
}
}
else
{
cout << "Book was not added to library.\n";
}
Book book4{ "About Me", "Osman Zakir", "5-6-9-4",
{2015, Month::apr, 17}, false, Genre::biography };
for (const auto &book : library.get_books_v())
{
if (book4 == book)
{
error("this book has the same ISBN as a book that's already in the library");
}
else
{
library.add_book(book4);
book4.set_checked_in(true);
break;
}
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.3 error: " << e.what() << '\n';
}
catch (Date::Invalid)
{
cerr << "try-catch block no.3 error: copyright date for one or more books is invalid\n";
}
// test 4: test to check if two or more patrons with the same library card number
// will trigger an error or not
// should fail with error message as expected
try
{
Patron patron1{ "Osman Zakir", "1234565165548", false };
Patron patron2{ "Some Guy", "1234565165548", false };
Library library;
library.add_patron(patron1);
for (const auto &patron : library.get_patrons_v())
{
if (patron2 == patron)
{
error("this patron has the same library card number as another patron in the library");
}
else
{
library.add_patron(patron2);
break;
}
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.4 error: " << e.what() << '\n';
}
// test 5: testing a situation where a patron who owes a fee
// tries to check out a book
// should fail with error message as expected
try
{
Patron patron3{ "John Doe", "1239752925379", true };
Library library;
library.add_patron(patron3);
if (!is_fee_owed(patron3))
{
for (unsigned i = 0; i < library.get_books_v().size(); ++i)
{
if (!library.get_books_v().empty() && library.get_books_v().size() == 1)
{
Transaction transaction{ library.get_books_v()[i],
patron3, {2017, Month::jan, 22} };
library.add_transaction(transaction);
break;
}
}
}
else
{
error("this patron owes a fee!");
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.5 error: " << e.what() << '\n';
}
// test 6: test for an invalid library card number
// should fail with error message as expected
try
{
Patron patron{ "A", "12345badandwontwork", false };
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.6 error: " << e.what() << '\n';
}
// test 7: test for a situation where a patron tries to check out a book
// that exists but isn't in the library
// should fail with expected error message
try
{
Book book5{ "A Book", "Some Author", "5-6-1-8",
{2016, Month::mar, 12}, false, Genre::nonfiction };
Patron patron{ "B", "1234512345679", false };
Library library;
library.add_patron(patron);
// note: not adding book to library here
// patron will try to check out that book:
if (!is_fee_owed(patron))
{
if (library.book_in_library(book5))
{
library.check_book_out(book5, patron);
Transaction transaction{ book5, patron, {2017, Month::apr, 25} };
library.add_transaction(transaction);
}
else
{
error("book not in library!");
}
}
else
{
error("this patron owes a fee!");
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.7 error: " << e.what() << '\n';
}
// test 8: testing a situation where a patron who isn't in the library
// tries to check out a book
try
{
Library library;
Patron patron1{ "Some Patron", "1234596875639", false };
Book book1{ "Some Book", "A", "1-2-3-9",
{2017, Month::aug, 14}, false, Genre::fiction };
library.add_book(book1);
book1.set_checked_in(true);
if (library.patron_in_library(patron1) && !is_fee_owed(patron1))
{
if (library.book_in_library(book1))
{
library.check_book_out(book1, patron1);
Transaction trans{ book1, patron1, {2017, Month::mar, 25} };
library.add_transaction(trans);
}
else
{
string s = "book " + book1.get_title();
error(s, " not in library!");
}
}
else if (!library.patron_in_library(patron1))
{
string s = "book " + patron1.get_user_name();
error(s, " not in library!");
}
else if (is_fee_owed(patron1))
{
string s = "book " + patron1.get_user_name();
error(s, " owes a fee!");
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.8 error: " << e.what() << '\n';
}
// testing a situation where there's no problem
// should pass with everything working
// result: test failed because library's books'
// vector came out empty
try
{
Library library;
Patron patron{ "Some Patron", "5694125968756", false };
library.add_patron(patron);
if (!library.get_books_v().empty())
{
const string isbn = "5-6-9-4";
for (auto book : library.get_books_v())
{
if (book.get_isbn() == isbn)
{
if (library.patron_in_library(patron) && !is_fee_owed(patron))
{
library.check_book_out(book, patron);
Transaction trans{ book, patron, {2017, Month::jan, 23} };
library.add_transaction(trans);
}
else if (!library.patron_in_library(patron))
{
string s = "patron " + patron.get_user_name();
error(s, " not in library!");
}
else if (is_fee_owed(patron))
{
string s = "patron " + patron.get_user_name();
error(s, " owes a fee!");
}
}
}
}
else
{
error("no books in the library!");
}
}
catch (const runtime_error &e)
{
cerr << "try-catch block no.9 error: " << e.what() << '\n';
}
Library lib;
if (!lib.get_transactions_v().empty())
{
for (auto transaction : lib.get_transactions_v())
{
cout << "Book " << transaction.m_book.get_title() << " has been checked out "
<< "by " << transaction.m_patron.get_user_name() << ".\n";
}
}
else
{
cout << "No transactions have been made successfully.\n";
}
}
|