Relation Between Classes in Python

Asked

Viewed 942 times

0

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

a) Think about the necessary data 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 the buyer. Create the class representing the buyer and create a method in the vehicle carrying out the sale of it, informing the buyer and the date of the 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 the reference to who sold the car. Create the class that represents the seller and modify the method that carries out 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 is that I can’t relate the different classes to create the purchase method.
  • because I cannot put the name of the object belonging to the buyer class in the attributes of the object of the Vehicle class
  • 1

    Basically you need to complete the classes with the methods so that you can use them. You are trying to use the sales_do_buyer() method that does not yet exist. It should be within the Vehicle class().

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 dealership. See that I modeled a class for the sale transaction and eliminated the models for buyer and seller, instantiating these objects of the class Person.

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

Observing:

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

See working on repl it..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.