Advertisement
AhmetUstun

12. Payment Package (CODE)

Jul 2nd, 2021
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PaymentPackage {
  2.    
  3.     constructor(name, value) {
  4.         this.name = name;
  5.         this.value = value;
  6.         this.VAT = 20;      // Default value    
  7.         this.active = true; // Default value
  8.     }
  9.  
  10.     get name() {
  11.         return this._name;
  12.     }
  13.  
  14.     set name(newValue) {
  15.         if (typeof newValue !== 'string') {
  16.             throw new Error('Name must be a non-empty string');
  17.         }
  18.         if (newValue.length === 0) {
  19.             throw new Error('Name must be a non-empty string');
  20.         }
  21.         this._name = newValue;
  22.     }
  23.  
  24.     get value() {
  25.         return this._value;
  26.     }
  27.  
  28.     set value(newValue) {
  29.         if (typeof newValue !== 'number') {
  30.             throw new Error('Value must be a non-negative number');
  31.         }
  32.         if (newValue < 0) {
  33.             throw new Error('Value must be a non-negative number');
  34.         }
  35.         this._value = newValue;
  36.     }
  37.  
  38.     get VAT() {
  39.         return this._VAT;
  40.     }
  41.  
  42.     set VAT(newValue) {
  43.         if (typeof newValue !== 'number') {
  44.             throw new Error('VAT must be a non-negative number');
  45.         }
  46.         if (newValue < 0) {
  47.             throw new Error('VAT must be a non-negative number');
  48.         }
  49.         this._VAT = newValue;
  50.     }
  51.  
  52.     get active() {
  53.         return this._active;
  54.     }
  55.  
  56.     set active(newValue) {
  57.         if (typeof newValue !== 'boolean') {
  58.             throw new Error('Active status must be a boolean');
  59.         }
  60.         this._active = newValue;
  61.     }
  62.  
  63.     toString() {
  64.         const output = [
  65.             `Package: ${this.name}` + (this.active === false ? ' (inactive)' : ''),
  66.             `- Value (excl. VAT): ${this.value}`,
  67.             `- Value (VAT ${this.VAT}%): ${this.value * (1 + this.VAT / 100)}`
  68.         ];
  69.         return output.join('\n');
  70.     }
  71. }
  72.  
  73. module.exports = PaymentPackage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement