Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the ArrayBag class structure. Implement a menu of options that uses the memb

ID: 2246875 • Letter: U

Question

Use the ArrayBag class structure.

Implement a menu of options that uses the member functions defined in the class.

Search based on an ID you assign for each speaker.

ArrayBag.h

#ifndef _ARRAY_BAG

#define _ARRAY_BAG

#include "BagInterface.h"

template<class ItemType>

class ArrayBag : public BagInterface<ItemType>

{

private:

       static const int DEFAULT_CAPACITY = 6;

       ItemType items[DEFAULT_CAPACITY];     

     int itemCount;                        

int maxItems

  

       int getIndexOf(const ItemType& target) const;  

public:

       ArrayBag();

       int getCurrentSize() const;

       bool isEmpty() const;

       bool add(const ItemType& newEntry);

       bool remove(const ItemType& anEntry);

       void clear();

       bool contains(const ItemType& anEntry) const;

       int getFrequencyOf(const ItemType& anEntry) const;

       vector<ItemType> toVector() const;

};

#include "ArrayBag.cpp"

#endif

ArrayBag.cpp

#include "ArrayBag.h"

#include <cstddef>

template<class ItemType>

ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY)

{

}

template<class ItemType>

int ArrayBag<ItemType>::getCurrentSize() const

{

       return itemCount;

}

template<class ItemType>

bool ArrayBag<ItemType>::isEmpty() const

{

       return itemCount == 0;

}

template<class ItemType>

bool ArrayBag<ItemType>::add(const ItemType& newEntry)

{

       bool hasRoomToAdd = (itemCount < maxItems);

       if (hasRoomToAdd)

       {

              items[itemCount] = newEntry;

              itemCount++;

       }     

       return hasRoomToAdd;

}

template<class ItemType>

bool ArrayBag<ItemType>::remove(const ItemType& anEntry)

{

   int locatedIndex = getIndexOf(anEntry);

       bool canRemoveItem = !isEmpty() && (locatedIndex > -1);

       if (canRemoveItem)

       {

              itemCount--;

              items[locatedIndex] = items[itemCount];

       }

   

       return canRemoveItem;

}

template<class ItemType>

void ArrayBag<ItemType>::clear()

{

       itemCount = 0;

}

template<class ItemType>

int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const

{

   int frequency = 0;

   int curIndex = 0;      

   while (curIndex < itemCount)

   {

      if (items[curIndex] == anEntry)

      {

         frequency++;

      }

     

      curIndex++;         

   }

  

   return frequency;

}

template<class ItemType>

bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const

{

       return getIndexOf(anEntry) > -1;

}

template<class ItemType>

vector<ItemType> ArrayBag<ItemType>::toVector() const

{

       vector<ItemType> bagContents;

       for (int i = 0; i < itemCount; i++)

              bagContents.push_back(items[i]);

     

   return bagContents;

}

template<class ItemType>

int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const

{

       bool found = false;

   int result = -1;

   int searchIndex = 0;

  

   while (!found && (searchIndex < itemCount))

   {

      if (items[searchIndex] == target)

      {

         found = true;

         result = searchIndex;

      }

      else

      {

         searchIndex++;

      }

   }

  

   return result;

}

BagInterface.h

#ifndef _BAG_INTERFACE

#define _BAG_INTERFACE

#include <vector>

using namespace std;

template<class ItemType>

class BagInterface

{

public:

  

   virtual int getCurrentSize() const = 0;

   virtual bool isEmpty() const = 0;

   virtual bool add(const ItemType& newEntry) = 0;

   virtual bool remove(const ItemType& anEntry) = 0;

   virtual void clear() = 0;

   virtual int getFrequencyOf(const ItemType& anEntry) const = 0;

   virtual bool contains(const ItemType& anEntry) const = 0;

   virtual vector<ItemType> toVector() const = 0;

};

#endif

9. Speakers' Bureau structure to store the following data about a speaker: Name Telephone Number Speaking Topic Fee Required The program should use an array of at least 10 structures. It should let the user enter data into the array, change the contents of any element, and display all the data store in the array. The program should have a menu-driven user interface. Input Validation: When the data for a new speaker is entered, be sure the user enters data for all the fields. No negative amounts should be entered for a speaker's fee.

Explanation / Answer

#ifndef _ARRAY_BAG
#define _ARRAY_BAG

#include "Baginterfce.h"

template<class ItemType>
class ArrayBag : public Baginterfce<ItemType>
{
private:
static const int DTCAPCTY = 6;
ItemType items[DTCAPCTY];   
int itemCount;
int maxItems
  
int getIndexOf(const ItemType& target) const;

public:
ArrayBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
vector<ItemType> toVector() const;
};
#include "ArrayBag.cpp"
#endif

ArrayBag.cpp
#include "ArrayBag.h"
#include <cstddef>

template<class ItemType>
ArrayBag<ItemType>::ArrayBag(): itemCount(0), maxItems(DTCAPCTY)
{
}

template<class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return itemCount;
}

template<class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
return itemCount == 0;
}




template<class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < maxItems);
if (hasRoomToAdd)
{
items[itemCount] = newEntry;
itemCount++;
}   
return hasRoomToAdd;
}

template<class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anEntry)
{
int locatedIndex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedIndex > -1);
if (canRemoveItem)
{
itemCount--;
items[locatedIndex] = items[itemCount];
}

return canRemoveItem;
}

template<class ItemType>
void ArrayBag<ItemType>::clear()
{
itemCount = 0;
}

template<class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0;
while (curIndex < itemCount)
{
if (items[curIndex] == anEntry)
{
frequency++;
}

curIndex++;   
}
  
return frequency;
}

template<class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
}



template<class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
{
vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(items[i]);

return bagContents;
}

template<class ItemType>
int ArrayBag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
  
while (!found && (searchIndex < itemCount))
{
if (items[searchIndex] == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
}
}
  
return result;
}

Baginterfce.h
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE

#include <vector>
using namespace std;

template<class ItemType>
class Baginterfce
{
public:
  
virtual int getCurrentSize() const = 0;
virtual bool isEmpty() const = 0;
virtual bool add(const ItemType& newEntry) = 0;
virtual bool remove(const ItemType& anEntry) = 0;
virtual void clear() = 0;
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
virtual bool contains(const ItemType& anEntry) const = 0;
virtual vector<ItemType> toVector() const = 0;
};
#endif