fork download
  1. /**
  2.  * Класс, объекты которого описывают параметры гамбургера.
  3.  *
  4.  * @constructor
  5.  * @param size Размер
  6.  * @param stuffing Начинка
  7.  * @throws {HamburgerException} При неправильном использовании
  8.  */
  9. function Hamburger(size, stuffing) {
  10. this._setSize(size);
  11. this._setStuffing(stuffing);
  12. this._toppings = [];
  13. }
  14.  
  15. /* Размеры, виды начинок и добавок */
  16. Hamburger.SIZE_SMALL = "small";
  17. Hamburger.SIZE_LARGE = "large";
  18. Hamburger.STUFFING_CHEESE = "cheese";
  19. Hamburger.STUFFING_SALAD = "salad";
  20. Hamburger.STUFFING_POTATO = "potato";
  21. Hamburger.TOPPING_MAYO = "mayo";
  22. Hamburger.TOPPING_SPICE = "spice";
  23.  
  24. Hamburger.sizeList = {};
  25. Hamburger.sizeList[Hamburger.SIZE_SMALL] = {money: 50, calories: 20};
  26. Hamburger.sizeList[Hamburger.SIZE_LARGE] = {money: 100, calories: 40};
  27.  
  28. Hamburger.stuffingList = {};
  29. Hamburger.stuffingList[Hamburger.STUFFING_CHEESE] = {money: 10, calories: 20};
  30. Hamburger.stuffingList[Hamburger.STUFFING_SALAD] = {money: 20, calories: 5};
  31. Hamburger.stuffingList[Hamburger.STUFFING_POTATO] = {money: 15, calories: 10};
  32.  
  33. Hamburger.toppingList = {};
  34. Hamburger.toppingList[Hamburger.TOPPING_MAYO] = {money: 20, calories: 5};
  35. Hamburger.toppingList[Hamburger.TOPPING_SPICE] = {money: 15, calories: 0};
  36.  
  37. Hamburger.prototype._setSize = function(size) {
  38. if (size === undefined) {
  39. throw new HamburgerException("Do not set the undefined size.");
  40. } else if (!(size in Hamburger.sizeList)) {
  41. throw new HamburgerException("Wrong size: " + size + ". " +
  42. "Allowed sizes: " + Object.keys(Hamburger.sizeList).join(", ") + ".");
  43. }
  44. this._size = size;
  45. };
  46.  
  47. Hamburger.prototype._setStuffing = function(stuffing) {
  48. if (stuffing === undefined) {
  49. throw new HamburgerException("Do not set the undefined stuffing.");
  50. } else if (!(stuffing in Hamburger.stuffingList)) {
  51. throw new HamburgerException("Wrong stuffing: " + stuffing + ". " +
  52. "Allowed filling: " + Object.keys(Hamburger.stuffingList).join(", ") + ".");
  53. }
  54. this._stuffing = stuffing;
  55. };
  56.  
  57. /**
  58.  * Добавить добавку к гамбургеру. Можно добавить несколько
  59.  * добавок, при условии, что они разные.
  60.  *
  61.  * @param topping Тип добавки
  62.  * @throws {HamburgerException} При неправильном использовании
  63.  */
  64. Hamburger.prototype.addTopping = function(topping) {
  65. if (topping === undefined) {
  66. throw new HamburgerException("Do not set the undefined topping.");
  67. } else if (this._toppings.indexOf(topping) > -1) {
  68. throw new HamburgerException("You can not add the topping twice " + topping + ".");
  69. } else if (!(topping in Hamburger.toppingList)) {
  70. throw new HamburgerException("Wrong topping: " + topping + ". " +
  71. "Allowed topping: " + Object.keys(Hamburger.toppingList).join(", ") + ".");
  72. }
  73. this._toppings.push(topping);
  74. };
  75.  
  76. /**
  77.  * Убрать добавку, при условии, что она ранее была
  78.  * добавлена.
  79.  *
  80.  * @param topping Тип добавки
  81.  * @throws {HamburgerException} При неправильном использовании
  82.  */
  83. Hamburger.prototype.removeTopping = function(topping) {
  84. var index = this._toppings.indexOf(topping);
  85. if (index == -1) {
  86. throw new HamburgerException(topping + " topping " + "doesn't exist.");
  87. }
  88. this._toppings.splice(index, 1);
  89. };
  90.  
  91. /**
  92.  * Получить список добавок.
  93.  *
  94.  * @return {Array} Массив добавленных добавок, содержит константы
  95.  * Hamburger.TOPPING_*
  96.  */
  97. Hamburger.prototype.getToppings = function() {
  98. return this._toppings;
  99. };
  100.  
  101. /**
  102.  * Узнать размер гамбургера
  103.  */
  104. Hamburger.prototype.getSize = function() {
  105. return this._size;
  106. };
  107.  
  108. /**
  109.  * Узнать начинку гамбургера
  110.  */
  111. Hamburger.prototype.getStuffing = function() {
  112. return this._stuffing;
  113. };
  114.  
  115. /**
  116.  * Узнать цену гамбургера
  117.  * @return {Number} Цена в тугриках
  118.  */
  119. Hamburger.prototype.getPrice = function() {
  120. var money = Hamburger.sizeList[this._size].money +
  121. Hamburger.stuffingList[this._stuffing].money;
  122. if (this._toppings.length != 0) {
  123. for (var i = 0; i < this._toppings.length; i++) {
  124. money += Hamburger.toppingList[this._toppings[i]].money;
  125. }
  126. }
  127. return money;
  128. };
  129.  
  130. /**
  131.  * Узнать калорийность
  132.  * @return {Number} Калорийность в калориях
  133.  */
  134. Hamburger.prototype.getCalories = function() {
  135. var calories = Hamburger.sizeList[this._size].calories +
  136. Hamburger.stuffingList[this._stuffing].calories;
  137. if (this._toppings.length != 0) {
  138. for (var i = 0; i < this._toppings.length; i++) {
  139. calories += Hamburger.toppingList[this._toppings[i]].calories;
  140. }
  141. }
  142. return calories;
  143. };
  144.  
  145. /**
  146.  * Представляет информацию об ошибке в ходе работы с гамбургером.
  147.  * Подробности хранятся в свойстве message.
  148.  * @constructor
  149.  */
  150. function HamburgerException (message) {
  151. this.name = "HamburgerException";
  152. this.message = message;
  153. }
  154.  
  155. // маленький гамбургер с начинкой из сыра
  156. var hamburger = new Hamburger(Hamburger.SIZE_SMALL, Hamburger.STUFFING_CHEESE);
  157. // добавка из майонеза
  158. hamburger.addTopping(Hamburger.TOPPING_MAYO);
  159. // спросим сколько там калорий
  160. console.log("Calories: %f", hamburger.getCalories());
  161. // сколько стоит
  162. console.log("Price: %f", hamburger.getPrice());
  163. // я тут передумал и решил добавить еще приправу
  164. hamburger.addTopping(Hamburger.TOPPING_SPICE);
  165. // А сколько теперь стоит?
  166. console.log("Price with sauce: %f", hamburger.getPrice());
  167.  
  168. var errorSituations = [
  169. function() { hamburger.addTopping(Hamburger.TOPPING_MAYO); },
  170. function() { hamburger.addTopping(Hamburger.TOPPING_CONDIMENT); },
  171. function() { hamburger.addTopping(Hamburger.TOPPING_INVALID); },
  172. function() { hamburger.removeTopping(Hamburger.SIZE_LARGE)},
  173. function() { new Hamburger(Hamburger.SIZE_LARGE); },
  174. function() { new Hamburger(); },
  175. function() { new Hamburger(Hamburger.TOPPING_MAYO, Hamburger.TOPPING_SPICE); },
  176. function() { new Hamburger({}); },
  177. function() { new Hamburger({price: 100}); },
  178. function() { new Hamburger(Hamburger.SIZE_VERY_SMALL); }
  179. ];
  180.  
  181. for (var i = 0; i < errorSituations.length; i++) {
  182. try {
  183. errorSituations[i]();
  184. } catch(e) {
  185. console.log("Error situation #" + (i + 1));
  186. console.log(e.name + ": " + e.message);
  187. }
  188. }
  189.  
Success #stdin #stdout 0.06s 21168KB
stdin
Standard input is empty
stdout
Calories: %f 45
Price: %f 80
Price with sauce: %f 95
Error situation #1
HamburgerException: You can not add the topping twice mayo.
Error situation #2
HamburgerException: Do not set the undefined topping.
Error situation #3
HamburgerException: Do not set the undefined topping.
Error situation #4
HamburgerException: large topping doesn't exist.
Error situation #5
HamburgerException: Do not set the undefined stuffing.
Error situation #6
HamburgerException: Do not set the undefined size.
Error situation #7
HamburgerException: Wrong size: mayo. Allowed sizes: small, large.
Error situation #8
HamburgerException: Wrong size: [object Object]. Allowed sizes: small, large.
Error situation #9
HamburgerException: Wrong size: [object Object]. Allowed sizes: small, large.
Error situation #10
HamburgerException: Do not set the undefined size.