Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PaymentProcessor {
  2.     constructor(options) {
  3.         this.options = options;
  4.         this.payments = [];
  5.     }
  6.  
  7.     get options() {
  8.         return this._options;
  9.     }
  10.  
  11.     set options(options) {
  12.         if (options === undefined) {
  13.             options = {};
  14.         }
  15.  
  16.         if (this.options === undefined) {
  17.             this._options = {};
  18.         }
  19.  
  20.         this.setOptions(options);
  21.     }
  22.  
  23.     registerPayment(id, name, type, value) {
  24.         if (id === '' || name === '' || !Number(value) || !this.options['types'].includes(type)) {
  25.             throw new Error('Invalid payment!');
  26.         }
  27.  
  28.         if (this.payments.some(x => x.id === id)) {
  29.             throw new Error('Payment exists!');
  30.         }
  31.  
  32.         let payment = {id, name, type, value};
  33.         this.payments.push(payment);
  34.     }
  35.  
  36.     deletePayment(id) {
  37.         if (this.payments.every(x => x.id !== id)) {
  38.             throw new Error('Payment does not exist!');
  39.         }
  40.  
  41.         this.payments = this.payments.filter(x => x.id !== id);
  42.     }
  43.  
  44.     get(id) {
  45.         if (this.payments.every(x => x.id !== id)) {
  46.             throw new Error('Payment does not exist!');
  47.         }
  48.  
  49.         let payment =  this.payments.filter(x => x.id === id)[0];
  50.         let result = `Details about payment ID: ${payment.id}\n`;
  51.         result += `- Name: ${payment.name}\n`;
  52.         result += `- Type: ${payment.type}\n`;
  53.         result += `- Value: ${payment.value.toFixed(this.options['precision'])}`;
  54.  
  55.         return result;
  56.     }
  57.  
  58.     setOptions(options) {
  59.         if (options['types'] === undefined && options['precision'] === undefined) {
  60.             this._options['types'] = ['service', 'product', 'other'];
  61.             this._options['precision'] = 2;
  62.         } else if (options['types'] === undefined && options['precision'] !== undefined) {
  63.             this._options['types'] = ['service', 'product', 'other'];
  64.             this._options['precision'] = options['precision'];
  65.         } else if (options['types'] !== undefined && options['precision'] === undefined) {
  66.             this._options['types'] = options['types'];
  67.             this._options['precision'] = 2;
  68.         } else {
  69.             this._options['types'] = options['types'];
  70.             this._options['precision'] = options['precision'];
  71.         }
  72.     }
  73.  
  74.     toString() {
  75.         let result = 'Summary:\n';
  76.         result += `- Payments: ${this.payments.length}\n`;
  77.         let balance = 0;
  78.  
  79.         for (let payment of this.payments) {
  80.             balance += payment.value;
  81.         }
  82.  
  83.         result += `- Balance: ${balance.toFixed(this.options['precision'])}`;
  84.  
  85.         return result;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement