function Hamburger(size, stuffing) {
this.createTypesArrays();
this.validateSize(size);
this.validateStuffing(stuffing);
this.size = size;
this.stuffing = stuffing;
this.toppings = [];
this.createTablePricesAndCalories();
}
Hamburger.prototype.createTypesArrays = function () {
this.sizesTypes = [Hamburger.SIZE_BIG,
Hamburger.SIZE_SMALL];
this.stuffingsTypes = [Hamburger.STUFFING_CHEESE,
Hamburger.STUFFING_SALAD,
Hamburger.STUFFING_POTATOES];
this.toppingsTypes = [Hamburger.TOPPING_SAUCE,
Hamburger.TOPPING_MAYO];
}
Hamburger.prototype.createTablePricesAndCalories = function () {
this.pricesAndCalories = {};
var p = this.pricesAndCalories;
p[Hamburger.SIZE_BIG] = {price: 100, calories: 40};
p[Hamburger.SIZE_SMALL] = {price: 50, calories: 20};
p[Hamburger.STUFFING_CHEESE] = {price: 10, calories: 20};
p[Hamburger.STUFFING_SALAD] = {price: 20, calories: 5};
p[Hamburger.STUFFING_POTATOES] = {price: 15, calories: 10};
p[Hamburger.TOPPING_SAUCE] = {price: 15, calories: 0};
p[Hamburger.TOPPING_MAYO] = {price: 20, calories: 5};
}
Hamburger.SIZE_BIG = "BIG";
Hamburger.SIZE_SMALL = "SMALL";
Hamburger.STUFFING_CHEESE = "CHEESE";
Hamburger.STUFFING_SALAD = "SALAD";
Hamburger.STUFFING_POTATOES = "POTATOES";
Hamburger.TOPPING_SAUCE = "SAUCE";
Hamburger.TOPPING_MAYO = "MAYO";
Hamburger.prototype.validateSize = function (size) {
if (this.sizesTypes.indexOf(size) < 0) {
throw new Error( size + " - неправильный размер" );
}
}
Hamburger.prototype.validateStuffing = function (stuffing) {
if (this.stuffingsTypes.indexOf(stuffing) < 0) {
throw new Error( stuffing + " - неправильная начинка" );
}
}
Hamburger.prototype.addTopping = function (topping) {
if (this.toppingsTypes.indexOf(topping) < 0) {
throw new Error( topping + " - неизвестная добавка");
};
if (this.toppings.indexOf(topping) >= 0) {
throw new Error("Одну и ту же добавку нельзя использовать больше одного раза!");
};
this.toppings.push(topping);
}
Hamburger.prototype.calculateCalories = function () {
var calories = 0;
var arr = this.pricesAndCalories;
calories += arr[this.size].calories;
calories += arr[this.stuffing].calories;
for (var i = 0; i < this.toppings.length; i++) {
calories += arr[this.toppings[i]].calories;
};
return calories;
}
Hamburger.prototype.calculatePrice = function () {
var price = 0;
var arr = this.pricesAndCalories;
price += arr[this.size].price;
price += arr[this.stuffing].price;
for (var i = 0; i < this.toppings.length; i++) {
price += arr[this.toppings[i]].price;
};
return price;
}
// маленький гамбургер с начинкой из сыра
var hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_CHEESE);
// добавка из майонеза
hamburger.addTopping(Hamburger.TOPPING_MAYO);
// спросим сколько там калорий
console.log("Calories: %f", hamburger.calculateCalories());
// сколько стоит
console.log("Price: %f", hamburger.calculatePrice());
// я тут передумал и решил добавить еще приправу
hamburger.addTopping(Hamburger.TOPPING_SAUCE);
// А сколько теперь стоит?
console.log("Price with sauce: %f", hamburger.calculatePrice());
console.log("Calories with sauce: %f", hamburger.calculateCalories());