Transaction.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. class TransactionRecipe{
  2. constructor(recipe, quantity){
  3. this._recipe = recipe;
  4. this._quantity = quantity;
  5. }
  6. get recipe(){
  7. return this._recipe;
  8. }
  9. get quantity(){
  10. return this._quantity;
  11. }
  12. }
  13. class Transaction{
  14. constructor(id, date, recipes, parent){
  15. date = new Date(date);
  16. this._id = id;
  17. this._parent = parent;
  18. this._date = date;
  19. this._recipes = [];
  20. for(let i = 0; i < recipes.length; i++){
  21. for(let j = 0; j < parent.recipes.length; j++){
  22. if(recipes[i].recipe === parent.recipes[j].id){
  23. const transactionRecipe = new TransactionRecipe(
  24. parent.recipes[j],
  25. recipes[i].quantity
  26. )
  27. this._recipes.push(transactionRecipe);
  28. break;
  29. }
  30. }
  31. }
  32. }
  33. get id(){
  34. return this._id;
  35. }
  36. get parent(){
  37. return this._parent;
  38. }
  39. get date(){
  40. return this._date;
  41. }
  42. get recipes(){
  43. return this._recipes;
  44. }
  45. /*
  46. Gets the quantity for a given ingredient
  47. */
  48. getIngredientQuantity(ingredient){
  49. let quantity = 0;
  50. for(let i = 0; i < this._recipes.length; i++){
  51. const recipe = this._recipes[i].recipe;
  52. for(let j = 0; j < recipe.ingredients.length; j++){
  53. if(recipe.ingredients[j].ingredient === ingredient){
  54. quantity += recipe.ingredients[j].quantity * this._recipes[i].quantity;
  55. break;
  56. }
  57. }
  58. }
  59. return quantity;
  60. }
  61. }
  62. module.exports = Transaction;