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