From f7cca4604165e1a5bc988d0bfeff8d2b72f17434 Mon Sep 17 00:00:00 2001 From: Vinohradov Volodymyr Date: Sun, 5 Jan 2025 00:45:45 +0200 Subject: [PATCH 1/2] Implemented Car and CarWashStation classes with functionality for managing cars and providing car wash services. Added methods for washing cars, calculating service cost, rating the service, and maintaining a list of cars. --- app/main.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index b2d096bda..232c51e3d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,70 @@ class Car: - # write your code here - pass + cars_list = [] + + def __init__(self, comfort_class: int, + clean_mark: int, brand: str) -> None: + if not (1 <= comfort_class <= 7): + raise ValueError("Comfort class must be between 1 and 7.") + if not (1 <= clean_mark <= 10): + raise ValueError("Clean_mark must be between 1 and 10.") + + self.comfort_class = comfort_class + self.clean_mark = clean_mark + self.brand = brand + Car.cars_list.append(self) + + @property + def show_cars_list(self) -> list: + return Car.cars_list + + def __repr__(self) -> str: + return ", ".join(f"{key}={value!r}" for key, value + in self.__dict__.items()) class CarWashStation: - # write your code here - pass + def __init__(self, distance_from_city_center: float, + clean_power: int, average_rating: float, + count_of_ratings: int) -> None: + if not (1 <= distance_from_city_center <= 10): + raise ValueError("distance_from_city_center") + if not (1 <= average_rating <= 5): + raise ValueError("average raiting") + self.distance_from_city_center = distance_from_city_center + self.clean_power = clean_power + self.average_rating = average_rating + self.count_of_ratings = count_of_ratings + + def wash_single_car(self, one_car: Car) -> Car: + if self.clean_power > one_car.clean_mark: + one_car.clean_mark = self.clean_power + return one_car + + def serve_cars(self, car_list: list | Car) -> int: + income = 0 + for car in car_list: + if car.clean_mark < self.clean_power: + i = (car.comfort_class * (self.clean_power - car.clean_mark) + * self.average_rating / self.distance_from_city_center) + income += round(float(f"{i: .2f}"), 1) + self.wash_single_car(car) + return income + + def calculate_washing_price(self, car: Car) -> float: + income = 0 + formula = (car.comfort_class * (self.clean_power - car.clean_mark) + * self.average_rating / self.distance_from_city_center) + income += round(float(f"{formula: .2f}"), 1) + return income + + def rate_service(self, rate: int) -> float: + + formula = (((self.average_rating * self.count_of_ratings) + rate) + / (self.count_of_ratings + 1)) + self.average_rating = round(float(f"{formula}"), 1) + self.count_of_ratings += 1 + return self.average_rating + + def __repr__(self) -> str: + return ", ".join(f"{key}={value!r}" for key, value + in self.__dict__.items()) From fe8b30c15a46d7aeb6c50bb320247c84dda6d924 Mon Sep 17 00:00:00 2001 From: Vinohradov Volodymyr Date: Sun, 5 Jan 2025 10:04:40 +0200 Subject: [PATCH 2/2] - Improved error handling by adding appropriate messages for ValueErrors. - Updated the valid range of `istance_from_city_center` from 1-10 to 0-10. - Refactored `serve_cars` to eliminate duplicate code by reusing `calculate_washing_price` function. --- app/main.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/app/main.py b/app/main.py index 232c51e3d..53b9a5ed6 100644 --- a/app/main.py +++ b/app/main.py @@ -26,10 +26,10 @@ class CarWashStation: def __init__(self, distance_from_city_center: float, clean_power: int, average_rating: float, count_of_ratings: int) -> None: - if not (1 <= distance_from_city_center <= 10): - raise ValueError("distance_from_city_center") + if not (0 <= distance_from_city_center <= 10): + raise ValueError("distance from city must be between 0 and 10") if not (1 <= average_rating <= 5): - raise ValueError("average raiting") + raise ValueError("average raiting must be between 1 and 5") self.distance_from_city_center = distance_from_city_center self.clean_power = clean_power self.average_rating = average_rating @@ -40,16 +40,6 @@ def wash_single_car(self, one_car: Car) -> Car: one_car.clean_mark = self.clean_power return one_car - def serve_cars(self, car_list: list | Car) -> int: - income = 0 - for car in car_list: - if car.clean_mark < self.clean_power: - i = (car.comfort_class * (self.clean_power - car.clean_mark) - * self.average_rating / self.distance_from_city_center) - income += round(float(f"{i: .2f}"), 1) - self.wash_single_car(car) - return income - def calculate_washing_price(self, car: Car) -> float: income = 0 formula = (car.comfort_class * (self.clean_power - car.clean_mark) @@ -57,6 +47,14 @@ def calculate_washing_price(self, car: Car) -> float: income += round(float(f"{formula: .2f}"), 1) return income + def serve_cars(self, car_list: list[Car]) -> int: + income = 0 + for car in car_list: + if car.clean_mark < self.clean_power: + income += self.calculate_washing_price(car) + self.wash_single_car(car) + return income + def rate_service(self, rate: int) -> float: formula = (((self.average_rating * self.count_of_ratings) + rate)