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

(Data Structure C++) Need help changing what\'s on the switch and case statement

ID: 3885380 • Letter: #

Question

(Data Structure C++) Need help changing what's on the switch and case statements, has to be changed into tamplate class functions. Also make a search based on ID that can be assigned to each speaker.

Code:

#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;

  

};

///////////////////////////////

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;

  

bool operator ==(const ItemType &obj);

  

};

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)

  

{

{

  

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;

  

  

return result;

  

}

template<class ItemType>

bool ArrayBag<ItemType>:: operator ==(const ItemType &obj)

{

  

return (this->items[0] == obj );

  

}

#include<iostream>

#include<string>

using namespace std;

struct Speaker

{

  

string name;

  

string telephone;

  

string topic;

  

double fee;

  

};

/////////////////////////////////////////////////

int main()

{

  

ArrayBag<Speaker> speakersArray;

  

Speaker sp[10];

  

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

  

{

  

int option;

  

cout << "what you want to do Please select 1, 2 or 3:" << endl;

  

cout << " 1.Enter Data 2.Change content 3.Display" << endl;

  

string name, telephone, topic;

  

double fee;

  

vector<Speaker> v;

  

string newField;

  

double newFee;

  

cin >> option;

  

Speaker s;

  

switch (option)

  

{

  

case 1:

  

while (true)

  

{

  

cout << "Please enter Name of Speaker:";

  

cin >> name;

  

if (name.size() > 0)

  

break;

  

else

  

cout << "Please enter a valid Name of Speaker:";

  

}

  

cout << "Please enter telephone number of Speaker:";

  

cin >> telephone;

  

cout << "Please enter topic of Speaker:";

  

cin >> topic;

  

while (true)

  

{

  

cout << "Please enter fee of Speaker:";

  

cin >> fee;

  

if (fee > 0)

  

break;

  

else

  

cout << "Please enter a right positive number as fee of Speaker:";

  

}

  

sp[i].telephone = telephone;

  

sp[i].name = name;

  

sp[i].topic = topic;

  

sp[i].fee = fee;

  

speakersArray.add(sp[i]);

  

break;

  

case 2:

  

cout << "which element you want to change enter index starting from 0:";

  

int i;

  

cin >> i;

  

cout << "what you want to change:" << endl;

  

cout << "1: name 2: telephone 3:topic 4:fee" << endl;

  

int fieldTochange;

  

cin >> fieldTochange;

  

v = speakersArray.toVector();

  

s = v.at(i);

  

switch (fieldTochange)

  

{

  

case 1:

  

cout << "Enter new name " << endl;

  

cin >> newField;

  

s.name = newField;

  

case 2:

  

cout << "Enter new telephone " << endl;

  

cin >> newField;

  

s.telephone = newField;

  

case 3:

  

cout << "Enter new topic " << endl;

  

cin >> newField;

  

s.topic = newField;

  

case 4:

  

cout << "Enter new fee " << endl;

  

cin >> newFee;

  

s.fee = newFee;

  

default:

  

cout << "enter valid input" << endl;

  

break;

  

}

  

break;

  

case 3:

  

v = speakersArray.toVector();

  

for (auto it = v.begin(); it != v.end(); it++)

  

{

  

Speaker s = *it;

  

cout << "name " << s.name << " telephone " << s.telephone << " topic " << s.topic << " fee " << s.fee << endl;

  

}

  

break;

  

default:

  

cout << "please enter a valid option" << endl;

  

break;

}

}

return 0;

}

Explanation / Answer

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#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;

  

};

///////////////////////////////

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;

  

    bool operator ==(const ItemType &obj);

  

};

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)

      

    {

        {

          

            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;

  

  

    return result;

  

}

template<class ItemType>

bool ArrayBag<ItemType>:: operator ==(const ItemType &obj)

{

  

    return (this->items[0] == obj );

  

}

#include<iostream>

#include<string>

using namespace std;

struct Speaker

{

  

    string name;
string telephone;
    string topic;
    double fee;
};
/////////////////////////////////////////////////
int main()
{
    ArrayBag<Speaker> speakersArray;
    Speaker sp[10];
    for (int i = 0; i < 10; i++)
    {
        int option;

      

        cout << "what you want to do Please select 1, 2 or 3:" << endl;

      

        cout << " 1.Enter Data 2.Change content 3.Display" << endl;
        string name, telephone, topic;
        double fee;

      

        vector<Speaker> v;

      

        string newField;

      

        double newFee;

      

        cin >> option;

      

        Speaker s;

      

        switch (option)
        {
            case 1:
                while (true)

                {

                    cout << "Please enter Name of Speaker:";
                    cin >> name;
                    if (name.size() > 0)
                        break;
                    else
                        cout << "Please enter a valid Name of Speaker:";
                }

                cout << "Please enter telephone number of Speaker:";

                cin >> telephone;

                cout << "Please enter topic of Speaker:";
                cin >> topic;
          
                    cout << "Please enter fee of Speaker:";
                    cin >> fee;
                    if (fee < 0)
                    cout << "Please enter a right positive number as fee of Speaker:";

                sp[i].telephone = telephone;
                sp[i].name = name;

                sp[i].topic = topic;
                sp[i].fee = fee;

                speakersArray.add(sp[i]);
                break;

              

            case 2:

              

                cout << "which element you want to change enter index starting from 0:";

                int i;

                cin >> i;

                cout << "what you want to change:" << endl;
                cout << "1: name 2: telephone 3:topic 4:fee" << endl;

              

                int fieldTochange;

              

                cin >> fieldTochange;

              

                v = speakersArray.toVector();

              

                s = v.at(i);

              

                switch (fieldTochange)

              

            {

                  

                case 1:

                  

                    cout << "Enter new name " << endl;

                  

                    cin >> newField;

                  

                    s.name = newField;

                  

                case 2:

                  

                    cout << "Enter new telephone " << endl;

                  

                    cin >> newField;

                  

                    s.telephone = newField;

                  

                case 3:

                  

                    cout << "Enter new topic " << endl;

                  

                    cin >> newField;

                  

                    s.topic = newField;

                  

                case 4:

                  

                    cout << "Enter new fee " << endl;

                  

                    cin >> newFee;

                  

                    s.fee = newFee;

                  

                default:

                  

                    cout << "enter valid input" << endl;

                  

                    break;

                  

            }

                v.at(i)=s;

                break;

              

            case 3:

              

                v = speakersArray.toVector();

              

                for (auto it = v.begin(); it != v.end(); it++)

                  

                {

                  

                    Speaker s = *it;

                  

                    cout << "name " << s.name << " telephone " << s.telephone << " topic " << s.topic << " fee " << s.fee << endl;

                  

                }

              

                break;

              

            default:

              

                cout << "please enter a valid option" << endl;

              

                break;

        }  

    }

    return 0;

}