Challenge: MVC Pattern
In this challenge, you have to implement the MVC pattern to solve the given problem.
We'll cover the following
Problem statement
In this challenge, you have to implement the MVC pattern to display the items in a shopping cart.
You have three components:
-
ShoppingCartModel
The shopping cart model should have the following properties:
itemNumber
,itemName
,itemQuantity
, anditemPrice
. It should also have a get function for each property. -
ShoppingCartView
You are already given the part of the code that initializes the current view for the
controller
and thedisplayItem
function that displays an item’s information.Your task is to implement a
buyItem
function to buy an item.A
changeItemQuantity
function that updates the quantity of an item in the cart. It should take the following parameters:itemNumber
andnewQuantity
. -
ShoppingCartController
The controller should update the view whenever a change occurs in the shopping cart model or if a user edits the view.
Input
The buyItem
function is called to buy items and changeItemQuantity
is called to change the quantity of an existing item
Output
The items in the cart are displayed
Sample input
var view = new ShoppingCartView();
var controller = new ShoppingCartController();
view.registerWith(controller);
view.buyItem("Popcorn", 3, 2.50);
view.changeItemQuantity(0,6);
Sample output
"Item Number: 0"
"Item: Popcorn"
"Quantity: 3"
"Price: 2.5"
"Item Number: 0"
"Item: Popcorn"
"Quantity: 6"
"Price: 2.5"
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.