Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Hotel {
- constructor (name, capacity) {
- this.name = name
- this.capacity = capacity
- this.bookings = []
- this.currentBookingNumber = 1
- this.rooms = {
- single: ~~(this.capacity * 0.5),
- double: ~~(this.capacity * 0.3),
- maisonette: ~~(this.capacity * 0.2)
- }
- }
- get roomsPricing () {
- return {
- single: 50,
- double: 90,
- maisonette: 135
- }
- }
- get servicesPricing () {
- return {
- food: 10,
- drink: 15,
- housekeeping: 25
- }
- }
- rentARoom (clientName, roomType, nights) {
- if (!this.rooms.hasOwnProperty(roomType) || this.rooms[roomType] <= 0) {
- let message = [`No ${roomType} rooms available!`];
- Object.entries(this.rooms).forEach(r => {
- if (r[1] > 0) {
- message.push(`Available ${r[0]} rooms: ${r[1]}.`);
- }
- })
- return message.join(' ');
- }
- let booking = {
- clientName,
- roomType,
- nights,
- roomNumber: this.currentBookingNumber++
- }
- this.bookings.push(booking)
- this.rooms[roomType]--
- return `Enjoy your time here Mr./Mrs. ${clientName}. ` +
- `Your booking is ${booking.roomNumber}.`
- }
- roomService (currentBookingNumber, serviceType) {
- let booking = this.bookings.find(b => b.roomNumber === currentBookingNumber)
- if (!booking) {
- return `The booking ${currentBookingNumber} is invalid.`;
- } else if (!this.servicesPricing.hasOwnProperty(serviceType)) {
- return `We do not offer ${serviceType} service.`;
- }
- if (!booking.hasOwnProperty('services')) {
- booking.services = []
- }
- booking.services.push(serviceType)
- return `Mr./Mrs. ${booking.clientName}, ` +
- `Your order for ${serviceType} service has been successful.`
- }
- checkOut(currentBookingNumber) {
- let index = this.bookings.findIndex(a => a.roomNumber === currentBookingNumber);
- if (index === -1) {
- return `The booking ${currentBookingNumber} is invalid.`;
- }
- let booking = this.bookings[index];
- this.bookings.splice(index, 1);
- this.rooms[booking.roomType]++;
- let totalMoney = booking.nights * this.roomsPricing[booking.roomType];
- if (!booking.services) {
- return `We hope you enjoyed your time here, Mr./Mrs. ${booking.clientName}. The total amount of money you have to pay is ${totalMoney} BGN.`;
- } else {
- let servicesPrice = 0;
- for(let service of booking.services) {
- servicesPrice += this.servicesPricing[service];
- }
- 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.`;
- }
- }
- report () {
- let data = `${this.name.toUpperCase()} DATABASE:\n${'-'.repeat(20)}\n`
- data += this.bookings.length === 0
- ? 'There are currently no bookings.'
- : this.bookings
- .map(b =>
- [
- `bookingNumber - ${b.roomNumber}`,
- `clientName - ${b.clientName}`,
- `roomType - ${b.roomType}`,
- `nights - ${b.nights}`,
- !b.hasOwnProperty('services')
- ? ''
- : `services: ${b.services.join(', ')}`,
- '-'.repeat(10)
- ]
- .filter(Boolean)
- .join('\n')
- )
- .join('\n')
- .slice(0, -11)
- return data
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement