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