function Product(name,price) { this.name = name; this.price = price; //this.discount = 0; // <- remove this line and refactor with the code below var _discount; // private member Object.defineProperty(this,"discount",{ get: function() { return _discount; }, set: function(value) { _discount = value; if(_discount>80) _discount = 80; } }); } // the client code var sneakers = new Product("Sneakers",20); sneakers.discount = 50; // 50, setter is called sneakers.discount+= 20; // 70, setter is called sneakers.discount+= 20; // 80, not 90! alert(sneakers.discount); // getter is called
assertEquals(1, products.size()); Product product = products.iterator().next(); assertEquals(productName, product.getProductName()); assertEquals(price, product.getPrice());
public class PriceSorter implements Comparator<Product>{ public int compare(Product one, Product another){ int returnVal = 0; if(one.getPrice() < another.getPrice()){ returnVal = -1; }else if(one.getPrice() > another.getPrice()){ returnVal = 1; }else if(one.getPrice() == another.getPrice()){ returnVal = 0; } return returnVal; } }
for(Product p: array) { // initialization p = new Product(); total += p.getTotal(); fee += p.getRestockingFee(); }
@Test(expected = WrongCostFormat.class) public void shouldThrowNumberFormatException() { Product p = new Product(); p.checkAndReturnDoubleIfOk("1.234"); }
mProductArrayList.add(new Product("a", 100)); mProductArrayList.add(new Product("b", 200)); mProductArrayList.add(new Product("c", 300)); mProductArrayList.add(new Product("d", 400)); mProductArrayList.add(new Product("e", 500)); mProductArrayList.add(new Product("f", 600)); mProductArrayList.add(new Product("g", 700)); mProductArrayList.add(new Product("h", 800)); mProductArrayList.add(new Product("i", 900)); mProductArrayList.add(new Product("j", 1000)); mProductArrayList.add(new Product("k", 1100)); mProductArrayList.add(new Product("l", 1200)); mProductArrayList.add(new Product("m", 1000)); mProductArrayList.add(new Product("n", 1300)); mProductArrayList.add(new Product("o", 1400)); mProductArrayList.add(new Product("p", 1500));
public class SortByPrice implements Comparator{ public int compare(Object o1, Object o2) { Product p1 = (Product) o1; Product p2 = (Product) o2; // return -1, 0, 1 to determine less than, equal to or greater than return (p1.getPrice() > p2.getPrice() ? 1 : (p1.getPrice() == p2.getPrice() ? 0 : -1)); // **or** the previous return statement can be simplified to: return p1.getPrice() - p2.getPrice(); } }
new Product( "Electric Guitar", "http://images.example/electric-guitar.jpg", 100)); products.add( new Product( "Electric Train", "http://images.example/train.jpg", 100)); products.add( new Product( "Acoustic Guitar", "http://images.example/acoustic-guitar.jpg", 80)); products.add( new Product( "Guarana Soda", "http://images.example/soda.jpg",
var Product = Parse.Object.extend("product"); var newProduct = new Product(); var productName = "Temp4Product"; newProduct.set("name",productName); newProduct.set("name_lowercase",productName.toLowerCase()); newProduct.save();
function Product(sku) { Object.defineProperty(this, "sku", { get: function() { return sku;}, writable: false, // default value, doesn't have to be specified configurable: false, // default value, doesn't have to be specified enumerable: false }); } var p = new Product(1234); console.log(p.sku);
var items = [ new Product({ Name: "Crystalys", SKU: "00001", Price: 500, Stocks: 12, Quantity: 0 }), new Product({ ...
var a = new Product(); var b = new Product_dept(); a instanceof Product // true a instanceof Product_dept // false b instanceof Product // true b instanceof Product_dept // true
function Product(sku) { this.getSku = function() { return sku; } } var p = new Product(1234); console.log(p.getSku());
function Product() { this.product_id = 0; this.quantity = 0; this.book_cost = "hello"; } var p = new Product(); p.quantity = 5; var json = JSON.stringify(p);
var Product = function(){ this.is_active = true, this.is_bom_enabled = false; }; var p = new Product();
@Test public void testsTotalPriceAsSumOfProductPrices() { Products products = new Products(); // Or any appropriate constructor products.add(new Product("first", 10)); // Assuming such a constructor exists products.add(new Product("second", 20)); products.add(new Product("second", 30)); assertEquals("Sum must be eq to 60", 60, products.getTotalPrice()); }