Advertisement
nikolayneykov92

Untitled

Jun 28th, 2019
456
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: ~~(this.capacity * 0.5),
  9.       double: ~~(this.capacity * 0.3),
  10.       maisonette: ~~(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.   rentARoom (clientName, roomType, nights) {
  31.     if (!this.rooms.hasOwnProperty(roomType) || this.rooms[roomType] <= 0) {
  32.       let message = [`No ${roomType} rooms available!`];
  33.       Object.entries(this.rooms).forEach(r => {
  34.         if (r[1] > 0) {
  35.           message.push(`Available ${r[0]} rooms: ${r[1]}.`);
  36.         }
  37.       })
  38.  
  39.       return message.join(' ');
  40.     }
  41.  
  42.     let booking = {
  43.       clientName,
  44.       roomType,
  45.       nights,
  46.       roomNumber: this.currentBookingNumber++
  47.     }
  48.  
  49.     this.bookings.push(booking)
  50.     this.rooms[roomType]--
  51.  
  52.     return `Enjoy your time here Mr./Mrs. ${clientName}. ` +
  53.       `Your booking is ${booking.roomNumber}.`
  54.   }
  55.  
  56.   roomService (currentBookingNumber, serviceType) {
  57.     let booking = this.bookings.find(b => b.roomNumber === currentBookingNumber)
  58.  
  59.     if (!booking) {
  60.       return `The booking ${currentBookingNumber} is invalid.`;
  61.     } else if (!this.servicesPricing.hasOwnProperty(serviceType)) {
  62.       return `We do not offer ${serviceType} service.`;
  63.     }
  64.  
  65.     if (!booking.hasOwnProperty('services')) {
  66.       booking.services = []
  67.     }
  68.  
  69.     booking.services.push(serviceType)
  70.  
  71.     return `Mr./Mrs. ${booking.clientName}, ` +
  72.       `Your order for ${serviceType} service has been successful.`
  73.   }
  74.  
  75.    checkOut(currentBookingNumber) {
  76.     let index = this.bookings.findIndex(a => a.roomNumber === currentBookingNumber);
  77.     if (index === -1) {
  78.         return `The booking ${currentBookingNumber} is invalid.`;
  79.     }
  80.  
  81.     let booking = this.bookings[index];
  82.     this.bookings.splice(index, 1);
  83.  
  84.     this.rooms[booking.roomType]++;
  85.     let totalMoney = booking.nights * this.roomsPricing[booking.roomType];
  86.  
  87.     if (!booking.services) {
  88.         return `We hope you enjoyed your time here, Mr./Mrs. ${booking.clientName}. The total amount of money you have to pay is ${totalMoney} BGN.`;
  89.     } else {
  90.         let servicesPrice = 0;
  91.         for(let service of booking.services) {
  92.             servicesPrice += this.servicesPricing[service];
  93.         }
  94.  
  95.         return `We hope you enjoyed your time here, Mr./Mrs. ${booking.clientName}. The total amount of money you have to pay is ${totalMoney + servicesPrice} BGN. You have used additional room services, costing ${servicesPrice} BGN.`;
  96.     }
  97. }
  98.  
  99.  
  100.   report () {
  101.     let data = `${this.name.toUpperCase()} DATABASE:\n${'-'.repeat(20)}\n`
  102.     data += this.bookings.length === 0
  103.       ? 'There are currently no bookings.'
  104.       : this.bookings
  105.         .map(b =>
  106.           [
  107.             `bookingNumber - ${b.roomNumber}`,
  108.             `clientName - ${b.clientName}`,
  109.             `roomType - ${b.roomType}`,
  110.             `nights - ${b.nights}`,
  111.             !b.hasOwnProperty('services')
  112.               ? ''
  113.               : `services: ${b.services.join(', ')}`,
  114.             '-'.repeat(10)
  115.           ]
  116.             .filter(Boolean)
  117.             .join('\n')
  118.         )
  119.         .join('\n')
  120.         .slice(0, -11)
  121.  
  122.     return data
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement