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 ParseIndicesstd::string str int& outIndex int& outIndex;
int mainint argc, char argv
Initialize a new grocery listGroceryList groceryList;string command;bool quit false;while quitgetlinecin command;
Process user input
if command "print"
groceryList.Printcout;else if command.findadd
groceryList.AddWithUndocommandsubstr;
else if command.findremoveat int index stoicommandsubstr;groceryList.RemoveAtWithUndoindex; else if command.findswap
int index index;
if ParseIndicescommandsubstr index index groceryList.SwapWithUndoindex index;else cout swap command requires two indices, separated ;cout by a space. Ex: swap endl;else if command "undo"if groceryList.GetUndoStackSizecout "Cannot execute undo because undo stack is empty" endl;else groceryListExecuteUndo;else if command "quit"quit true;else cout "Unknown command: command endl;return ;
bool ParseIndicesstd::string str int& outIndex int& outIndexauto spaceIndex strfind;if spaceIndex string::nposreturn false;outIndex stoistr;outIndex stoistrsubstrspaceIndex ;return true;
UndoCommand.h
#ifndef UNDOCOMMANDH
#define UNDOCOMMANDH
class UndoCommand
public:virtual ~UndoCommand virtual void Execute;;#endif
RemoveLastCommand.h
#ifndef REMOVELASTCOMMANDH
#define REMOVELASTCOMMANDH
#include
#include
#include "UndoCommand.h
class RemoveLastCommand : public UndoCommand
private:
std::vector sourceVector;
public:
RemoveLastCommandstd::vector vector
sourceVector vector;
void Execute override
if sourceVectorempty
sourceVectorpopback;
;
#endif
SwapCommand.h
#ifndef SWAPCOMMANDH
#define SWAPCOMMANDH
#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 INSERTATCOMMANDH
#define INSERTATCOMMANDH
#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 GROCERYLISTH
#define GROCERYLISTH
#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 AddWithUndostd::string newItemName
Add the new list item
listItems.pushbacknewItemName;
Make an undo command that removes the last item and push onto stack
undoStack.pushnew RemoveLastCommand&listItems;
virtual void RemoveAtWithUndoint removalIndex
Your code here
virtual void SwapWithUndoint index int index
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 intlistItemssize;virtual int GetUndoStackSize const
return intundoStacksize;virtual std::vector GetVectorCopy const return listItems;virtual void Printstd::ostream& outputStreamfor sizet i ; i listItems.size; ioutputStream i listItemsi std::endl;;#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
