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