We are asked to design a class to assess the value for money of different hotels. You inform us that the following attributes are calculated for the hotels: name, location, rating, price. In addition to storing and displaying the hotels, we also need to be able to compare them in terms of value for money so that x <y means that hotel x is worse than hotel y in terms of value for money, and that two hotels are the same if they have the same value for money. The price-performance ratio of a hotel is defined by our customers as (Score ^ 2) ∗ 10 / price.
As a result, we need to be able to order a list of hotels in the order just defined, from smallest to largest.
We learn a little more about the properties of the hotels:
• The name and position cannot be empty strings.
• The score must be a number (no restriction on the value)
• The price must be a non-zero number.
We started designing the class:
The method init:
• Create objects of the hotel class with the specified attributes (name, location, score, price).
• The standard values for the construction are: score in 0, price in float ("inf") (infinite), name and location in * (the very high price is used so that if the price of a hotel is not reported, it will be used) take the highest possible value.
• We need to check that the score and price are numbers (we will use the number_ function that was used in the case of points). When a price becomes zero, its value is replaced with float ("inf") (to ensure that the price is never zero).
• We need to check that the name and position are not empty strings (for which we need to create an es_cadena_no_vacia function).
• If the data does not meet the requirements, a TypeError exception is raised.
There will be a method str To view hotels with a style chain:
"Hotel City de Mercedes – Score: 3.25 – Price: 78 Pesos".
Regarding the relationship between hotels, the class must have the methods necessary to make these comparisons and order a list of hotels.
Almost all tasks can be performed using the topics displayed for creating the Point class. For the last point, we have to introduce new special methods.
Hotel exercise. Write the function es_ca_no_vacia (value), which decides whether a value is a non-empty string or not, and insert it into the validation module.
The original fragment of the class programmed in Python looks like this: