Advertisement
Todorov_Stanimir

03. Hotel JS Advanced Exam - 17 March 2019

Oct 21st, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Hotel {
  2.     constructor(name, capacity) {
  3.         this.name = name;
  4.         this.capacity = capacity;
  5.         this.bookings = [];
  6.         this.currentBookingNumber = 1;
  7.         this.rooms = {
  8.             single: Math.round(+this.capacity / 2),
  9.             double: Math.round(+this.capacity * 0.3),
  10.             maisonette: Math.round(+this.capacity * 0.2),
  11.         };
  12.     }
  13.  
  14.     get roomsPricing() {
  15.         return {
  16.             single: 50,
  17.             double: 90,
  18.             maisonette: 135,
  19.         }
  20.     }
  21.  
  22.     get servicesPricing() {
  23.         return {
  24.             food: 10,
  25.             drink: 15,
  26.             housekeeping: 25,
  27.         };
  28.     }
  29.  
  30.     isAvailableRoom(roomType) {
  31.         return this.rooms[roomType] > 0;
  32.     }
  33.  
  34.     decreaseRoom(room) {
  35.         this.rooms[room] -= 1;
  36.     }
  37.  
  38.     increaseRoom(room) {
  39.         this.rooms[room] += 1;
  40.     }
  41.  
  42.     rentARoom(clientName, roomType, nights) {
  43.         if (this.isAvailableRoom(roomType)) {
  44.  
  45.             let clientBooking = {
  46.                 clientName,
  47.                 roomType,
  48.                 nights: +nights,
  49.                 roomNumber: this.currentBookingNumber
  50.             }
  51.             this.bookings.push(clientBooking);
  52.             this.currentBookingNumber += 1;
  53.             this.decreaseRoom(roomType);
  54.             return `Enjoy your time here Mr./Mrs. ${clientName}. Your booking is ${this.currentBookingNumber - 1}.`
  55.         } else {
  56.             let output = `No ${roomType} rooms available!`;
  57.             for (const room in this.rooms) {
  58.                 if (this.rooms.hasOwnProperty(room)) {
  59.                     if (this.isAvailableRoom(room)) {
  60.                         output += ` Available ${room} rooms: ${this.rooms[room]}.`;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             return output;
  66.         }
  67.     }
  68.     roomService = (currentBookingNumber, serviceType) => {
  69.         let booking = this.bookings.find(b => b.roomNumber === currentBookingNumber);
  70.         if (!booking) {
  71.             return `The booking ${currentBookingNumber} is invalid.`
  72.         }
  73.         if (!this.servicesPricing.hasOwnProperty(serviceType)) {
  74.             return `We do not offer ${serviceType} service.`;
  75.         }
  76.         if (!(booking.hasOwnProperty('services'))) {
  77.             booking['services'] = [];
  78.         }
  79.         booking.services.push(serviceType);
  80.         return `Mr./Mrs. ${booking.clientName}, Your order for ${serviceType} service has been successful.`;
  81.     }
  82.     checkOut = (currentBookingNumber) => {
  83.         const index = this.bookings.findIndex(function (i) {
  84.             return i.roomNumber === currentBookingNumber;
  85.         });
  86.         if (index === -1) {
  87.             return `The booking ${currentBookingNumber} is invalid.`
  88.         }
  89.         let booking = this.bookings.splice(+index, 1)[0];
  90.  
  91.         this.increaseRoom(booking.roomType);
  92.  
  93.         let totalMoney = this.roomsPricing[booking.roomType] * booking.nights;
  94.  
  95.         if (!booking.hasOwnProperty('services')) {
  96.             return `We hope you enjoyed your time here, Mr./Mrs. ${booking.clientName}. The total amount of money you have to pay is ${totalMoney} BGN.`
  97.         }
  98.  
  99.         let totalServiceMoney = booking.services.reduce((a, service) => a + this.servicesPricing[service], 0)
  100.         return `We hope you enjoyed your time here, Mr./Mrs. ${booking.clientName}. The total amount of money you have to pay is ${totalMoney + totalServiceMoney} BGN. You have used additional room services, costing ${totalServiceMoney} BGN.`
  101.     }
  102.  
  103.     report = () => {
  104.         if (this.bookings.length === 0) {
  105.             return `${this.name.toUpperCase()} DATABASE:\n--------------------\nThere are currently no bookings.`;
  106.         } else {
  107.             let output = `${this.name.toUpperCase()} DATABASE:\n--------------------\n`;
  108.             let bookingsArray = [];
  109.             for (const booking of this.bookings) {
  110.                 bookingsArray.push(`bookingNumber - ${booking.roomNumber}\nclientName - ${booking.clientName}\nroomType - ${booking.roomType}\nnights - ${booking.nights}${booking.hasOwnProperty('services') ? '\nservices: ' + booking.services.join(', ') : ''}`);
  111.             }
  112.             output += bookingsArray.join('\n----------\n');
  113.             return output.trim();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement