Source of Listing3-2.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: #include <iostream>
  5: #include <string>
  6: #include "ArrayBag.h"

  8: using std::cout;
  9: using std::endl;

 11: void displayBag(ArrayBag<std::string>& bag)
 12: {
 13:    cout << "The bag contains " << bag.getCurrentSize()
 14:         << " items:" << endl;
 15:    std::vector<string> bagItems = bag.toVector();
 16:    
 17:    int numberOfEntries = (int)bagItems.size();
 18:    for (int i = 0; i < numberOfEntries; i++)
 19:    {
 20:       cout << bagItems[i] << " ";
 21:    }  // end for
 22:    cout << endl << endl;
 23: }  // end displayBag

 25: void bagTester(ArrayBag<string>& bag)
 26: {
 27:    cout << "isEmpty: returns " << bag.isEmpty()
 28:         << "; should be 1 (true)" << endl;
 29:    displayBag(bag);
 30:    
 31:    std::string items[] = {"one", "two", "three", "four", "five", "one"};
 32:    cout << "Add 6 items to the bag: " << endl;
 33:    for (int i = 0; i < 6; i++)
 34:    {
 35:       bag.add(items[i]);
 36:    }  // end for
 37:    
 38:    displayBag(bag);
 39:    
 40:    cout << "isEmpty: returns " << bag.isEmpty()
 41:         << "; should be 0 (false)" << endl;
 42:    
 43:    cout << "getCurrentSize: returns " << bag.getCurrentSize()
 44:         << "; should be 6" << endl;
 45:    
 46:    cout << "Try to add another entry: add(\"extra\") returns "
 47:         << bag.add("extra") << endl;
 48: } // end bagTester

 50: int main()
 51: {
 52:    ArrayBag<std::string> bag;
 53:    cout << "Testing the Array-Based Bag:" << endl;
 54:    cout << "The initial bag is empty." << endl;
 55:    bagTester(bag);
 56:    cout << "All done!" << endl;
 57:    
 58:    return 0;
 59: } // end main