Challenge: MVC Pattern
In this challenge, you have to implement the MVC pattern to solve the given problem.
Problem statement
In this challenge, you have to implement the MVC pattern to display the items in a shopping cart.
You have three components:
-
ShoppingCartModelThe shopping cart model should have the following properties:
itemNumber,itemName,itemQuantity, anditemPrice. It should also have a get function for each property. -
ShoppingCartViewYou are already given the part of the code that initializes the current view for the
controllerand thedisplayItemfunction that displays an item’s information.Your task is to implement a
buyItemfunction to buy an item.A
changeItemQuantityfunction that updates the quantity of an item in the cart. It should take the following parameters:itemNumberandnewQuantity. -
ShoppingCartControllerThe 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"
Challenge #
Take a close look and design a step-by-step solution before jumping on to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided. Good Luck!
Let’s discuss the solution in the next lesson.