#include "Library.h" #include "Book.h" #include "Magazine.h" int main() { Library l; char resp = 'y'; int answer = -1; do { cout << "What would you like to do?\n"; cout << "\t(1) Add a new item\n"; cout << "\t(2) View all items\n"; cout << "\t(3) View a particular item\n"; cout << "> "; cin >> answer; while(answer < 1 || answer > 3) { cout << "Bad answer, try again > "; cin >> answer; } if(answer == 1) { char type; cout << "Would you like to add a book (b) or magazine (m)? "; cin >> type; while(type != 'b' && type != 'm') { cout << "Try again. > "; cin >> type; } LibraryItem* newitem; if(type == 'b') { string title; string author; cout << "Enter title: "; cin.ignore(cin.rdbuf()->in_avail(), '\n'); getline(cin, title); cout << "Enter author: "; cin.ignore(cin.rdbuf()->in_avail(), '\n'); getline(cin, author); newitem = new Book(title, author); } else { string title; int month; int year; cout << "Enter title: "; cin.ignore(cin.rdbuf()->in_avail(), '\n'); getline(cin, title); cout << "Enter publication month: "; cin >> month; cout << "Enter publication year: "; cin >> year; newitem = new Magazine(title, month, year); } try { l.addItem(newitem); } catch(LibraryFullException lfe) { cout << lfe.getMsg() << endl;; cout << "Sorry, cannot add item.\n"; } } else if(answer == 2) { l.printItems(); } else if (answer == 3) { int itemtoview; cout << "Which item would you like to view? "; cin >> itemtoview; try { LibraryItem* li = l.getItem(itemtoview-1); li->print(); } catch (NoSuchItemException nsie) { cout << nsie.getMsg() << endl; cout << "We're sorry your item could not be found.\n"; } } cout << "Go again? "; cin >> resp; while(resp != 'n' && resp != 'y') { cout << "Bad answer, try again > "; cin >> resp; } } while(resp == 'y'); return EXIT_SUCCESS; }