fork download
  1. function Hamburger(size, stuffing) {
  2.  
  3. this.sizesArr = [Hamburger.SIZE_BIG,
  4. Hamburger.SIZE_SMALL];
  5.  
  6. this.stuffingsArr = [Hamburger.STUFFING_CHEESE,
  7. Hamburger.STUFFING_SALAD,
  8. Hamburger.STUFFING_POTATOES];
  9.  
  10. this.toppingsArr = [Hamburger.TOPPING_SAUCE,
  11. Hamburger.TOPPING_MAYO];
  12.  
  13. this.validateSize(size);
  14. this.validateStuffing(stuffing);
  15.  
  16. this.size = size;
  17. this.stuffing = stuffing;
  18.  
  19. this.toppings = [];
  20.  
  21. this.pricesAndCalorics = {};
  22.  
  23. this.pricesAndCalorics[Hamburger.SIZE_BIG] = {price: 100, calories: 40};
  24. this.pricesAndCalorics[Hamburger.SIZE_SMALL] = {price: 50, calories: 20};
  25.  
  26. this.pricesAndCalorics[Hamburger.STUFFING_CHEESE] = {price: 10, calories: 20};
  27. this.pricesAndCalorics[Hamburger.STUFFING_SALAD] = {price: 20, calories: 5};
  28. this.pricesAndCalorics[Hamburger.STUFFING_POTATOES] = {price: 15, calories: 10};
  29.  
  30. this.pricesAndCalorics[Hamburger.TOPPING_SAUCE] = {price: 15, calories: 0};
  31. this.pricesAndCalorics[Hamburger.TOPPING_MAYO] = {price: 20, calories: 5};
  32. }
  33.  
  34. Hamburger.SIZE_BIG = "BIG";
  35. Hamburger.SIZE_SMALL = "SMALL";
  36.  
  37. Hamburger.STUFFING_CHEESE = "CHEESE";
  38. Hamburger.STUFFING_SALAD = "SALAD";
  39. Hamburger.STUFFING_POTATOES = "POTATOES";
  40.  
  41. Hamburger.TOPPING_SAUCE = "SAUCE";
  42. Hamburger.TOPPING_MAYO = "MAYO";
  43.  
  44. Hamburger.prototype.validateSize = function (size) {
  45.  
  46. if (this.sizesArr.indexOf(size) < 0) {
  47. throw new Error( size + " - неправильный размер" );
  48. }
  49. }
  50.  
  51. Hamburger.prototype.validateStuffing = function (stuffing) {
  52.  
  53. if (this.stuffingsArr.indexOf(stuffing) < 0) {
  54. throw new Error( stuffing + " - неправильная начинка" );
  55. }
  56. }
  57.  
  58. Hamburger.prototype.addTopping = function (topping) {
  59.  
  60. if (this.toppingsArr.indexOf(topping) < 0) {
  61. throw new Error( topping + " - неизвестная добавка");
  62. };
  63.  
  64. if (this.toppings.indexOf(topping) >= 0) {
  65. throw new Error("Одну и ту же добавку нельзя использовать больше одного раза!");
  66. };
  67.  
  68. this.toppings.push(topping);
  69. }
  70.  
  71. Hamburger.prototype.calculateCalories = function () {
  72.  
  73. var calories = 0;
  74. var arr = this.pricesAndCalorics;
  75. calories += arr[this.size].calories;
  76. calories += arr[this.stuffing].calories;
  77.  
  78. for (var i = 0; i < this.toppings.length; i++) {
  79. calories += arr[this.toppings[i]].calories;
  80. };
  81.  
  82. return calories;
  83. }
  84.  
  85. Hamburger.prototype.calculatePrice = function () {
  86.  
  87. var price = 0;
  88. var arr = this.pricesAndCalorics;
  89. price += arr[this.size].price;
  90. price += arr[this.stuffing].price;
  91.  
  92. for (var i = 0; i < this.toppings.length; i++) {
  93. price += arr[this.toppings[i]].price;
  94. };
  95.  
  96. return price;
  97. }
  98.  
  99. // маленький гамбургер с начинкой из сыра
  100. var hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_CHEESE);
  101. // добавка из майонеза
  102. hamburger.addTopping(Hamburger.TOPPING_MAYO);
  103. // спросим сколько там калорий
  104. console.log("Calories: %f", hamburger.calculateCalories());
  105. // сколько стоит
  106. console.log("Price: %f", hamburger.calculatePrice());
  107. // я тут передумал и решил добавить еще приправу
  108. hamburger.addTopping(Hamburger.TOPPING_SAUCE);
  109. // А сколько теперь стоит?
  110. console.log("Price with sauce: %f", hamburger.calculatePrice());
  111. console.log("Calories with sauce: %f", hamburger.calculateCalories());
Runtime error #stdin #stdout #stderr 0.53s 323200KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
js: uncaught JavaScript runtime exception: ReferenceError: "console" is not defined.