0% found this document useful (0 votes)
2 views5 pages

Project 2 CarBuying

The document outlines a Python program that helps buyers select cars based on their preferences. It includes three classes: Car, CarShop, and CarBuyer, which manage car attributes, available cars, and buyer preferences respectively. The program allows users to input their budget, fuel efficiency, brand preference, and safety rating to receive car recommendations.

Uploaded by

Majid Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Project 2 CarBuying

The document outlines a Python program that helps buyers select cars based on their preferences. It includes three classes: Car, CarShop, and CarBuyer, which manage car attributes, available cars, and buyer preferences respectively. The program allows users to input their budget, fuel efficiency, brand preference, and safety rating to receive car recommendations.

Uploaded by

Majid Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Write a python program that helps a buyer to choose his desired car

based on available cars of different models and parameters from a car


shop. Consider the following while developing the python code:

1. Car: This class will represent a car with its attributes (price, fuel
efficiency, brand, etc.).

2. CarShop: This class will represent the car shop that contains
available cars.

3. CarBuyer: This class will assist the buyer in choosing a car


based on their preferences.

Python Code for Car Recommendation System:

1
import pandas as pd

# Class representing a single car

class Car:

def __init__(self, model, price, fuel_efficiency, brand, horsepower,


safety_rating, color):

self.model = model

self.price = price

self.fuel_efficiency = fuel_efficiency

self.brand = brand

self.horsepower = horsepower

self.safety_rating = safety_rating

self.color = color

def __repr__(self):

return f"{self.model} ({self.brand}): ${self.price},


{self.fuel_efficiency} MPG, Safety: {self.safety_rating} Stars"

# Class representing the car shop with available cars

class CarShop:

def __init__(self):

self.cars = []

def add_car(self, car):

self.cars.append(car)

def get_available_cars(self):

return self.cars

# Class representing a car buyer, who can choose a car based on


preferences

2
class CarBuyer:

def __init__(self, budget, min_fuel_efficiency, brand_preference,


min_safety_rating):

self.budget = budget

self.min_fuel_efficiency = min_fuel_efficiency

self.brand_preference = brand_preference

self.min_safety_rating = min_safety_rating

def filter_cars(self, cars):

# Filter cars based on the buyer's preferences

matching_cars = [

car for car in cars

if car.price <= self.budget

and car.fuel_efficiency >= self.min_fuel_efficiency

and (self.brand_preference.lower() in car.brand.lower() or


self.brand_preference == "")

and car.safety_rating >= self.min_safety_rating ]

return matching_cars

def recommend_car(self, car_shop):

available_cars = car_shop.get_available_cars()

filtered_cars = self.filter_cars(available_cars)

if not filtered_cars:

print("No cars match your criteria. Please adjust your


preferences.")

else:

print(f"\nHere are the cars that match your preferences (Price


<= ${self.budget}, Min Fuel Efficiency >= {self.min_fuel_efficiency}
MPG, Brand: {self.brand_preference}, Min Safety Rating >=
{self.min_safety_rating}):")

for car in filtered_cars:

3
print(car)

# Example usage:

# 1. Create a CarShop and add some cars

car_shop = CarShop()

# Adding some car models to the car shop

car_shop.add_car(Car('Honda Civic', 22000, 30, 'Honda', 158, 4.5,


'Red'))

car_shop.add_car(Car('Toyota Corolla', 21000, 32, 'Toyota', 139, 4.7,


'Blue'))

car_shop.add_car(Car('BMW 3 Series', 35000, 25, 'BMW', 255, 4.2,


'Black'))

car_shop.add_car(Car('Ford Focus', 20000, 28, 'Ford', 160, 4.3,


'White'))

car_shop.add_car(Car('Chevrolet Malibu', 23000, 26, 'Chevrolet', 160,


4.4, 'Silver'))

# 2. Ask for buyer preferences and create a CarBuyer object

print("Welcome to the car shop!\n")

# Get buyer preferences (for simplicity, input is taken as plain text)

try:

budget = float(input("Enter your maximum budget (in USD): $"))

min_fuel_efficiency = float(input("Enter the minimum fuel efficiency


(in MPG): "))

brand_preference = input("Enter your preferred brand (leave blank


for any): ")

min_safety_rating = float(input("Enter the minimum safety rating


(out of 5): "))

buyer = CarBuyer(budget, min_fuel_efficiency, brand_preference,


min_safety_rating)

# 3. Get recommendations based on buyer preferences

buyer.recommend_car(car_shop)

4
except ValueError:

print("Invalid input! Please enter valid numbers for budget, fuel


efficiency, and safety rating.")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy