<\/span><\/h2>\n\n\n\nIn this challenge, the task is to debug the existing code to successfully execute all provided test files.<\/p>\n\n\n\n
The given code defines two classes HotelRoom<\/code> and HotelApartment<\/code> denoting respectively a standard hotel room and a hotel apartment. An instance of any of these classes has two parameters: bedrooms<\/code> and bathrooms<\/code> denoting respectively the number of bedrooms and the number of bathrooms in the room.<\/p>\n\n\n\nThe prices of a standard hotel room and hotel apartment are given as:<\/p>\n\n\n\n
- Hotel Room: 50. bedrooms<\/em><\/strong> + 100.bathrooms<\/em><\/strong>.<\/li>
- Hotel Apartment: The price of a standard room with the same number bedrooms and bathrooms plus 100<\/strong>.<\/li><\/ul>\n\n\n\n
For example, if a standard room costs 200<\/strong>, then an apartment with the same number of bedrooms and bathrooms costs 300<\/strong>.<\/p>\n\n\n\nIn hotel’s codebase, there is a piece of code reading the list of rooms booked for today and calculates the total profit for the hotel. However, sometimes calculated profits are lower than they should be.<\/p>\n\n\n\n
Debug the existing HotelRoom<\/code> and HotelApartment<\/code> classes’ implementations so that the existing code computing the total profit returns a correct profit.<\/p>\n\n\n\nYour function will be tested against several cases by the locked template code.<\/p>\n\n\n\n
<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe input is read by the provided locked code template.
In the first line, there is a single integer n<\/em><\/strong> denoting the number of rooms booked for today.
After that n<\/em><\/strong> lines follow. Each of these lines begins with a room_type<\/code> which is either standard<\/code> or apartment<\/code>, and is followed by the number of bedrooms and the number of bathrooms in this room.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= n<\/em> <= 100<\/strong><\/li>
- There is at least 1<\/strong> and at most 5<\/strong> bedrooms in a room<\/li>
- There is at least 1<\/strong> and at most 5<\/strong> bathrooms in a room<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nThe output is produced by the provided and locked code template. It calculates the total profit by iterating through the vector of all rooms read from the input.<\/p>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\n2\nstandard 3 1\napartment 1 1<\/code><\/pre>\n\n\n\nSample Output 0<\/strong><\/p>\n\n\n\n500<\/code><\/pre>\n\n\n\nExplanation 0<\/strong><\/p>\n\n\n\nIn the sample we have one standard room with 3<\/strong> bedrooms and 1<\/strong> bathroom, and one apartment with one 1<\/strong> bedrooms and 1<\/strong> bathroom. The price for the room is 3.50 + 100 = 250<\/strong>. The price for the apartment is 50 + 100 + 100 = 250<\/strong>. Thus the hotel profit is 250 + 250 = 500<\/strong> as the sum of prices of both rooms.<\/p>\n\n\n\n<\/span>Solution – Hotel Prices in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <vector>\n\nusing namespace std;\n\nclass HotelRoom {\npublic:\n HotelRoom(int bedrooms, int bathrooms) \n : bedrooms_(bedrooms), bathrooms_(bathrooms) {}\n \n int get_price() {\n return 50*bedrooms_ + 100*bathrooms_;\n }\nprivate:\n int bedrooms_;\n int bathrooms_;\n};\n\nclass HotelApartment : public HotelRoom {\npublic:\n HotelApartment(int bedrooms, int bathrooms) \n : HotelRoom(bedrooms + 2, bathrooms) {}\n\n int get_price() {\n return HotelRoom::get_price() + 100;\n }\n};\n\nint main() {\n int n;\n cin >> n;\n vector<HotelRoom*> rooms;\n for (int i = 0; i < n; ++i) {\n string room_type;\n int bedrooms;\n int bathrooms;\n cin >> room_type >> bedrooms >> bathrooms;\n if (room_type == \"standard\") {\n rooms.push_back(new HotelRoom(bedrooms, bathrooms));\n } else {\n rooms.push_back(new HotelApartment(bedrooms, bathrooms));\n }\n }\n\n int total_profit = 0;\n for (auto room : rooms) {\n total_profit += room->get_price();\n }\n cout << total_profit << endl;\n\n for (auto room : rooms) {\n delete room;\n }\n rooms.clear();\n\n return 0;\n}<\/pre>\n\n\n\n