Skip to content

Instantly share code, notes, and snippets.

@Samuelachema
Created August 14, 2018 00:08
Show Gist options
  • Save Samuelachema/1a259ddf29f2b3ce7085535640c37292 to your computer and use it in GitHub Desktop.
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...
/*
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
Copy link

class ShoppingCart {
  constructor() {
    this.total = 0;
    this.items = {};
  }

  addItem(itemName, quantity, price) {
    this.total += quantity * price;
    this.items[itemName] = quantity;
  }

  removeItem(itemName, quantity, price) {
    this.total -= quantity * price;
    this.items[itemName] = this.items[itemName] - quantity;
  }

  checkout(cashPaid) {
    if (cashPaid < this.total) {
      return "Cash paid not enough";
    }
    return cashPaid - this.total;
  }
}
class Shop extends ShoppingCart {
  constructor() {
    super();
    this.quantity = 100;
  }
  removeItem() {
    --this.quantity;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment