Advertisement
MartinGeorgiev

Vacantioner

Mar 13th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vacationer {
  2.  
  3.     constructor(fullName, creditCard) {
  4.         this.fullName = fullName;
  5.         this.creditCard = creditCard;
  6.         this.wishList = [];
  7.         this.idNumber = 0;
  8.     }
  9.     generateIDNumber() {
  10.         //231 * firstName’s first letter’s ASCII code + 139 * middleName length
  11.         let firstLetterAscii = (this.fullName.firstName.charAt(0)).charCodeAt(0);
  12.         let sum = (231 * firstLetterAscii) + (139 * this.fullName.middleName.length);
  13.         let lastLetter = this.fullName.lastName.charAt(this.fullName.lastName.length - 1)
  14.         let last = '7'
  15.         if (lastLetter === 'a' || lastLetter === 'u' || lastLetter === 'o' || lastLetter === 'i' || lastLetter === 'e') {
  16.             last = '8';
  17.         }
  18.         this._idNumber = sum + last;
  19.     }
  20.  
  21.     get idNumber() {
  22.  
  23.  
  24.         return this._idNumber;
  25.     }
  26.     set idNumber(value) {
  27.         this.generateIDNumber()
  28.     }
  29.  
  30.     get fullName() {
  31.         return this._fullName;
  32.     }
  33.     set fullName(value) {
  34.         let firstName = value[0];
  35.         let middleName = value[1];
  36.         let lastName = value[2];
  37.         let pattern = /^[A-Z][a-z]+$/;
  38.         if (value.length != 3) {
  39.             throw new Error("Name must include first name, middle name and last name");
  40.         }
  41.         if (!(pattern.test(firstName) && pattern.test(middleName) && pattern.test(lastName))) {
  42.             throw new Error("Invalid full name");
  43.         }
  44.  
  45.         this._fullName = {
  46.             firstName,
  47.             middleName,
  48.             lastName,
  49.         }
  50.     }
  51.     get creditCard() {
  52.         return this._creditCard;
  53.     }
  54.     set creditCard(value) {
  55.         if (value !== undefined) {
  56.             if (value.length !== 3) {
  57.                 throw new Error("Missing credit card information")
  58.             }
  59.             let cardNumber = value[0];
  60.             let expirationDate = value[1];
  61.             let securityNumber = value[2];
  62.             if (typeof (cardNumber) !== 'number' || typeof (securityNumber) !== 'number') {
  63.                 throw new Error("Invalid credit card details");
  64.             }
  65.             this._creditCard = {
  66.                 cardNumber,
  67.                 expirationDate,
  68.                 securityNumber,
  69.             }
  70.         } else {
  71.  
  72.             this._creditCard = {
  73.                 cardNumber: 1111,
  74.                 expirationDate: "",
  75.                 securityNumber: 111,
  76.             }
  77.         }
  78.     }
  79.  
  80.     addDestinationToWishList(destination) {
  81.         if (this.wishList.includes(destination)) {
  82.             throw new Error("Destination already exists in wishlist");
  83.         }
  84.         this.wishList.push(destination);
  85.         this.wishList.sort((a, b) => a.length - b.length);
  86.     }
  87.     addCreditCardInfo(creditCard) {
  88.         this.creditCard = creditCard;
  89.     }
  90.     getVacationerInfo() {
  91.         let result = `Name: ${this.fullName.firstName} ${this.fullName.middleName} ${this.fullName.lastName}\nID Number: ${this.idNumber}\nWishlist:\n`;
  92.         if (this.wishList.length === 0) {
  93.             result += "empty";
  94.         } else {
  95.             result += this.wishList.join(", ");
  96.         }
  97.         result +=
  98.             `\nCredit Card:\nCard Number: ${this.creditCard.cardNumber}\nExpiration Date: ${this.creditCard.expirationDate}\nSecurity Number: ${this.creditCard.securityNumber}`
  99.         return result;
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement