PDA

View Full Version : java script help (cash register)



jarques
06-03-2014, 06:27 AM
I need to do a cash register for a school assignment and i am currently able to make it give out the total price and take off a percentage discount.
I need help to make the register also account for location charges and discounts on location charges.

I have tried to work it out but i keep getting errors, any help would be appreciated.

Here is my current code

function Discount(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}

var discount = new Discount("Discount",5);
var bigDiscount = new Discount("BIGDiscount",10);


var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
scan: function(item,quantity){
switch (item){
case "P01": this.add(16.50 * quantity); break;
case "P02": this.add(14.75 * quantity); break;
case "P03": this.add(14.75 * quantity); break;
case "P04": this.add(19.00 * quantity); break;
case "P05": this.add(12.35 * quantity); break;
case "P06": this.add(13.00 * quantity); break;
case "P07": this.add(18.50 * quantity); break;
case "P08": this.add(21.00 * quantity); break;
case "P09": this.add(15.65 * quantity); break;
case "P10": this.add(11.00 * quantity); break;
}
return true;
},
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
applyDiscount : function(discount){
this.total -= this.total*(discount.discountPercent / 100);
}
};

cashRegister.scan('P01',1);
cashRegister.scan('P04',1);
cashRegister.scan('P10',3);

cashRegister.applyDiscount(discount);

console.log('Your bill is $'+cashRegister.total.toFixed(2));

Philip M
06-03-2014, 10:31 AM
var discount = new Discount("Discount",5);
var bigDiscount = new Discount("BIGDiscount",10);

That is not Javascript syntax.

This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

Dormilich
06-03-2014, 01:47 PM
@Philipp: looks perfectly like JS to me. and object creation via new is still valid JS (though not always the best choice).

Philip M
06-03-2014, 01:49 PM
@Philipp: looks perfectly like JS to me. and object creation via new is still valid JS (though not always the best choice).

Oh, you tell me something! I guess I have not used object creation with new.

It would help if the OP indicated the errors he is getting.

Dormilich
06-03-2014, 03:00 PM
without error messages there is hardly anything to say.

besides that, some notes regarding the code:

- I find it quite odd to use an object for a mere discount value, esp. since you can’t seemingly do more with it than with the bare value.

- scan() contains a lot of repetitive code. a simple "config" would make that extensible and easier to manage:


var cashRegister = {
// …
prices: {
P01: 16.50,
P02: 14.75,
// …
},
scan: function (item, quantity) {
// explicitly naming the Singleton instance saves you from
// surprises by context mutation ("this" is not as fixed as you may think)
if (!cashRegister.prices.hasOwnProperty(item)) {
// throw new Error("There is no such Item " + item);
return cashRegister.total;
}
quantity = quantity || 0;
cashRegister.add(cashRegister.prices[item] * quantity);
// always returning true doesn’t tell you anything
return cashRegister.total;
}
};