2

there are a vehicle class and customer class . In short, in the customer class there is a function that shows 'can this person or company rent that car'.The function uses a object of vehicle and compares with the customer so my question is in the class diagram which association should be there to show this connection between classes?


class Vehicle:
    
    def __init__(self,Id,Type,numberPlate,basePrice):
        self.Id=Id
        self.Type=Type
        self.numberPlate=numberPlate
        self.basePrice=basePrice
        
        
    def calculatecost(self):
        print("just working for subclasses")
        
        
        
        
        
class Car(Vehicle):

    def __init__(self,Id,Type,numberPlate,basePrice,airbag,gearType,age):
            super().__init__(Id,Type,numberPlate,basePrice)
            self.airbag=airbag
            self.gearType=gearType
            self.age=age
            
            
    def calculatecost(self):
            self.price=0
            if self.airbag:
                self.price=self.basePrice+50
            if self.age<5:
                self.price+=50
            return self.price   
        
        
        


class Customer:
    def __init__(self,customerId,name):
        self.customerId=customerId
        self.name=name
    def rentVehicle(self,vehicle):
        if self.canRent(vehicle):
            print(f"{self.name} rented {vehicle.Type} with ID {vehicle.Id}.")
        else:
            print(f"{self.name} cannot rent  with ID {vehicle.Id}.")
    def canRent(self):
        print("just working for subclasses")

            
car1=Car(1,"Car","60bc60",200,True,"automatic",3)
print(car1.calculatecost())
person1=Person(100,"ali","B")
person1.rentVehicle(car1)
1
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Nov 17, 2023 at 21:37

3 Answers 3

1

After some research I've decided to switch from my earlier answer to a directed dependency association.

enter image description here

This is because Customer does not hold Vehicle as part of its state. It merely takes a Vehicle as an argument to a method.

My thanks to plantuml.com for the diagrams. You can reproduce this one with the following code:

@startuml
class Vehicle 
class Car 
class Customer 
class Person

note top: Not explicitly defined.\nImplied by other code

Vehicle : id
Vehicle : Type
Vehicle : numberPlate
Vehicle : calculatecost()
Car : airbag
Car : geartype
Car : age
Car : calculatecost()
Customer : customerID
Customer : name
Customer : rentVehicle(Vehicle)
Customer : canRent()

Vehicle <|- Car : =is a
Customer .> Vehicle : =references
note top on link : indicated by code in Customer.\n<b>def rentVehicle(self,vehicle)</b>
Person <|- Customer : =is a
@enduml
2
  • It is correct, that the dependencies in the source code are depicted in this diagram. However, the source code leaves it open, how canRent is implemented. If it evaluates properties of Vehicle and Customer, like for example Customer.licence and Vehicle.requiredLicense, then nothing else is needed. However, if there is a list of vehicles the customer is allowed to rent, this could be done with an association. Commented Dec 10, 2023 at 13:51
  • the keyword «reference» correspond to very specific dependencies for packages and profile elements, unless you create an own «References» stereotype in your UML profile.
    – Christophe
    Commented Mar 24 at 18:27
0

in the class diagram which association should be there to show this connection between classes?

A directed using association:

enter image description here

in the customer class there is a function that shows 'can this person or company rent that car'.

Yeah, but there's a bug here in Customer. def canRent(self): doesn't take a Vehicle. This is despite rentVehicle() calling it, self.canRent(vehicle), as if it did. But since rentVehicle() takes a Vehicle we know there is a using relationship from Customer to Vehicle.

0

In short

The challenge to answer the question is that the design is incomplete, and it will not allow to address the needs that we can guess from your current implementation.

More details

Representing your current implementation

In your implementation, the link of the Customer with a Vehicle is transient: it will only last for the time of an operation. This is not at all sufficient to justify an association; there is only a usage dependency from the Customer to Vehicle. , because one or several Customer operations need to know about Vehicle (the right keyword would be «use»):

enter image description here

UML 2.5.1 specification section 7.7.3.2: A Usage is a Dependency in which one NamedElement requires another NamedElement (or set of NamedElements) for its full implementation or operation. The Usage does not specify how the client uses the supplier other than the fact that the supplier is used by the definition or implementation of the client.

Additional remark: I think that canRent() has in its definition a mission argument ;-)

Why this design will not address the needs?

When reading the following statement in the implementation of rentVehicle(), we immediately understand that the system leaves a trace of which customer rented what vehicle:

        print(f"{self.name} rented {vehicle.Type} with ID {vehicle.Id}.")

We can deduce from there that to meet the needs, you will sooner or later have to add an association between Customer and Vehicle. This is currently missing, both in the narrative and in your implementation. You may then probably want to already add an operation returnVehicle(). Since a customer may rent several vehicles, and each vehicle may be rented by several customers, you'd have a many-to-many association:

enter image description here

How to fix it:

Moreover, for invoicing purpose, you'd probably also need some start and end date of each rental. So for each "link" (i.e. occurence of an association), you'll need to have additional properties that are neither belonging to the Verhicle nor to the Customer. You'd probably need an association class:

enter image description here

To implement such an association-class, is another story. You could for example think to maintain redundant collections (list of vehicles rented by each customer and list of customers that rented each vehicle), or use a dictionary that for each pair of customers/vehicle would associate a Rental instance.

Try pyreverse for automating diagrams?

pyreverse is part of pylint, and it can generate automatically some UML class diagram from your Python code. It's far from perfect (it doesn't show usage dependencies), but it's a good start, even more as it can generate plantuml diagrams that you can still fine tune for what was not detected automatically (yet).

Here's the result for your code sinppet (which doesn't include the Person class): enter image description here

Here's the result when adding:

class Rental: 
    def __init__(self,customer:Customer, vehicle:Vehicle):
        self.customer=customer
        self.vehicle=vehicle

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.