Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PaymentProcessor {
- constructor(options) {
- this.options = options;
- this.payments = [];
- }
- get options() {
- return this._options;
- }
- set options(options) {
- if (options === undefined) {
- options = {};
- }
- if (this.options === undefined) {
- this._options = {};
- }
- this.setOptions(options);
- }
- registerPayment(id, name, type, value) {
- if (id === '' || name === '' || !Number(value) || !this.options['types'].includes(type)) {
- throw new Error('Invalid payment!');
- }
- if (this.payments.some(x => x.id === id)) {
- throw new Error('Payment exists!');
- }
- let payment = {id, name, type, value};
- this.payments.push(payment);
- }
- deletePayment(id) {
- if (this.payments.every(x => x.id !== id)) {
- throw new Error('Payment does not exist!');
- }
- this.payments = this.payments.filter(x => x.id !== id);
- }
- get(id) {
- if (this.payments.every(x => x.id !== id)) {
- throw new Error('Payment does not exist!');
- }
- let payment = this.payments.filter(x => x.id === id)[0];
- let result = `Details about payment ID: ${payment.id}\n`;
- result += `- Name: ${payment.name}\n`;
- result += `- Type: ${payment.type}\n`;
- result += `- Value: ${payment.value.toFixed(this.options['precision'])}`;
- return result;
- }
- setOptions(options) {
- if (options['types'] === undefined && options['precision'] === undefined) {
- this._options['types'] = ['service', 'product', 'other'];
- this._options['precision'] = 2;
- } else if (options['types'] === undefined && options['precision'] !== undefined) {
- this._options['types'] = ['service', 'product', 'other'];
- this._options['precision'] = options['precision'];
- } else if (options['types'] !== undefined && options['precision'] === undefined) {
- this._options['types'] = options['types'];
- this._options['precision'] = 2;
- } else {
- this._options['types'] = options['types'];
- this._options['precision'] = options['precision'];
- }
- }
- toString() {
- let result = 'Summary:\n';
- result += `- Payments: ${this.payments.length}\n`;
- let balance = 0;
- for (let payment of this.payments) {
- balance += payment.value;
- }
- result += `- Balance: ${balance.toFixed(this.options['precision'])}`;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement