In: Computer Science
C++
CLASSES
1. Define a class date that has day, month and year data members.
2. Define a class reservation with following data members:
- An integer counter that generates reservation numbers.
- An integer as a reservation number. The counter is incremented each time a reservation object is created. This value will be assigned as the reservation number.
- Number of beds.
- Reservation date from class date.
3. Define a class rooms with data members:
- Room number
- Number of beds (1 or 2).
- A 2D int array (12 by 30) that show the availability of the room on each day of the year. The rows are the months and columns are days of month. If the room in available on a specific date, the array element value is zero. If the room has been reserved, the array element value has the reservation number.
4. Define a manager class with data member:
- A variable motel_size that indicates the number of rooms of motel.
- An array of pointers to rooms.
- An array of pointers to guests.
Member Functions:
- Function that processes a reservation received as parameter. If unsuccessful the function returns zero. If the reservation is successful (if the a room with requested # of beds is available during the required dates) the function returns reservation number.
- Function that receives reservation number as parameter and outputs the details of reservation.
- Function that receives reservation number and date as parameters and cancels reservation.
- Function that dictates the availability of single and double rooms on a required date.
5. Main function:
Creates manager, and reservation objects. This function calls the manager member function to process reservations.
#include <iostream> #include "Reservation.cpp" int main() { Manager m; reservation r; r.reservationNo = 2322; r.reservationDate.day=1; r.reservationDate.month =2; r.numberOfBeds =2; m.reserve(&r); m.printDetails(r.reservationNo,std::cout); std::cout<<"\nIs room available:"<<(m.isAvailable(2,r.reservationDate)>0?"true":"false"); }
#include <ostream> class date { public: int day; int month; int year; friend std::ostream &operator<<(std::ostream &os, const date &date) { os << "day: " << date.day << " month: " << date.month << " year: " << date.year; return os; } }; class reservation { public: int countReservationNos; int reservationNo; int numberOfBeds; date reservationDate; friend std::ostream &operator<<(std::ostream &os, const reservation &reservation) { os << "countReservationNos: " << reservation.countReservationNos << " reservationNo: " << reservation.reservationNo << " numberOfBeds: " << reservation.numberOfBeds << " reservationDate: " << reservation.reservationDate; return os; } }; class room { public: int roomNp; int noOfBeds; int availability[12][30]; friend std::ostream &operator<<(std::ostream &os, const room &room) { os << "roomNp: " << room.roomNp << " noOfBeds: " << room.noOfBeds << " availability: " << "True"; return os; } room() { noOfBeds = 2;//by default for (int i = 0; i < 12; ++i) { for (int j = 0; j < 30; ++j) { availability[i][j] = 0; } } } room(int beds) { if (beds == 1 || beds == 2) { noOfBeds = beds; } else { beds = 2;//default } for (int i = 0; i < 12; ++i) { for (int j = 0; j < 30; ++j) { availability[i][j] = 0; } } } }; class guest { char *name; }; class Manager { public: // int emptyRoomNo = 0; int motel_size=10; room *rooms; guest *guests; Manager() { rooms = new room[motel_size]; } Manager(int noOfRooms) { rooms = new room[noOfRooms]; motel_size = noOfRooms; } int reserve(reservation *reserva) { bool reserved=false; for (int i = 0; i < motel_size; ++i) { if((rooms+i)->availability[reserva->reservationDate.day][reserva->reservationDate.month]==0){ (rooms+i)->availability[reserva->reservationDate.day][reserva->reservationDate.month] = reserva->reservationNo; return true; } } return reserved; } bool isAvailable(int beds, date dat) { for (int i = 0; i < motel_size; ++i) { if ((rooms + i)->availability[dat.day][dat.month] == 0 && (rooms + i)->noOfBeds >= beds) { return true; } } return false; } bool cancelReservaton(int resNo, date dat) { for (int k = 0; k < motel_size; ++k) { if ((rooms + k)->availability[dat.day][dat.month] == resNo) { (rooms + k)->availability[dat.day][dat.month]=0; return true; } } return false; } void printDetails(int resNo,std::ostream& temp){ for (int k = 0; k < motel_size; ++k) { for (int i = 0; i < 12; ++i) { for (int j = 0; j < 30; ++j) { if ((rooms + k)->availability[i][j] == resNo) { temp<<*(rooms+k); return; } } } } } };
Sample out: