Dados los datos para la gestión del Hotel y el Usuario:
Datos del Hotel:
Hotel Name Room Available Location Rating Price per Room H1 4 Bangalore 5 100 H2 5 Bangalore 5 200 H3 6 Mumbai 3 100
Datos del usuario:
User Name UserID Booking Cost U1 2 1000 U2 3 1200 U3 4 1100
La tarea es responder a la siguiente pregunta.
- Imprime los datos del hotel.
- Ordenar hoteles por nombre.
- Ordenar Hotel por calificación más alta.
- Imprimir los datos del hotel para la ubicación de Bangalore.
- Ordenar hoteles por número máximo de habitaciones Disponibles.
- Imprimir datos de reserva del usuario.
La ronda de codificación automática implica resolver un problema de diseño en cuestión de un par de horas.
Requiere diseñar y codificar una solución limpia, modular y extensible basada en un conjunto específico de requisitos.
Acercarse:
- Cree clases para datos de hotel y datos de usuario.
- Inicialice las variables que almacenan los datos del hotel y los datos del usuario.
- Cree Objetos para Hotel y clases de usuario que accedan a los datos del Hotel y los datos del Usuario.
- inicialice dos arrays de vectores que contienen los datos del hotel y los datos del usuario.
- Resuelva las preguntas anteriores una por una.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program to solve // the given question #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; // Create class for hotel data. class Hotel { public: string name; int roomAvl; string location; int rating; int pricePr; }; // Create class for user data. class User : public Hotel { public: string uname; int uId; int cost; }; // Function to Sort Hotels by // Bangalore location bool sortByBan(Hotel& A, Hotel& B) { return A.name > B.name; } // Function to sort hotels // by rating. bool sortByr(Hotel& A, Hotel& B) { return A.rating > B.rating; } // Function to sort hotels // by rooms availability. bool sortByRoomAvailable(Hotel& A, Hotel& B) { return A.roomAvl < B.roomAvl; } // Print hotels data. void PrintHotelData(vector<Hotel> hotels) { cout << "PRINT HOTELS DATA:" << endl; cout << "HotelName" << " " << "Room Available" << " " << "Location" << " " << "Rating" << " " << "PricePer Room:" << endl; for (int i = 0; i < 3; i++) { cout << hotels[i].name << " " << hotels[i].roomAvl << " " << hotels[i].location << " " << hotels[i].rating << " " << hotels[i].pricePr << endl; } cout << endl; } // Sort Hotels data by name. void SortHotelByName(vector<Hotel> hotels) { cout << "SORT BY NAME:" << endl; std::sort(hotels.begin(), hotels.end(), sortByBan); for (int i = 0; i < hotels.size(); i++) { cout << hotels[i].name << " " << hotels[i].roomAvl << " " << hotels[i].location << " " << hotels[i].rating << " " << " " << hotels[i].pricePr << endl; } cout << endl; } // Sort Hotels by rating void SortHotelByRating(vector<Hotel> hotels) { cout << "SORT BY A RATING:" << endl; std::sort(hotels.begin(), hotels.end(), sortByr); for (int i = 0; i < hotels.size(); i++) { cout << hotels[i].name << " " << hotels[i].roomAvl << " " << hotels[i].location << " " << hotels[i].rating << " " << " " << hotels[i].pricePr << endl; } cout << endl; } // Print Hotels for any city Location. void PrintHotelBycity(string s, vector<Hotel> hotels) { cout << "HOTELS FOR " << s << " LOCATION IS:" << endl; for (int i = 0; i < hotels.size(); i++) { if (hotels[i].location == s) { cout << hotels[i].name << " " << hotels[i].roomAvl << " " << hotels[i].location << " " << hotels[i].rating << " " << " " << hotels[i].pricePr << endl; } } cout << endl; } // Sort hotels by room Available. void SortByRoomAvailable(vector<Hotel> hotels) { cout << "SORT BY ROOM AVAILABLE:" << endl; std::sort(hotels.begin(), hotels.end(), sortByRoomAvailable); for (int i = hotels.size() - 1; i >= 0; i--) { cout << hotels[i].name << " " << hotels[i].roomAvl << " " << hotels[i].location << " " << hotels[i].rating << " " << " " << hotels[i].pricePr << endl; } cout << endl; } // Print the user's data void PrintUserData(string userName[], int userId[], int bookingCost[], vector<Hotel> hotels) { vector<User> user; User u; // Access user data. for (int i = 0; i < 3; i++) { u.uname = userName[i]; u.uId = userId[i]; u.cost = bookingCost[i]; user.push_back(u); } // Print User data. cout << "PRINT USER BOOKING DATA:" << endl; cout << "UserName" << " " << "UserID" << " " << "HotelName" << " " << "BookingCost" << endl; for (int i = 0; i < user.size(); i++) { cout << user[i].uname << " " << user[i].uId << " " << hotels[i].name << " " << user[i].cost << endl; } } // Functiont to solve // Hotel Management problem void HotelManagement(string userName[], int userId[], string hotelName[], int bookingCost[], int rooms[], string locations[], int ratings[], int prices[]) { // Initialize arrays that stores // hotel data and user data vector<Hotel> hotels; // Create Objects for // hotel and user. Hotel h; // Initialise the data for (int i = 0; i < 3; i++) { h.name = hotelName[i]; h.roomAvl = rooms[i]; h.location = locations[i]; h.rating = ratings[i]; h.pricePr = prices[i]; hotels.push_back(h); } cout << endl; // Call the various operations PrintHotelData(hotels); SortHotelByName(hotels); SortHotelByRating(hotels); PrintHotelBycity("Bangalore", hotels); SortByRoomAvailable(hotels); PrintUserData(userName, userId, bookingCost, hotels); } // Driver Code. int main() { // Initialize variables to stores // hotels data and user data. string userName[] = { "U1", "U2", "U3" }; int userId[] = { 2, 3, 4 }; string hotelName[] = { "H1", "H2", "H3" }; int bookingCost[] = { 1000, 1200, 1100 }; int rooms[] = { 4, 5, 6 }; string locations[] = { "Bangalore", "Bangalore", "Mumbai" }; int ratings[] = { 5, 5, 3 }; int prices[] = { 100, 200, 100 }; // Function to perform operations HotelManagement(userName, userId, hotelName, bookingCost, rooms, locations, ratings, prices); return 0; }
Python3
# Python program to solve # the given question # Create class for hotel data. class Hotel : sortParam='name' def __init__(self) -> None: self.name='' self.roomAvl=0 self.location='' self.rating=int self.pricePr=0 def __lt__(self,other): getattr(self,Hotel.sortParam)<getattr(other,Hotel.sortParam) # Function to change sort parameter to # name @classmethod def sortByName(cls): cls.sortParam='name' # Function to change sort parameter to # rating. @classmethod def sortByRate(cls): cls.sortParam='rating' # Function to change sort parameter to # room availability. @classmethod def sortByRoomAvailable(cls) : cls.sortParam='roomAvl' def __repr__(self) -> str: return "PRHOTELS DATA:\nHotelName:{}\tRoom Available:{}\tLocation:{}\tRating:{}\tPricePer Room:{}".format(self.name,self.roomAvl,self.location,self.rating,self.pricePr) # Create class for user data. class User: def __init__(self) -> None: self.uname='' self.uId=0 self.cost=0 def __repr__(self) -> str: return "UserName:{}\tUserId:{}\tBooking Cost:{}".format(self.uname,self.uId,self.cost) # Print hotels data. def PrintHotelData(hotels): for h in hotels: print(h) # Sort Hotels data by name. def SortHotelByName(hotels): print("SORT BY NAME:") Hotel.sortByName() hotels.sort() PrintHotelData(hotels) print() # Sort Hotels by rating def SortHotelByRating(hotels): print("SORT BY A RATING:") Hotel.sortByRate() hotels.sort() PrintHotelData(hotels) print() # Print Hotels for any city Location. def PrintHotelBycity(s,hotels): print("HOTELS FOR {} LOCATION ARE:".format(s)) hotelsByLoc=[h for h in hotels if h.location==s] PrintHotelData(hotelsByLoc) print() # Sort hotels by room Available. def SortByRoomAvailable(hotels): print("SORT BY ROOM AVAILABLE:") Hotel.sortByRoomAvailable() hotels.sort() PrintHotelData(hotels) print() # Print the user's data def PrintUserData(userName, userId, bookingCost, hotels): users=[] # Access user data. for i in range(3) : u=User() u.uname = userName[i] u.uId = userId[i] u.cost = bookingCost[i] users.append(u) for i in range(len(users)) : print(users[i],"\tHotel name:",hotels[i].name) # Functiont to solve # Hotel Management problem def HotelManagement(userName, userId, hotelName, bookingCost, rooms, locations, ratings, prices): # Initialize arrays that stores # hotel data and user data hotels=[] # Create Objects for # hotel and user. # Initialise the data for i in range(3) : h=Hotel() h.name = hotelName[i] h.roomAvl = rooms[i] h.location = locations[i] h.rating = ratings[i] h.pricePr = prices[i] hotels.append(h) print() # Call the various operations PrintHotelData(hotels) SortHotelByName(hotels) SortHotelByRating(hotels) PrintHotelBycity("Bangalore", hotels) SortByRoomAvailable(hotels) PrintUserData(userName, userId, bookingCost, hotels) # Driver Code. if __name__ == '__main__': # Initialize variables to stores # hotels data and user data. userName = ["U1", "U2", "U3"] userId = [2, 3, 4] hotelName = ["H1", "H2", "H3"] bookingCost = [1000, 1200, 1100] rooms = [4, 5, 6] locations = ["Bangalore", "Bangalore", "Mumbai"] ratings = [5, 5, 3] prices = [100, 200, 100] # Function to perform operations HotelManagement(userName, userId, hotelName, bookingCost, rooms, locations, ratings, prices)
Producción
PRINT HOTELS DATA: HotelName Room Available Location Rating PricePer Room: H1 4 Bangalore 5 100 H2 5 Bangalore 5 200 H3 6 Mumbai 3 100 SORT BY NAME: H3 6 Mumbai 3 100 H2 5 Bangalore 5 200 H1 4 Bangalore 5 100 SORT BY A RATING: H1 4 Bangalore 5 100 H2 5 Bangalore 5 200 H3 6 Mumbai 3 100 HOTELS FOR Bangalore LOCATION IS: H1 4 Bangalore 5 100 H2 5 Bangalore 5 200 SORT BY ROOM AVAILABLE: H3 6 Mumbai 3 100 H2 5 Bangalore 5 200 H1 4 Bangalore 5 100 PRINT USER BOOKING DATA: UserName UserID HotelName BookingCost U1 2 H1 1000 U2 3 H2 1200 U3 4 H3 1100