Answered step by step
Verified Expert Solution
Question
1 Approved Answer
tals of Computer Science II home > 7 . 9 : LAB: Grocery list editor with undo stack 7 . 9 LAB: Grocery list editor
tals of Computer Science II home : LAB: Grocery list editor with undo stack
LAB: Grocery list editor with undo stack
In this lab a grocery list editor with undo functionality is implemented
Step : Inspect the UndoCommand abstract base class
The readonly UndoCommand py file has a declaration for the UndoCommand abstract base class. Access UndoCommand py by clicking on the orange arrow next to
main.py at the top of the coding window. The UndoCommand class represents a command objectan object. that stores all needed information to execute an action at a later point in time. For this lab, a command object stores information to undo a grocery list change made by the user.
Step : Inspect the incomplete GroceryList class
The GroceryList class is declared in
GroceryList.py Two attributes are declared
A list of strings for list items
A stack of UndoCommand references for undo commands
A simple list implementation of stack ADT is provided in Stack py
Note that the add with undo method is already implemented. The method adds a new item to the list and pushes a new
RemoveLastCommand object onto the undo stack
Step : Implement RemoveLastCommand's execute method
The RemoveLastCommand class inherits from UndoCommand and is declared in RemoveLastCommand py When a RemoveLastCommand object is executed, the string list's last element is removed So when the user appends a new item to the grocery list, a RemoveLastCommand is pushed onto the stack of undo commands. Popping and executing the RemovelastCommand then removes the item most recently added by the user
RemoveLastCommand s source fist attribute and constructor are aiready declared
sotrce list is a reference to a Grocerylist objects list of strings
The constructor takes a reference to alist of strings as a parameter, and assigns source list with the reference
Implement RemoveLastCommand's execute method to remove sourcelist's last element.
Step : Implement GroceryList's executeundo method
of Computer Science II home : LAB: Grocery list editor with undo stack
bananas
The program's output does not affect grading, so additional test cases can be added, if desired.
Submitting code written so far to obtain partial credit is recommended before proceeding to the next step
Step : Implement the SwapCommand class and GroceryList's swapwithundo method
Implement the SwapCommand class in
SwapCommand.py The class itself is declared, but no attributes yet exist. Add necessary attribut and methods so that the command can undo swapping two items in the grocery list.
Implement GroceryList's swapwithundo method. The method swaps list items at the specified indices, then pushes a SwapCommand, to undo that swap, onto the undo stack.
Step : Implement the InsertAtCommand class and GroceryList's removeatwithundo method
Implement the InsertAtCommand class in
InsertAtCommand.py Add necessary fields and methods so that the command can undo removing a grocery list item at an arbitrary index
Implement GroceryList's removeatwithundo method. The method removes the list item at the specified index, then pushes an InsertAtCommand, to undo that removal, onto the undo stack.
Downioadable files
main.py
Undocommand.py
RemoveLastComand.py
SwapCommand.py
InsertAtCommand,py Grocerylist,py and
Stack.py
Download
:
:
self. undo stack Stack :
self Mist items append item name
Load default template.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started