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