Recipe.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. class RecipeIngredient{
  2. constructor(ingredient, quantity){
  3. if(quantity < 0){
  4. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  5. return false;
  6. }
  7. this._ingredient = ingredient;
  8. this._quantity = this.convertToBase(quantity);
  9. }
  10. get ingredient(){
  11. return this._ingredient;
  12. }
  13. get quantity(){
  14. switch(unit){
  15. case "g":return this._quantity;
  16. case "kg": return this._quantity * 1000;
  17. case "oz": return this._quantity * 28.3495;
  18. case "lb": return this._quantity * 453.5924;
  19. case "ml": return this._quantity / 1000;
  20. case "l": return this._quantity;
  21. case "tsp": return this._quantity / 202.8842;
  22. case "tbsp": return this._quantity / 67.6278;
  23. case "ozfl": return this._quantity / 33.8141;
  24. case "cup": return this._quantity / 4.1667;
  25. case "pt": return this._quantity / 2.1134;
  26. case "qt": return this._quantity / 1.0567;
  27. case "gal": return this._quantity * 3.7854;
  28. case "mm": return this._quantity / 1000;
  29. case "cm": return this._quantity / 100;
  30. case "m": return this._quantity;
  31. case "in": return this._quantity / 39.3701;
  32. case "ft": return this._quantity / 3.2808;
  33. default: return this._quantity;
  34. }
  35. }
  36. set quantity(quantity){
  37. if(quantity < 0){
  38. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  39. return false;
  40. }
  41. this_quantity = this.convertToBase(quantity);
  42. }
  43. convertToBase(quantity){
  44. switch(this._ingredient.unit){
  45. case "g": return quantity;
  46. case "kg": return quantity / 1000;
  47. case "oz": return quantity / 28.3495;
  48. case "lb": return quantity / 453.5924;
  49. case "ml": return quantity *= 1000;
  50. case "l": return quantity;
  51. case "tsp": return quantity * 202.8842;
  52. case "tbsp": return quantity * 67.6278;
  53. case "ozfl": return quantity * 33.8141;
  54. case "cup": return quantity * 4.1667;
  55. case "pt": return quantity * 2.1134;
  56. case "qt": return quantity * 1.0567;
  57. case "gal": return quantity / 3.7854;
  58. case "mm": return quantity * 1000;
  59. case "cm": return quantity * 100;
  60. case "m": return quantity;
  61. case "in": return quantity * 39.3701;
  62. case "ft": return quantity * 3.2808;
  63. default: return quantity;
  64. }
  65. }
  66. }
  67. class Recipe{
  68. constructor(id, name, price, ingredients, parent){
  69. if(price < 0){
  70. banner.createError("PRICE CANNOT BE A NEGATIVE NUMBER");
  71. return false;
  72. }
  73. if(!controller.sanitaryString(name)){
  74. banner.createError("NAME CONTAINS ILLEGAL CHARACTERS");
  75. return false;
  76. }
  77. this._id = id;
  78. this._name = name;
  79. this._price = price;
  80. this._parent = parent;
  81. this._ingredients = [];
  82. for(let i = 0; i < ingredients.length; i++){
  83. const recipeIngredient = new RecipeIngredient(
  84. ingredients[i].ingredient,
  85. ingredients[i].quantity
  86. );
  87. this._ingredients.push(recipeIngredient);
  88. }
  89. this._parent.modules.recipeBook.isPopulated = false;
  90. this._parent.modules.analytics.isPopulated = false;
  91. this._parent.modules.recipeBook.display();
  92. }
  93. get id(){
  94. return this._id;
  95. }
  96. get name(){
  97. return this._name;
  98. }
  99. set name(name){
  100. if(!controller.sanitaryString(name)){
  101. return false;
  102. }
  103. this._name = name;
  104. }
  105. get price(){
  106. return this._price;
  107. }
  108. set price(price){
  109. if(price < 0){
  110. return false;
  111. }
  112. this._price = price;
  113. }
  114. get parent(){
  115. return this._parent;
  116. }
  117. get ingredients(){
  118. return this._ingredients;
  119. }
  120. addIngredient(ingredient, quantity){
  121. if(quantity < 0){
  122. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  123. return false;
  124. }
  125. let recipeIngredient = new RecipeIngredient(ingredient, quantity);
  126. this._ingredients.push(recipeIngredient);
  127. this._parent.modules.recipeBook.isPopulated = false;
  128. this._parent.modules.analytics.isPopulated = false;
  129. this._parent.modules.recipeBook.display();
  130. }
  131. removeIngredient(ingredient){
  132. const index = this._ingredients.indexOf(ingredient);
  133. this._ingredients.splice(index, 1);
  134. }
  135. updateIngredient(ingredient, quantity){
  136. if(quantity < 0){
  137. banner.createError("QUANTITY CANNOT BE A NEGATIVE NUMBER");
  138. return false;
  139. }
  140. const index = this._ingredients.indoxOf(ingredient);
  141. this._ingredients[index].quantity = quantity;
  142. }
  143. }
  144. module.exports = Recipe;