Question: main.cpp #include #include #include #include GroceryList.h using namespace std; bool ParseIndices ( std::string str , int& outIndex 1 , int& outIndex 2 ) ;

main.cpp
#include
#include
#include
#include "GroceryList.h"
using namespace std;
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2);
int main(int argc, char *argv[]){
// Initialize a new grocery listGroceryList groceryList;string command;bool quit = false;while (!quit){getline(cin, command);
// Process user input
if (command == "print"){
groceryList.Print(cout);}else if (0== command.find("add ")){
groceryList.AddWithUndo(command.substr(4));
}else if (0== command.find("removeat ")){int index = stoi(command.substr(9));groceryList.RemoveAtWithUndo(index); }else if (0command.find("swap ")){
int index1=-1, index2=-1;
if (ParseIndices(command.substr(5), index1, index2)){ groceryList.SwapWithUndo(index1, index2);}else {cout <<"\"swap\" command requires two indices, separated ";cout <<"by a space. Ex: swap 25"<< endl;}}else if (command == "undo"){if (0== groceryList.GetUndoStackSize()){cout << "Cannot execute undo because undo stack is empty" << endl;}else {groceryList.ExecuteUndo();}}else if (command == "quit"){quit = true;}else {cout << "Unknown command: "<< command << endl;}}return 0;}
bool ParseIndices(std::string str, int& outIndex1, int& outIndex2){auto spaceIndex = str.find("");if (spaceIndex == string::npos){return false;}outIndex1= stoi(str);outIndex2= stoi(str.substr(spaceIndex +1));return true;}
UndoCommand.h
#ifndef UNDOCOMMAND_H
#define UNDOCOMMAND_H
class UndoCommand {
public:virtual ~UndoCommand(){} virtual void Execute()=0;};#endif
RemoveLastCommand.h
#ifndef REMOVELASTCOMMAND_H
#define REMOVELASTCOMMAND_H
#include
#include
#include "UndoCommand.h"
class RemoveLastCommand : public UndoCommand {
private:
std::vector* sourceVector;
public:
RemoveLastCommand(std::vector* vector){
sourceVector = vector;
}
void Execute() override {
if (!sourceVector->empty()){
sourceVector->pop_back();
}}};
#endif
SwapCommand.h
#ifndef SWAPCOMMAND_H
#define SWAPCOMMAND_H
#include
#include
#include "UndoCommand.h"
class SwapCommand : public UndoCommand {
private:// Your member variable declarations herepublic:/ Your constructor code herevoid Execute() override {// Your code here}};
#endif
InsertAtCommand.h
#ifndef INSERTATCOMMAND_H
#define INSERTATCOMMAND_H
#include
#include
#include "UndoCommand.h"
class InsertAtCommand : public UndoCommand {
private:
// Your member variable declarations here
public:
// Your constructor code here
void Execute() override {/ Your code here}};
#endif
GroceryList.h
#ifndef GROCERYLIST_H
#define GROCERYLIST_H
#include
#include
#include "UndoCommand.h"
#include "RemoveLastCommand.h"
#include "InsertAtCommand.h"
#include "SwapCommand.h"
class GroceryList {
protected:
std::vector listItems;
std::stack undoStack;
public:
virtual void AddWithUndo(std::string newItemName){
// Add the new list item
listItems.push_back(newItemName);
// Make an undo command that removes the last item and push onto stack
undoStack.push(new RemoveLastCommand(&listItems));}
virtual void RemoveAtWithUndo(int removalIndex){
// Your code here}
virtual void SwapWithUndo(int index1, int index2){
// Your code here
}// Pops and executes the undo command at the top of the undo stack. Then
// deletes the executed command.
virtual void ExecuteUndo(){
// Your code here }virtual int GetListSize() const {return (int)listItems.size();}virtual int GetUndoStackSize() const {
return (int)undoStack.size();}virtual std::vector GetVectorCopy() const { return listItems;}virtual void Print(std::ostream& outputStream){for (size_t i =0; i < listItems.size(); i++){outputStream << i <<"."<< listItems[i]<< std::endl;}}};#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!