Hamburger.SIZE_SMALL = 'SMALL';
Hamburger.SIZE_BIG = 'BIG';
Hamburger.STUFFING_CHEESE = 'CHEESE';
Hamburger.STUFFING_SALADE = 'SALADE';
Hamburger.STUFFING_POTATO = 'POTATO';
Hamburger.TOPPING_MAYO = 'MAYO';
Hamburger.TOPPING_CONDIMENT = 'CONDIMENT';
// Значения "констант" не влияют на работу Hamburger, можно раскоментировать и проверить
/*
Hamburger.SIZE_SMALL = 1;
Hamburger.SIZE_BIG = 2;
Hamburger.STUFFING_CHEESE = 3;
Hamburger.STUFFING_SALADE = 4;
Hamburger.STUFFING_POTATO = 5;
Hamburger.TOPPING_MAYO = 6;
Hamburger.TOPPING_CONDIMENT = 7;*/
Hamburger._sizeList = [];
Hamburger._sizeList[Hamburger.SIZE_SMALL] = { price: 50, calories: 20 };
Hamburger._sizeList[Hamburger.SIZE_BIG] = { price: 100, calories: 40 };
Hamburger._stuffingList = [];
Hamburger._stuffingList[Hamburger.STUFFING_CHEESE] = { price: 10, calories: 20 };
Hamburger._stuffingList[Hamburger.STUFFING_SALADE] = { price: 20, calories: 5 };
Hamburger._stuffingList[Hamburger.STUFFING_POTATO] = { price: 15, calories: 10 };
Hamburger._toppingList = [];
Hamburger._toppingList[Hamburger.TOPPING_MAYO] = { price: 20, calories: 5 };
Hamburger._toppingList[Hamburger.TOPPING_CONDIMENT] = { price: 15, calories: 0 };
function Hamburger(size, stuffing) {
if (arguments.length === 0) {
throw new HamburgerException('No size given');
}
this._toppingsSelected = [];
this._setSize(size);
this._setStuffing(stuffing);
}
Hamburger.prototype._setSize = function (sizeConst) {
var size = Hamburger._sizeList[sizeConst];
if (size === undefined) {
throw new HamburgerException('Invalid size. Only ' + Object.keys(Hamburger._sizeList).join(', ') + ' are available.');
}
this._size = size;
}
Hamburger.prototype._setStuffing = function (stuffingConst) {
var stuffing = Hamburger._stuffingList[stuffingConst];
if (stuffing === undefined) {
throw new HamburgerException('Invalid stuffing. Only '+ Object.keys(Hamburger._stuffingList).join(', ') + ' are available.');
}
this._stuffing = stuffing;
}
Hamburger.prototype.addTopping = function (toppingConst) {
var topping = Hamburger._toppingList[toppingConst];
if (topping === undefined) {
throw new HamburgerException('Invalid topping. Only ' + Object.keys(Hamburger._toppingList).join(', ') + ' are available.');
} else if (this._toppingsSelected[toppingConst]) {
throw new HamburgerException('Duplicate topping: ' + toppingConst);
}
this._toppingsSelected[toppingConst] = topping;
}
Hamburger.prototype.calculatePrice = function () {
var toppingsTotalPrice = Hamburger._calcSumOfProperties('price', this._toppingsSelected);
return this._size.price + this._stuffing.price + toppingsTotalPrice;
}
Hamburger.prototype.calculateCalories = function () {
var toppingsTotalCalories = Hamburger._calcSumOfProperties('calories', this._toppingsSelected);
return this._size.calories + this._stuffing.calories + toppingsTotalCalories;
}
Hamburger._calcSumOfProperties = function (property, consts) {
var sum = 0;
for (var constName in consts) {
sum += consts[constName][property];
}
return sum;
}
function HamburgerException(errorMessage) {
this.name = 'HamburgerException';
this.message = errorMessage;
}
// Тесты
var hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_SALADE);
console.log(70 === hamburger.calculatePrice());
console.log(25 === hamburger.calculateCalories());
hamburger.addTopping(Hamburger.TOPPING_MAYO);
console.log(30 === hamburger.calculateCalories());
hamburger.addTopping(Hamburger.TOPPING_CONDIMENT);
console.log(105 === hamburger.calculatePrice());
var hamburgerExceptionProvider = [
function() { hamburger.addTopping(Hamburger.TOPPING_MAYO); },
function() { hamburger.addTopping(Hamburger.TOPPING_CONDIMENT); },
function() { hamburger.addTopping(Hamburger.TOPPING_INVALID); },
function() { new Hamburger(Hamburger.SIZE_SMALL); },
function() { new Hamburger(); },
function() { new Hamburger({}); },
function() { new Hamburger({price: 100}); },
function() { new Hamburger(Hamburger.SIZE_VERY_SMALL); },
];
for (var i = 0; i < hamburgerExceptionProvider.length; i++) {
try {
hamburgerExceptionProvider[i]();
} catch (e) {
if (e instanceof HamburgerException) {
// Всё ок, переходим к следующему коллбеку
console.log('HamburgerException: ' + e.message);
continue;
} else {
// Проброс исключения дальше
throw e;
}
}
// После вызова коллбека исключение HamburgerException не было выброшено
throw new Error('HamburgerException was not thrown.');
}