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";
function Hamburger(size, stuffing) {
this.topping_sause = 0;
this.topping_mayo = 0;
this.validateSize = function (size) {
if (size != Hamburger.SIZE_BIG &&
size != Hamburger.SIZE_SMALL) {
throw new HamburgerError( size + " неправильный размер" );
}
}
if (stuffing != Hamburger.STUFFING_CHEESE &&
stuffing != Hamburger.STUFFING_SALAD &&
stuffing != Hamburger.STUFFING_POTATOES) {
throw new HamburgerError( stuffing + " неправильная начинка" );
}
Hamburger.prototype.addTopping = function (topping) {
if (topping == Hamburger.TOPPING_SAUCE) {
this.topping_sause++;
}
if (topping == Hamburger.TOPPING_MAYO) {
this.topping_mayo++;
}
if (this.topping_sause >= 2) {
this.topping_sause = 1;
throw new HamburgerError("Приправа добавлена более 1 раза");
}
if (this.topping_mayo >= 2) {
this.topping_mayo = 1;
throw new HamburgerError("Майонезом полито более 1 раза");
}
if (topping != Hamburger.TOPPING_SAUCE &&
topping != Hamburger.TOPPING_MAYO) {
throw new HamburgerError( topping + " неизвестная добавка");
}
}
Hamburger.prototype.calculateCalories = function () {
var calories = 0;
if (size == Hamburger.SIZE_BIG) {
calories += 40;
}
if (size == Hamburger.SIZE_SMALL) {
calories += 20;
}
if (stuffing == Hamburger.STUFFING_CHEESE) {
calories += 20;
}
if (stuffing == Hamburger.STUFFING_SALAD) {
calories += 5;
}
if (stuffing == Hamburger.STUFFING_POTATOES) {
calories += 10;
}
if (this.topping_sause == 1) {
calories += 0;
}
if (this.topping_mayo == 1) {
calories += 5;
}
return calories;
}
Hamburger.prototype.calculatePrice = function () {
var price = 0;
if (size == Hamburger.SIZE_BIG) {
price += 100;
}
if (size == Hamburger.SIZE_SMALL) {
price += 50;
}
if (stuffing == Hamburger.STUFFING_CHEESE) {
price += 10;
}
if (stuffing == Hamburger.STUFFING_SALAD) {
price += 20;
}
if (stuffing == Hamburger.STUFFING_POTATOES) {
price += 15;
}
if (this.topping_sause == 1) {
price += 15;
}
if (this.topping_mayo == 1) {
price += 20;
}
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());