Advertisement
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;
- }
- generateIDNumber() {
- //231 * firstName’s first letter’s ASCII code + 139 * middleName length
- let firstLetterAscii = (this.fullName.firstName.charAt(0)).charCodeAt(0);
- let sum = (231 * firstLetterAscii) + (139 * this.fullName.middleName.length);
- let lastLetter = this.fullName.lastName.charAt(this.fullName.lastName.length - 1)
- let last = '7'
- if (lastLetter === 'a' || lastLetter === 'u' || lastLetter === 'o' || lastLetter === 'i' || lastLetter === 'e') {
- last = '8';
- }
- this._idNumber = sum + last;
- }
- get idNumber() {
- return this._idNumber;
- }
- set idNumber(value) {
- this.generateIDNumber()
- }
- get fullName() {
- return this._fullName;
- }
- set fullName(value) {
- let firstName = value[0];
- let middleName = value[1];
- let lastName = value[2];
- let pattern = /^[A-Z][a-z]+$/;
- if (value.length != 3) {
- throw new Error("Name must include first name, middle name and last name");
- }
- if (!(pattern.test(firstName) && pattern.test(middleName) && pattern.test(lastName))) {
- throw new Error("Invalid full name");
- }
- this._fullName = {
- firstName,
- middleName,
- lastName,
- }
- }
- get creditCard() {
- return this._creditCard;
- }
- set creditCard(value) {
- if (value !== undefined) {
- if (value.length !== 3) {
- throw new Error("Missing credit card information")
- }
- let cardNumber = value[0];
- let expirationDate = value[1];
- let securityNumber = value[2];
- if (typeof (cardNumber) !== 'number' || typeof (securityNumber) !== 'number') {
- throw new Error("Invalid credit card details");
- }
- this._creditCard = {
- cardNumber,
- expirationDate,
- securityNumber,
- }
- } else {
- this._creditCard = {
- cardNumber: 1111,
- expirationDate: "",
- securityNumber: 111,
- }
- }
- }
- addDestinationToWishList(destination) {
- if (this.wishList.includes(destination)) {
- throw new Error("Destination already exists in wishlist");
- }
- this.wishList.push(destination);
- this.wishList.sort((a, b) => a.length - b.length);
- }
- addCreditCardInfo(creditCard) {
- this.creditCard = creditCard;
- }
- getVacationerInfo() {
- let result = `Name: ${this.fullName.firstName} ${this.fullName.middleName} ${this.fullName.lastName}\nID Number: ${this.idNumber}\nWishlist:\n`;
- if (this.wishList.length === 0) {
- result += "empty";
- } else {
- result += this.wishList.join(", ");
- }
- result +=
- `\nCredit Card:\nCard Number: ${this.creditCard.cardNumber}\nExpiration Date: ${this.creditCard.expirationDate}\nSecurity Number: ${this.creditCard.securityNumber}`
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement