Relationship between classes in Python

0

Imagine that you are developing a system for controlling a Vehicles. With this it is necessary to register the vehicles available in the concessionaire for sale.

a) Think about the data you need and create a class to represent vehicles.

b) Run the program by creating 3 different vehicles and displaying your data.

c) The sale of the vehicle must be recorded. Each vehicle may contain the date of sale and a reference to who bought it. Create the class that represents the buyer and create a method in the vehicle that makes the sale, informing the buyer and the date of sale.

d) Run the program by registering the sale of one of the registered vehicles.

e) We will improve the registration of the sale of the vehicle. Each vehicle may also contain reference to who sold the car. Create the class that represents the vendor and modify the method that makes the sale of the vehicle, also informing the seller.

f) Re-sell one of the registered vehicles.

class Concessionaria:

    #Metodo Construtor
    def __init__(self):
        self.carros = carros
        self.compradores = compradores

class Veiculo(Concessionaria):

    # Metodo Construtor:
    def __init__(self, modelo, ano, preço):
        self.modelo = modelo
        self.ano = ano
        self.preço = preço
        self.vendedor = None
        self.comprador = None


class Comprador(Concessionaria):

    # Metodo Construtor
    def __init__(self, nome, cpf, dinheiro):
        self.nome = nome
        self.cpf = cpf
        self.dinheiro = dinheiro
        self.carro_proprio = None

class Vendedor(Concessionaria):

    # Metodo Construtor
    def __init__(self, nome, cpf, carro):
        self.nome = nome
        self.cpf = cpf
        self.carro = carro


# Objeto:
carro1 = Veiculo('P.G-207', 2007, 25000)
comprador1 = Comprador('Rodrigo','05063499180',30000)

# Metodos:
carro1.venda_do_comprador(comprador1, 'Rodrigo')

# Visualizção
print('MODELO',' |','ANO',' |','PREÇO')
print(carro1.modelo,'|',  carro1.ano,'|',  carro1.preço)
  • My problem lies in not being able to relate the different classes in order to create the buying method.
  • I can not put the name of the object that belongs to the buyer class in the attributes of the Vehicle class object
asked by anonymous 30.09.2018 / 23:56

1 answer

0

TL; DR

In my opinion the main mistake of your approach is in modeling, I would do totally different, I made an adaptation to try to preserve your "style" to the maximum, to be simpler I disregarded the concessionaire. See I modeled a class for the sales operation and deleted the templates for buyer and seller by instantiating these objects from the Person class.

class Veiculo:
    def __init__(self, modelo, ano, preco):
        self.modelo = modelo
        self.ano = ano
        self.preco = preco
        self.vendedor = None
        self.comprador = None  

class Pessoa():
    def __init__(self, nome, cpf):
       self.nome = nome
       self.cpf = cpf

class Venda():
    def __init__(self, num_nf, veiculo, comprador, vendedor, valor):
        self.nf=num_nf
        self.veiculo = veiculo
        self.comprador = comprador
        self.vendedor = vendedor
        self.valor = valor    

veiculo = Veiculo('P.G-207', 2007, 25000)
comprador = Pessoa('Rodrigo','05063499180')
vendedor = Pessoa('João','099634996690')
venda = Venda('18-450', veiculo, comprador, vendedor, 30000)

print('Dados da venda:')
print('NF: '+venda.nf, 'Veiculo: '+veiculo.modelo+'/'+str(veiculo.ano), 
'Comprador: '+venda.comprador.nome, \
  'Vendedor: '+venda.vendedor.nome, 'Valor: '+str(venda.valor), sep='\n')

Output:

Dados da venda:
NF: 18-450
Veiculo: P.G-207/2007
Comprador: Rodrigo
Vendedor: João
Valor: 30000
  

Note:

     

Note that the answer presents the "general idea", you can add fields and methods in the classes to meet the statement.

See working at repl.it .

    
03.10.2018 / 03:38