Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vacationer {
- constructor(fullName, creditCard) {
- this.fullName = fullName;
- this.creditCard = creditCard;
- this.wishList = [];
- this.idNumber = 0;
- }
- set creditCard(card) {
- this._creditCard = this.addCreditCardInfo(card);
- }
- get creditCard() {
- return this._creditCard;
- }
- set idNumber(num) {
- this._idNumber = this.generateIDNumber();
- }
- get idNumber() {
- return this._idNumber;
- }
- set fullName(value) {
- if (value.length !== 3) {
- throw new Error('Name must include first name, middle name and last name');
- }
- let firstName = value[0];
- let middleName = value[1];
- let lastName = value[2];
- let pattern = /^[A-Z][a-z]+$/;
- if (!(pattern.test(firstName) && pattern.test(middleName) && pattern.test(lastName))) {
- throw new Error('Invalid full name');
- }
- this._fullName = {
- firstName,
- middleName,
- lastName
- };
- }
- get fullName() {
- return this._fullName;
- }
- addCreditCardInfo(value) {
- if (value === undefined) {
- return {
- cardNumber: 1111,
- expirationDate: '',
- securityNumber: 111
- };
- } else {
- if (value.length !== 3) {
- throw new Error('Missing credit card information');
- } else {
- if (typeof value[0] !== 'number' || typeof value[2] !== 'number') {
- throw new Error('Invalid credit card details');
- } else {
- let cardNumber = value[0];
- let expirationDate = value[1];
- let securityNumber = value[2];
- return {
- cardNumber,
- expirationDate,
- securityNumber
- };
- }
- }
- }
- }
- addDestinationToWishList(destination) {
- if (this.wishList.includes(destination)) {
- throw new Error('Destination already exists in wishlist');
- }
- return this.wishList.push(destination);
- }
- generateIDNumber() {
- let firstNameFirstLetterAscii = this._fullName.firstName.charAt(0).charCodeAt(0);
- let middleNameLength = this._fullName.middleName.length;
- let lastNameLetter = this._fullName.lastName.charAt(this._fullName.lastName.length - 1);
- let sum = 231 * firstNameFirstLetterAscii + 139 * middleNameLength;
- let add = "7";
- if (lastNameLetter === 'a' || lastNameLetter === 'e' || lastNameLetter === 'o' || lastNameLetter === 'i' || lastNameLetter === 'u') {
- add = "8";
- }
- return sum + add;
- }
- getVacationerInfo() {
- let destinations = this.wishList.sort((a, b) => a.length - b.length).join(', ');
- if (this.wishList.length === 0) {
- destinations = 'empty';
- }
- 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}`;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment