Advertisement
Guest User

ahad

a guest
Mar 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. class PaymentProcessor {
  2. constructor (types,precision) {
  3. this.types=["service", "product", "other"];
  4. this.precision=2
  5. // let obj={id:"string", name:"string", type:"string", value:"Number"}
  6. this.prop=[]
  7.  
  8. }
  9.  
  10.  
  11. deletePayment(idss) {
  12. this.prop.forEach(element => {
  13. let err ='p';
  14. if (element.id===idss) {
  15. this.prop.pop(element)
  16. err='s';
  17. }
  18. throw new Error(`Entity with id: ${idss} does not exist!`)
  19. });
  20. if (err=='p') {
  21.  
  22. }
  23.  
  24. }
  25. get(ids) {
  26. let ret =''
  27. let name2 =''
  28. let typ=''
  29. let balance=0
  30. this.prop.forEach(element => {
  31. if(element.id===ids)
  32. {
  33. ret=ids
  34. name2=element.name
  35. typ=element.type
  36. balance=Number(element.value)
  37.  
  38. }
  39. });
  40. if (ret==='') {
  41. throw new Error(`Entity with id: ${ids} does not exist!`)
  42. }
  43. return `Details about payment ID: ${ret}\n- Name: ${name2}\n- Type: ${typ}- Value: ${balance.toFixed(this.precision)}`
  44. }
  45. registerPayment(id1,name1,type1,value) {
  46. this.prop.forEach(element => {
  47. if(element.id===id1)
  48. {
  49. throw new Error(`Entity with id: ${id1} exist!`)
  50. }
  51. });
  52. let num=Number(value)
  53. let obj = {id:`${id1}`, name: `${name1}`, type:`${type1}`, value:num}
  54.  
  55. this.prop.push( obj)
  56.  
  57. }
  58. toString() {
  59. let sum =0
  60. this.prop.forEach(element => {
  61. sum =sum+element.value
  62. });
  63.  
  64. let str = `Summary:\n- Payments: ${this.prop.length}\n- Balance: ${sum.toFixed(this.precision)}`
  65. return str
  66. }
  67. setOptions(op1,op2) {
  68. this.types=op1
  69. this.precision=op2
  70.  
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement