Advertisement
ShadowEmbrace

hotel

Apr 17th, 2019
964
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.  
  8.         this.availableRooms = {
  9.             single: Math.floor(this.capacity * 0.5),
  10.             double: Math.floor(this.capacity * 0.3),
  11.             maisonette: Math.floor(this.capacity * 0.2)
  12.         };
  13.         this.roomsPricing = {
  14.             single: 50,
  15.             double: 90,
  16.             maisonette: 135
  17.         };
  18.         this.servicesPricing = {
  19.             food: 10,
  20.             drink: 15,
  21.             housekeeping: 25
  22.         };
  23.     }
  24.  
  25.     rentARoom(clientName, roomType, nights) {
  26.         if (this.availableRooms[roomType] <= 0) {
  27.             let output = [];
  28.             output.push(`No ${roomType} rooms available!`);
  29.             let keys = Object.keys(this.availableRooms).filter(x => this.availableRooms[x] > 0);
  30.  
  31.             for (const room of keys) {
  32.                 output.push(`Available ${room} rooms: ${this.availableRooms[room]}.`);
  33.             }
  34.  
  35.             return output.join(' ');
  36.         }
  37.  
  38.         let output = `Enjoy your time here Mr./Mrs. ${clientName}. Your booking is ${this.currentBookingNumber}.`;
  39.         let obj = {
  40.             clientName,
  41.             roomType,
  42.             nights,
  43.             currentBookingNumber: this.currentBookingNumber
  44.         };
  45.         this.bookings.push(obj);
  46.         this.currentBookingNumber += 1;
  47.         this.availableRooms[roomType] -= 1;
  48.  
  49.         return output;
  50.     }
  51.  
  52.     roomService(currentBookingNumber, serviceType) {
  53.         let currentRoom = this.bookings.filter(x => x.currentBookingNumber === currentBookingNumber);
  54.  
  55.         if (currentRoom.length === 0) {
  56.             return `The booking ${currentBookingNumber} is invalid.`;
  57.         }
  58.  
  59.         if (!this.servicesPricing.hasOwnProperty(serviceType)) {
  60.             return `We do not offer ${serviceType} service.`;
  61.         }
  62.  
  63.         if (!currentRoom[0].hasOwnProperty('services')) {
  64.             currentRoom[0]['services'] = [];
  65.         }
  66.         currentRoom[0]['services'].push(serviceType);
  67.         return `Mr./Mrs. ${currentRoom[0]['clientName']}, Your order for ${serviceType} service has been successful.`;
  68.     }
  69.  
  70.     checkOut(currentBookingNumber) {
  71.         let currentRoom = this.bookings.filter(x => x.currentBookingNumber === currentBookingNumber)[0];
  72.         if (!currentRoom) {
  73.             return `The booking ${currentBookingNumber} is invalid.`;
  74.         }
  75.  
  76.         let roomType = currentRoom['roomType'];
  77.         this.availableRooms[roomType] += 1;
  78.         this.bookings = this.bookings.filter(x => x.currentBookingNumber !== currentBookingNumber);
  79.  
  80.         let totalMoney = this.roomsPricing[roomType] * currentRoom['nights'];
  81.         if (!currentRoom['services']) {
  82.             return `We hope you enjoyed your time here, Mr./Mrs. ${currentRoom['clientName']}. The total amount of money you have to pay is ${totalMoney} BGN.`
  83.         }
  84.         let totalServiceMoney = 0;
  85.         for (let service of currentRoom['services']) {
  86.             totalServiceMoney += this.servicesPricing[service];
  87.         }
  88.         return `We hope you enjoyed your time here, Mr./Mrs. ${currentRoom['clientName']}. The total amount of money you have to pay is ${totalMoney + totalServiceMoney} BGN. You have used additional room services, costing ${totalServiceMoney} BGN.`;
  89.     }
  90.  
  91.     report() {
  92.         let output = [];
  93.         output.push(`${this.name.toUpperCase()} DATABASE:`);
  94.         output.push(`--------------------`);
  95.  
  96.         if (this.bookings.length === 0) {
  97.             output.push(`There are currently no bookings.`);
  98.  
  99.             return output.join('\n');
  100.         }
  101.  
  102.         let midOutput = [];
  103.         for (const room of this.bookings) {
  104.             let curr = [];
  105.             curr.push(`bookingNumber - ${room['currentBookingNumber']}`);
  106.             curr.push(`clientName - ${room['clientName']}`);
  107.             curr.push(`roomType - ${room['roomType']}`);
  108.             curr.push(`nights - ${room['nights']}`);
  109.  
  110.             if (room['services']) {
  111.                 let server = [];
  112.                 for (const serv of room['services']) {
  113.                     server.push(serv);
  114.                 }
  115.                 curr.push('services: ' + server.join(', '));
  116.             }
  117.             midOutput.push(curr.join('\n'));
  118.         }
  119.         output.push(midOutput.join(`\n${'-'.repeat(10)}\n`));
  120.  
  121.         return output.join('\n').trim();
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement