emotrend

Untitled

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