Transaction.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. class TransactionRecipe{
  2. constructor(recipe, quantity){
  3. if(quantity < 0){
  4. controller.createBanner("QUANTITY CANNOT BE A NEGATIVE NUMBER", "error");
  5. return false;
  6. }
  7. if(quantity % 1 !== 0){
  8. controller.createBanner("RECIPES WITHIN A TRANSACTION MUST BE WHOLE NUMBERS", "error");
  9. return false;
  10. }
  11. this._recipe = recipe;
  12. this._quantity = quantity;
  13. }
  14. get recipe(){
  15. return this._recipe;
  16. }
  17. get quantity(){
  18. return this._quantity;
  19. }
  20. }
  21. class Transaction{
  22. constructor(id, date, recipes, parent){
  23. date = new Date(date);
  24. if(date > new Date()){
  25. controller.createBanner("DATE CANNOT BE SET TO THE FUTURE", "error");
  26. return false;
  27. }
  28. this._id = id;
  29. this._parent = parent;
  30. this._date = date;
  31. this._recipes = [];
  32. for(let i = 0; i < recipes.length; i++){
  33. for(let j = 0; j < parent.recipes.length; j++){
  34. if(recipes[i].recipe === parent.recipes[j].id){
  35. const transactionRecipe = new TransactionRecipe(
  36. parent.recipes[j],
  37. recipes[i].quantity
  38. )
  39. this._recipes.push(transactionRecipe);
  40. break;
  41. }
  42. }
  43. }
  44. }
  45. get id(){
  46. return this._id;
  47. }
  48. get parent(){
  49. return this._parent;
  50. }
  51. get date(){
  52. return this._date;
  53. }
  54. get recipes(){
  55. return this._recipes;
  56. }
  57. /*
  58. Gets the quantity for a given ingredient
  59. */
  60. getIngredientQuantity(ingredient){
  61. let quantity = 0;
  62. for(let i = 0; i < this._recipes.length; i++){
  63. const recipe = this._recipes[i].recipe;
  64. for(let j = 0; j < recipe.ingredients.length; j++){
  65. if(recipe.ingredients[j].ingredient === ingredient){
  66. quantity += recipe.ingredients[j].quantity * this._recipes[i].quantity;
  67. }
  68. }
  69. }
  70. return quantity;
  71. }
  72. }
  73. module.exports = Transaction;