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> 11: void displayBag(BagInterface<std::string>* bagPtr) 12: { 13: std::cout << "The bag contains " << bagPtr– >getCurrentSize() 14: << " items:" << std::endl; 15: std::vector<std::string> bagItems = bagPtr– >toVector(); 16: int numberOfEntries = bagItems.size(); 17: for (int i = 0; i < numberOfEntries; i++) 18: { 19: std::cout << bagItems[i] << " "; 20: } // end for 21: std::cout << std::endl << std::endl; 22: } // end displayBag 24: void bagTester(BagInterface<std::string>* bagPtr) 25: { 26: std::cout << "isEmpty: returns " << bagPtr– >isEmpty() 27: << "; should be 1 (true)" << std::endl; 28: std::string items[] = {"one", "two", "three", "four", "five", "one"}; 29: std::cout << "Add 6 items to the bag: " << std::endl; 30: for (int i = 0; i < 6; i++) 31: { 32: bagPtr– >add(items[i]); 33: } // end for 34: 35: displayBag(bagPtr); 36: std::cout << "isEmpty: returns " << bagPtr– >isEmpty() 37: << "; should be 0 (false)" << std::endl; 38: std::cout << "getCurrentSize returns : " << bagPtr– >getCurrentSize() 39: << "; should be 6" << std::endl; 40: std::cout << "Try to add another entry: add(\"extra\") returns " 41: << bagPtr– >add("extra") << std::endl; 42: } // end bagTester 44: int main() 45: { 46: BagInterface<std::string>* bagPtr = nullptr; 47: char userChoice; 48: std::cout << "Enter 'A' to test the array-based implementation\n" 49: << " or 'L' to test the link-based implementation: "; 50: std::cin >> userChoice; 51: if (toupper(userChoice) == 'A') 52: { 53: bagPtr = new ArrayBag<std::string>(); 54: std::cout << "Testing the Array-Based Bag:" << std::endl; 55: } 56: else 57: { 58: bagPtr = new LinkedBag<std::string>(); 59: std::cout << "Testing the Link-Based Bag:" << std::endl; 60: } // end if 61: 62: std::cout << "The initial bag is empty." << std::endl; 63: bagTester(bagPtr); 64: delete bagPtr; 65: bagPtr = nullptr; 66: std::cout << "All done!" << std::endl; 67: 68: return 0; 69: } // end main