1: // Created by Frank M. Carrano and Tim Henry. 2: // Copyright (c) 2013 __Pearson Education__. All rights reserved. 4: /** Listing 4-4. */ 5: #include "BagInterface.h" 6: #include "ArrayBag.h" 7: #include "LinkedBag.h" 8: #include <iostream> 9: #include <string> 10: #include <cctype> 12: using namespace std; 14: void displayBag(BagInterface<string>* bagPtr) 15: { 16: cout << "The bag contains " << bagPtr->getCurrentSize() 17: << " items:" << endl; 18: vector<string> bagItems = bagPtr->toVector(); 19: 20: int numberOfEntries = (int)bagItems.size(); 21: for (int i = 0; i < numberOfEntries; i++) 22: { 23: cout << bagItems[i] << " "; 24: } // end for 25: cout << endl << endl; 26: } // end displayBag 28: void bagTester(BagInterface<string>* bagPtr) 29: { 30: cout << "isEmpty: returns " << bagPtr->isEmpty() 31: << "; should be 1 (true)" << endl; 32: 33: string items[] = {"one", "two", "three", "four", "five", "one"}; 34: cout << "Add 6 items to the bag: " << endl; 35: for (int i = 0; i < 6; i++) 36: { 37: bagPtr->add(items[i]); 38: } // end for 39: 40: displayBag(bagPtr); 41: cout << "isEmpty: returns " << bagPtr->isEmpty() 42: << "; should be 0 (false)" << endl; 43: cout << "getCurrentSize returns : " << bagPtr->getCurrentSize() 44: << "; should be 6" << endl; 45: cout << "Try to add another entry: add(\"extra\") returns " 46: << bagPtr->add("extra") << endl; 47: } // end bagTester 49: int main() 50: { 51: BagInterface<string>* bagPtr = nullptr; 52: char userChoice; 53: cout << "Enter 'A' to test the array-based implementation\n" 54: << " or 'L' to test the link-based implementation: "; 55: cin >> userChoice; 56: if (toupper(userChoice) == 'A') 57: { 58: bagPtr = new ArrayBag<string>(); 59: cout << "Testing the Array-Based Bag:" << endl; 60: } 61: else 62: { 63: bagPtr = new LinkedBag<string>(); 64: cout << "Testing the Link-Based Bag:" << endl; 65: } // end if 66: 67: cout << "The initial bag is empty." << endl; 68: bagTester(bagPtr); 69: delete bagPtr; 70: bagPtr = nullptr; 71: cout << "All done!" << endl; 72: 73: return 0; 74: } // end main 76: /* 77: Enter 'A' to test the array-based implementation 78: or 'L' to test the link-based implementation: A 79: Testing the Array-Based Bag: 80: The initial bag is empty. 81: isEmpty: returns 1; should be 1 (true) 82: Add 6 items to the bag: 83: The bag contains 6 items: 84: one two three four five one 85: 86: isEmpty: returns 0; should be 0 (false) 87: getCurrentSize returns : 6; should be 6 88: Try to add another entry: add("extra") returns 0 89: All done! 90: 91: 92: Enter 'A' to test the array-based implementation 93: or 'L' to test the link-based implementation: L 94: Testing the Link-Based Bag: 95: The initial bag is empty. 96: isEmpty: returns 1; should be 1 (true) 97: Add 6 items to the bag: 98: The bag contains 6 items: 99: one five four three two one 100: 101: isEmpty: returns 0; should be 0 (false) 102: getCurrentSize returns : 6; should be 6 103: Try to add another entry: add("extra") returns 1 104: All done! 105: */