Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions dependency-injection/betterpython/coupling_cohesion_after.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@


import string
import random

class VehicleInfo:

def __init__(self,brand, electric, catalogue_price) -> None:
self.brand = brand
self.electric = electric
self.catalogue_price = catalogue_price

def compute_tax(self):
tax_percentage = 0.05
if self.electric:
tax_percentage = 0.02
return tax_percentage

def print(self):
print("Registration complete. Vehicle information:")
print(f"Brand : {self.brand}")
print(f"Payable tax: {self.payable_tax}")

class Vehicle:

def __init__(self,id, license_plate, info:VehicleInfo) -> None:
self.id = id
self.license_plate = license_plate
self.info = info

def print(self):
print(f"Id : {self.id}")
print(f"License plate: {self.license_plate}")
self.info.print()

class VehicleRegistry:

def __init__(self):
self.vehicle_info = {}
self.add_vehicle_info("Tesla Model 3", True, 60000)
self.add_vehicle_info("Volkswagen ID3", True, 35000)
self.add_vehicle_info("BMW 5", False, 45000)
self.add_vehicle_info("Tesla Model Y", True, 75000)


def add_vehicle_info(self, brand, electric, catalogue_price):
self.vehicle_info[brand] = VehicleInfo(brand, electric, catalogue_price)

def generate_vehicle_id(self, length):
return ''.join(random.choices(string.ascii_uppercase, k=length))

def generate_vehicle_license(self, id):
return f"{id[:2]}-{''.join(random.choices(string.digits, k=2))}-{''.join(random.choices(string.ascii_uppercase, k=2))}"

def create_vehicle(self, brand):
id = self.generate_vehicle_id(12)
license_plate = self.generate_vehicle_license(id)
return Vehicle(id, license_plate, self.vehicle_info[brand])


class Application:

def register_vehicle(self,brand:string):

# create a registry instance
registry = VehicleRegistry()

vehicle = registry.create_vehicle(brand)

# print out the vehicle information
vehicle.print()


app = Application()
app.register_vehicle('BMW 5')

59 changes: 59 additions & 0 deletions dependency-injection/betterpython/coupling_cohesion_before.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@


import string
import random


class VehicleRegistry:

def generate_vehicle_id(self,length):
return ''.join(random.choices(string.ascii_uppercase, k=length))

def generate_vehicle_license(self,id):
return f"{id[:2]}-{''.join(random.choices(string.digits,k=2))}-{''.join(random.choices(string.ascii_uppercase,k=2))}"



class Application:

def register_vehicle(self,brand:string):

# create a resistry instance
registry = VehicleRegistry()

# generate a vehicle id of length of 12
vehicle_id = registry.generate_vehicle_id(12)

# now generate a license plate for the vehicle
# using the first two characters of the vehicle id
license_plate = registry.generate_vehicle_license(vehicle_id)

# compute the catalogue price
catalogue_price =0

if brand == 'Tesla' :
catalogue_price = 6000
elif brand == 'Volkswagen':
catalogue_price = 3500
elif brand == 'BMW':
catalogue_price = 4500

# compute the tax percentage (default 5% of the catalogue price, except for electric cars where it is 2%)
tax_percentage = 0.05
if brand == 'Tesla' or brand == 'Volkswagen':
tax_percentage = 0.02


#compute the payable tax
payable_tax = tax_percentage * catalogue_price

#print out the vehicle registration information
print("Registration complete. Vehicle information:")
print(f"Brand : {brand}")
print(f"Id : {vehicle_id}")
print(f"License plate: {license_plate}")
print(f"Payable tax: {payable_tax}")

app = Application()
app.register_vehicle('Tesla')