Bill.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import Format from "../Format.js";
  2. export default class Bill{
  3. constructor(id, name, amount, active){
  4. this._id = id;
  5. this._name = name;
  6. this._amount = amount;
  7. this._active = active;
  8. }
  9. get id(){
  10. return this._id;
  11. }
  12. get name(){
  13. return this._name;
  14. }
  15. set name(v){
  16. this._name = v;
  17. }
  18. get amount(){
  19. return this._amount;
  20. }
  21. set amount(v){
  22. if(typeof v !== "string") v = Number(v);
  23. this._amount = Format.dollarsToCents(v);
  24. }
  25. get type(){
  26. return "Bill";
  27. }
  28. get active(){
  29. return this._active;
  30. }
  31. set active(v){
  32. if(typeof v !== "boolean") throw new TypeError("'value' requires a boolean");
  33. this._active = v;
  34. }
  35. static create(name, amount){
  36. return new Bill(
  37. crypto.randomUUID(),
  38. name,
  39. amount,
  40. true
  41. );
  42. }
  43. static fromObject(data){
  44. return new Bill(
  45. data.id,
  46. data.name,
  47. data.amount,
  48. data.active
  49. );
  50. }
  51. serialize(){
  52. return {
  53. id: this._id,
  54. name: this._name,
  55. amount: this._amount,
  56. active: this._active
  57. };
  58. }
  59. }