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