fork download
  1. Hamburger.SIZE_SMALL = 'SMALL';
  2. Hamburger.SIZE_BIG = 'BIG';
  3. Hamburger.STUFFING_CHEESE = 'CHEESE';
  4. Hamburger.STUFFING_SALADE = 'SALADE';
  5. Hamburger.STUFFING_POTATO = 'POTATO';
  6. Hamburger.TOPPING_MAYO = 'MAYO';
  7. Hamburger.TOPPING_CONDIMENT = 'CONDIMENT';
  8.  
  9. // Значения "констант" не влияют на работу Hamburger, можно раскоментировать и проверить
  10. /*
  11. Hamburger.SIZE_SMALL = 1;
  12. Hamburger.SIZE_BIG = 2;
  13. Hamburger.STUFFING_CHEESE = 3;
  14. Hamburger.STUFFING_SALADE = 4;
  15. Hamburger.STUFFING_POTATO = 5;
  16. Hamburger.TOPPING_MAYO = 6;
  17. Hamburger.TOPPING_CONDIMENT = 7;*/
  18.  
  19. Hamburger._sizeList = [];
  20. Hamburger._sizeList[Hamburger.SIZE_SMALL] = { price: 50, calories: 20 };
  21. Hamburger._sizeList[Hamburger.SIZE_BIG] = { price: 100, calories: 40 };
  22.  
  23. Hamburger._stuffingList = [];
  24. Hamburger._stuffingList[Hamburger.STUFFING_CHEESE] = { price: 10, calories: 20 };
  25. Hamburger._stuffingList[Hamburger.STUFFING_SALADE] = { price: 20, calories: 5 };
  26. Hamburger._stuffingList[Hamburger.STUFFING_POTATO] = { price: 15, calories: 10 };
  27.  
  28. Hamburger._toppingList = [];
  29. Hamburger._toppingList[Hamburger.TOPPING_MAYO] = { price: 20, calories: 5 };
  30. Hamburger._toppingList[Hamburger.TOPPING_CONDIMENT] = { price: 15, calories: 0 };
  31.  
  32. function Hamburger(size, stuffing) {
  33. if (arguments.length === 0) {
  34. throw new HamburgerException('No size given');
  35. }
  36.  
  37. this._toppingsSelected = [];
  38. this._setSize(size);
  39. this._setStuffing(stuffing);
  40. }
  41.  
  42. Hamburger.prototype._setSize = function (sizeConst) {
  43. var size = Hamburger._sizeList[sizeConst];
  44.  
  45. if (size === undefined) {
  46. throw new HamburgerException('Invalid size. Only ' + Object.keys(Hamburger._sizeList).join(', ') + ' are available.');
  47. }
  48.  
  49. this._size = size;
  50. }
  51.  
  52. Hamburger.prototype._setStuffing = function (stuffingConst) {
  53. var stuffing = Hamburger._stuffingList[stuffingConst];
  54.  
  55. if (stuffing === undefined) {
  56. throw new HamburgerException('Invalid stuffing. Only '+ Object.keys(Hamburger._stuffingList).join(', ') + ' are available.');
  57. }
  58.  
  59. this._stuffing = stuffing;
  60. }
  61.  
  62. Hamburger.prototype.addTopping = function (toppingConst) {
  63. var topping = Hamburger._toppingList[toppingConst];
  64.  
  65. if (topping === undefined) {
  66. throw new HamburgerException('Invalid topping. Only ' + Object.keys(Hamburger._toppingList).join(', ') + ' are available.');
  67. } else if (this._toppingsSelected[toppingConst]) {
  68. throw new HamburgerException('Duplicate topping: ' + toppingConst);
  69. }
  70.  
  71. this._toppingsSelected[toppingConst] = topping;
  72. }
  73.  
  74. Hamburger.prototype.calculatePrice = function () {
  75. var toppingsTotalPrice = Hamburger._calcSumOfProperties('price', this._toppingsSelected);
  76.  
  77. return this._size.price + this._stuffing.price + toppingsTotalPrice;
  78. }
  79.  
  80. Hamburger.prototype.calculateCalories = function () {
  81. var toppingsTotalCalories = Hamburger._calcSumOfProperties('calories', this._toppingsSelected);
  82.  
  83. return this._size.calories + this._stuffing.calories + toppingsTotalCalories;
  84. }
  85.  
  86. Hamburger._calcSumOfProperties = function (property, consts) {
  87. var sum = 0;
  88.  
  89. for (var constName in consts) {
  90. sum += consts[constName][property];
  91. }
  92.  
  93. return sum;
  94. }
  95.  
  96. function HamburgerException(errorMessage) {
  97. this.name = 'HamburgerException';
  98. this.message = errorMessage;
  99. }
  100.  
  101. // Тесты
  102. var hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_SALADE);
  103. console.log(70 === hamburger.calculatePrice());
  104. console.log(25 === hamburger.calculateCalories());
  105. hamburger.addTopping(Hamburger.TOPPING_MAYO);
  106. console.log(30 === hamburger.calculateCalories());
  107. hamburger.addTopping(Hamburger.TOPPING_CONDIMENT);
  108. console.log(105 === hamburger.calculatePrice());
  109.  
  110. var hamburgerExceptionProvider = [
  111. function() { hamburger.addTopping(Hamburger.TOPPING_MAYO); },
  112. function() { hamburger.addTopping(Hamburger.TOPPING_CONDIMENT); },
  113. function() { hamburger.addTopping(Hamburger.TOPPING_INVALID); },
  114. function() { new Hamburger(Hamburger.SIZE_SMALL); },
  115. function() { new Hamburger(); },
  116. function() { new Hamburger({}); },
  117. function() { new Hamburger({price: 100}); },
  118. function() { new Hamburger(Hamburger.SIZE_VERY_SMALL); },
  119. ];
  120. for (var i = 0; i < hamburgerExceptionProvider.length; i++) {
  121. try {
  122. hamburgerExceptionProvider[i]();
  123. } catch (e) {
  124. if (e instanceof HamburgerException) {
  125. // Всё ок, переходим к следующему коллбеку
  126. console.log('HamburgerException: ' + e.message);
  127. continue;
  128. } else {
  129. // Проброс исключения дальше
  130. throw e;
  131. }
  132. }
  133. // После вызова коллбека исключение HamburgerException не было выброшено
  134. throw new Error('HamburgerException was not thrown.');
  135. }
  136.  
Success #stdin #stdout 0.06s 21168KB
stdin
Standard input is empty
stdout
true
true
true
true
HamburgerException: Duplicate topping: MAYO
HamburgerException: Duplicate topping: CONDIMENT
HamburgerException: Invalid topping. Only MAYO, CONDIMENT are available.
HamburgerException: Invalid stuffing. Only CHEESE, SALADE, POTATO are available.
HamburgerException: No size given
HamburgerException: Invalid size. Only SMALL, BIG are available.
HamburgerException: Invalid size. Only SMALL, BIG are available.
HamburgerException: Invalid size. Only SMALL, BIG are available.