Created
August 14, 2018 00:08
-
-
Save Samuelachema/1a259ddf29f2b3ce7085535640c37292 to your computer and use it in GitHub Desktop.
Create a class called ShoppingCart. Create a constructor that has no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items. Create a method addItem that requires itemName, quantity and price arguments. This method should add the cost of the added items to the current value of total...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Create a class called ShoppingCart. Create a constructor that has no arguments and sets the total attribute to zero, and initializes an empty dict attribute named items. Create a method addItem that requires itemName, quantity and price arguments. This method should add the cost of the added items to the current value of total. It should also add an entry to the items dict such that the key is the itemName and the value is the quantity of the item. Create a method removeItem that requires similar arguments as add_item. It should remove items that have been added to the shopping cart and are not required. This method should deduct the cost of these items from the current total and also update the items dict accordingly. If the quantity of items to be removed exceeds current quantity in cart, assume that all entries of that item are to be removed. Create a method checkout that takes in cashPaid and returns the value of balance from the payment. If cashPaid is not enough to cover the total, return Cash paid not enough. Create a class called Shop that has a constructor which initializes an attribute called quantity at 100. Make sure Shop inherits from ShoppingCart. In the Shop class, override the removeItem method, such that calling Shop's removeItem with no arguments decrements quantity by one. | |
JavaScript | |
use camel case for your class method names, such that add_item becomes addItem | |
*/ | |
function ShoppingCart() { | |
this.total = 0; | |
this.items = {}; | |
} | |
//add item method | |
ShoppingCart.prototype.addItem = function(itemName,quantity,price){ | |
//add the cost of the added items to the current value of total. | |
this.total += price*quantity; | |
//add an entry to the items dict. | |
this.items[itemName]= quantity; | |
}; | |
//remove item method | |
ShoppingCart.prototype.removeItem = function(itemName,quantity,price){ | |
//deduct the cost of the items that have been removed from the shopping cart | |
this.total -= price*quantity; | |
//remove items from the items dict | |
this.items[itemName]= this.items[itemName] - quantity ; | |
}; | |
//checkout method | |
ShoppingCart.prototype.checkout = function(cashPaid){ | |
if(cashPaid < this.total){ | |
return "Cash paid not enough"; | |
} | |
return cashPaid-this.total ; | |
}; | |
//shop, sub class of ShoppingCart | |
function Shop() { | |
this.quantity = 100; | |
} | |
Shop.prototype = Object.create(ShoppingCart.prototype); | |
//remove item method | |
Shop.prototype.removeItem = function(){ | |
//remove items from the items dict | |
this.quantity = this.quantity-1; | |
}; |
SirPhemmiey
commented
May 23, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment