Hamburger.SIZE_SMALL = "small";
Hamburger.SIZE_BIG = "big";
Hamburger.STUFFING_CHEESE = "cheese";
Hamburger.STUFFING_SALAD = "salad";
Hamburger.STUFFING_POTATO = "potato";
Hamburger.TOPPING_SAUCE = "sauce";
Hamburger.TOPPING_MAYO = "mayo";
function Hamburger(size, stuffing) {
this._setSize(size);
this._setStuffing(stuffing);
this._toppings = {};
}
Hamburger.sizeList = {
"small": {money: 50, calories: 20},
"big": {money: 100, calories: 40}
};
Hamburger.stuffingList = {
"cheese": {money: 10, calories: 20},
"salad": {money: 20, calories: 5},
"potato": {money: 15, calories: 10}
};
Hamburger.toppingList = {
"sauce": {money: 15, calories: 0},
"mayo": {money: 20, calories: 5}
};
Hamburger.prototype._setSize = function(size) {
if (size === undefined) {
throw new HamburgerException("Не задан размер.");
} else if (!(size in Hamburger.sizeList)) {
throw new HamburgerException("Неправильный размер " + size + "." +
" Разрешены размеры: " + Object.keys(Hamburger.sizeList) + ".");
}
this._size = Hamburger.sizeList[size];
};
Hamburger.prototype._setStuffing = function(stuffing) {
if (stuffing === undefined) {
throw new HamburgerException("Не задана начинка.");
} else if (!(stuffing in Hamburger.stuffingList)) {
throw new HamburgerException("Неправильная начинка " + stuffing + "." +
" Разрешены начинки: " + Object.keys(Hamburger.stuffingList) + ".");
}
this._stuffing = Hamburger.stuffingList[stuffing];
};
Hamburger.prototype._setTopping = function(topping) {
if (!(topping in Hamburger.toppingList)) {
throw new HamburgerException("Неправильная добавка " + topping + "." +
" Разрешены добавки: " + Object.keys(Hamburger.toppingList) + ".");
} else if (topping in this._toppings) {
throw new HamburgerException("Нельзя два раза добавить добавку " + topping + ".");
}
this._toppings[topping] = Hamburger.toppingList[topping];
};
Hamburger.prototype.addTopping = function(topping) {
this._setTopping(topping);
};
Hamburger.prototype.calculateCalories = function() {
var calories = this._size.calories + this._stuffing.calories;
if (this._toppings.length != 0) {
for (var topping in this._toppings) {
calories += this._toppings[topping].calories;
}
}
return calories;
};
Hamburger.prototype.calculatePrice = function() {
var money = this._size.money + this._stuffing.money;
if (this._toppings.length != 0) {
for (var topping in this._toppings) {
money += this._toppings[topping].money;
}
}
return money;
};
function HamburgerException(errorMessage) {
this.name = "HamburgerException";
this.message = errorMessage;
}
// маленький гамбургер с начинкой из сыра
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());
var errorSituations = [
function() {new Hamburger(Hamburger.SIZE_BIG); },
function() {new Hamburger(); },
function() {new Hamburger(Hamburger.TOPPING_MAYO, Hamburger.TOPPING_SAUCE); },
function() { hamburger.addTopping(Hamburger.TOPPING_MAYO); },
function() { hamburger.addTopping(Hamburger.TOPPING_CONDIMENT); },
function() { hamburger.addTopping(Hamburger.TOPPING_INVALID); },
function() { new Hamburger({}); },
function() { new Hamburger({price: 100}); },
function() { new Hamburger(Hamburger.SIZE_VERY_SMALL); }
];
for (var i = 0; i < errorSituations.length; i++) {
try {
errorSituations[i]();
} catch(e) {
console.log(e.name + ": " + e.message);
}
}