Advertisement
Liliana797979

3_Vacationer - js advanced exam

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